# BLE Receive Arduino IDE VS MicroPython

**URL:** <https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372>\
**Category:** BLE / BlueTooth\
**Created:** [August 3, 2023, 8:42am UTC](https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372 "2023-08-03T08:42:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickhosewolup](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nickhosewolup](https://community.thunkable.com/u/nickhosewolup)\
**Post date:** [August 3, 2023, 8:42am UTC](https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372/1 "2023-08-03T08:42:30Z")

</div>

Hi everyone!  
I developed an app in Thunkable X and coded in Arduino IDE, enabling communication between them via BLE. Sending and receiving data worked well for both the app and my ESP32.

However, I decided to explore a different approach and rewrote my code in MicroPython. Now, I can successfully send data from the app to my ESP32, but encountering an issue when sending data back from ESP32 to the app. A BLE terminal works fine with both the Arduino IDE and MicroPython codes, but in Thunkable, only the Arduino code works for receiving data.

Both codes have the same TX UUID set as NOTIFY and READ (see pictures below), so it’s unclear why the problem persists.

I have included my codes and screenshots of the BLE scanner to illustrate the UUID properties for both codes are the same. Any assistance you can provide would be greatly appreciated. Thank you!

Arduino IDE code (BLE part):

```auto
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

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

      if (rxValue.length() > 0) {
        Serial.println(" *********");
        Serial.print("Received Value: ");

        for (int i = 0; i < rxValue.length(); i++) {
          Serial.print(rxValue[i]);
        }

        Serial.println();

        rxValueString = rxValue.c_str();
        verwerkt = 1;
      }
    }
};
  // Create the BLE Device
  BLEDevice::init("ChickMate"); // Give it a name

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
                    );
                      
  pCharacteristic->addDescriptor(new BLE2902());
  pCharacteristic->setCallbacks(new MyCallbacks());

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");

```

MicroPyton code (BLE part):

```auto
ble_msg = ""

class ESP32_BLE():
    def __init__ (self, name):
        # Create internal objects for the onboard LED
        # blinking when no BLE device is connected
        # stable ON when connected
        self.led = Pin(2, Pin.OUT)
        self.timer1 = Timer(0)
        
        self.name = name
        self.ble = ubluetooth.BLE()
        self.ble.active(True)
        self.disconnected()
        self.ble.irq(self.ble_irq)
        self.register()
        self.advertiser()

    def connected(self):
        self.led.value(1)
        self.timer1.deinit()
        print("BLE device connected succesfully")

    def disconnected(self):        
        self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))
        print("No BLE device connected")

    def ble_irq(self, event, data):
        global ble_msg
        
        if event == 1: #_IRQ_CENTRAL_CONNECT:
                       # A central has connected to this peripheral
            self.connected()

        elif event == 2: #_IRQ_CENTRAL_DISCONNECT:
                         # A central has disconnected from this peripheral.
            self.advertiser()
            self.disconnected()
        
        elif event == 3: #_IRQ_GATTS_WRITE:
                         # A client has written to this characteristic or descriptor.          
            buffer = self.ble.gatts_read(self.rx)
            ble_msg = buffer.decode('UTF-8').strip()
            
    def register(self):        
        # Nordic UART Service (NUS)
        NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
        RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
        TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
        
        #NUS_UUID = '0000AAAA-0000-1000-8000-00805F9B34FB'
        #RX_UUID = '0000AAAA-0002-1000-8000-00805F9B34FB'
        #TX_UUID = '0000AAAA-0001-1000-8000-00805F9B34FB'
            
        BLE_NUS = ubluetooth.UUID(NUS_UUID)
        BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
        BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_READ) # Add the READ property here
            
        BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
        SERVICES = (BLE_UART, )
        ((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)

    def send(self, data):
        self.ble.gatts_notify(0, self.tx, data)

    def advertiser(self):
        name = bytes(self.name, 'UTF-8')
        adv_data = bytearray(b'\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
        self.ble.gap_advertise(100, adv_data)
        print(adv_data)
        print("rn")

```

Arduino:

 ![1691051716714](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/d/0d7d1a1482cd493eb3f6fdfd09ec966133558533.jpeg)  
MicroPython:  
 ![1691051716723](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/c/2c467307925a68a9bee0839f6395a83d20444756.jpeg)

---

<div class="post-metadata">

**Author:** ![mlfrey](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@mlfrey](https://community.thunkable.com/u/mlfrey)\
**Post date:** [November 30, 2023, 2:09am UTC](https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372/2 "2023-11-30T02:09:36Z")

</div>

I have a similar problem and was wondering if you solved your problem yet?  
If not I may have an idea, so let me know.

---

<div class="post-metadata">

**Author:** ![jeremyrojas61h09ov](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@jeremyrojas61h09ov](https://community.thunkable.com/u/jeremyrojas61h09ov)\
**Post date:** [January 23, 2024, 11:53pm UTC](https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372/3 "2024-01-23T23:53:22Z")

</div>

I had the same problem. What worked for me is knowing that Thunktable doesn’t support Notify data, just Write data.

to send data from ESP32 to app I use this function:

def write(self, data, ntfy = False):  
self.\_ble.gatts\_write(self.\_tx\_handle, data)  
for conn\_handle in self.\_connections:  
self.\_ble.gatts\_notify(conn\_handle, self.\_tx\_handle)

try using gatts\_write instead of gatts\_notify.

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 1:33pm UTC](https://community.thunkable.com/t/ble-receive-arduino-ide-vs-micropython/2530372/4 "2024-11-08T13:33:35Z")

</div>


