BLE write strings NOT Sending

image





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

  1. 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 the WriteStrings method.
    • The StringsWritten event is used to confirm that the data was sent.
  2. Blocks in Use:

    • WriteStrings: Sends data to the ESP32.
    • StringsWritten: Confirms that data was sent.
    • Label2: Displays the status of the operation.
  3. Expected Behavior:

    • When a button is clicked (e.g., inc15), the app sends [15] to the ESP32.
    • The StringsWritten event updates Label2 to confirm the data was sent.

Issue Details

  1. 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.
  2. StringsWritten Event:

    • When the StringsWritten event is triggered, Label2 is updated with the value of global increment (e.g., "[15]"). This suggests that the data was sent successfully, but the ESP32 doesn’t seem to respond.
  3. 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.

What I’ve Tried

  1. 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!"
      
  2. 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
      
  3. 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());
      }
      

My Questions

  1. 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?
  2. 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!

Your strings written event has reversed date flow.

Also, global increment is not a list of numbers, but just bracketed text

This is probably the most relevant thing to the error shown. That error is an error on the iOS companion, and it's due to the fact that the component expects a list of strings, which it tries to take the count of in order to iterate of it. If you wrap the reference to global increment in a make a list block it should work.



@ewpatton @ABG Thank you for your suggestions! I implemented the changes, and the error has been resolved. However, I’m still not receiving any data on the ESP32, even though it shows as connected (as seen in the attached image). To provide more context, I’m also sharing the Arduino sketch in case it helps identify the issue.

Rotating_Table_Bluetooth2_MS2.ino.ino (5.1 KB)

Can you confirm you are using the correct UUIDs? In your latest screenshot, I see you have ReadStrings and WriteStrings, but both use CHARACTERISTIC_UUID_RX. Presumably one of these should be CHARACTERISTIC_UUID_TX?