Certainly! Below is a detailed post explaining the issue you're facing with MIT App Inventor and the steps to resolve it. You can share this post on forums, communities, or with anyone who might be able to help.
Problem: MIT App Inventor BLE Communication Issue with ESP32
I’m working on an MIT App Inventor app to control an ESP32 Motor Controller via Bluetooth Low Energy (BLE). The app scans for BLE devices, connects to the ESP32, and sends motor increment values (e.g., [15]
, [30]
) to control the motor. However, I’m facing an issue where the app doesn’t seem to send data correctly, and I’m getting errors like:
[NSTaggedPointerString count]: unrecognized selector sent to instance 0x9f437e033e88b6ba. Irritants: ()
Additionally, when I use the StringsWritten
event to confirm that data was sent, the app doesn’t behave as expected. Here’s a breakdown of the problem and what I’ve tried so far.
App Overview
-
Functionality:
- The app scans for BLE devices and connects to the ESP32.
- It sends motor increment values (e.g.,
[15]
,[30]
) to the ESP32 using theWriteStrings
method. - The
StringsWritten
event is used to confirm that the data was sent.
-
Blocks in Use:
WriteStrings
: Sends data to the ESP32.StringsWritten
: Confirms that data was sent.Label2
: Displays the status of the operation.
-
Expected Behavior:
- When a button is clicked (e.g.,
inc15
), the app sends[15]
to the ESP32. - The
StringsWritten
event updatesLabel2
to confirm the data was sent.
- When a button is clicked (e.g.,
Issue Details
-
Error Message:
- The error
[NSTaggedPointerString count]: unrecognized selector sent to instance
occurs when the app tries to perform a list operation on a string. This suggests that somewhere in the blocks, a string is being treated as a list.
- The error
-
StringsWritten
Event:- When the
StringsWritten
event is triggered,Label2
is updated with the value ofglobal increment
(e.g.,"[15]"
). This suggests that the data was sent successfully, but the ESP32 doesn’t seem to respond.
- When the
-
ESP32 Behavior:
- The ESP32 is set up to receive data in the format
[increment]
(e.g.,[15]
). - The
onWrite
callback on the ESP32 should print the received data to the Serial Monitor, but nothing is being printed.
- The ESP32 is set up to receive data in the format
What I’ve Tried
-
Correcting the
WriteStrings
Block:- Ensured that the data is sent in the correct format (
[increment]
). - Example:
when confirm2.Click do if BluetoothLE1.IsDeviceConnected do call BluetoothLE1.WriteStrings serviceUUID: get Global SERVICE_UUID characteristicUUID: get Global CHARACTERISTIC_UUID_RX utf16: false value: join "[" get Global Increment "]" do set Label3.Text to "Sent: " & join "[" get Global Increment "]" else do set Label3.Text to "Failed, Bluetooth not connected!"
- Ensured that the data is sent in the correct format (
-
Handling the
StringsWritten
Event:- Updated
Label2
to confirm that the data was sent. - Example:
when BluetoothLE1.StringsWritten serviceUuid: serviceUuid characteristicUuid: characteristicUuid stringValues: stringValues do set Label3.Text to "Data sent: " & stringValues
- Updated
-
Debugging the ESP32:
- Added debug statements in the
onWrite
callback to confirm that data is being received. - Example:
void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); Serial.print("Received: "); Serial.println(value.c_str()); }
- Added debug statements in the
My Questions
-
Why is the ESP32 not receiving the data?
- Are there any issues with the UUIDs or the BLE connection?
- Is the data format (
[increment]
) correct?
-
How can I debug this further?
- Are there any tools or techniques to verify that the data is being sent correctly?
If anyone has faced a similar issue or has suggestions for debugging, I’d greatly appreciate your help! Thank you in advance!