BLE_bluetooth Can't recieve data from arduino (esp32)

Hi,

I’m trying to recieve some response from arduino to thunkable. When I try to send to arduino ther’s no problem, recieving seems not to fire any erros but no response is shown.

image

I have arduino code:

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
    Ported to Arduino ESP32 by Evandro Copercini
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
const byte ledG = 33;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"


class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();

      if (value == "0") {
        digitalWrite(ledG, HIGH);
      } else {
        digitalWrite(ledG, LOW);
      }
    }

   void onRead(BLECharacteristic *pCharacteristic) {
      int MyResponse;
      
      if (digitalRead(ledG) == LOW){
        MyResponse = 1;
      } else { 
        MyResponse = 0;
      }

      pCharacteristic->setValue(MyResponse);
    }

    /*void onRead(BLECharacteristic *pCharacteristic) {
      struct timeval tv;
      gettimeofday(&tv, nullptr);
      std::ostringstream os;
      os << "Time: " << tv.tv_sec;
      pCharacteristic->setValue(os.str());
    }*/
};

void setup() {
  Serial.begin(115200);

  Serial.println("1- Download and install an BLE scanner app in your phone");
  Serial.println("2- Scan for BLE devices in the app");
  Serial.println("3- Connect to MyESP32");
  Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
  Serial.println("5- See the magic =)");

  pinMode(ledG, OUTPUT);

  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Hi @proximilius, hope you are having a great weekend. Our engineers made an update to our platform last night that should have resolved this. Can you please test this again?

Many thanks, now it works.

Maybe the problem was also my “onRead” function inside the arduino script. With the old script my information label on the app refreshed into a blank with no text. After a small change now everything seems to be working.

New arduino snippet (with un changed thunkable blocks):

void onRead(BLECharacteristic *pCharacteristic) {
      int MyResponse;
      
      if (digitalRead(ledG) == LOW){
        pCharacteristic->setValue("1");
      } else { 
        pCharacteristic->setValue("0");
      }
   }