ESP32_BLE - How to get message

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
rnemeth
Posts: 1
Joined: Tue Jun 21, 2022 12:49 pm

ESP32_BLE - How to get message

Post by rnemeth » Wed Jun 22, 2022 8:33 pm

Hello,

could you please help me get the message in main.py? I would like to further use the message I get via ble.

Thank you in advance!

main.py

Code: Select all

from machine import Pin, Timer
from esp32_ble import BLE

ble = BLE("ESP32")
print(ble.get_message())
esp32_ble.py

Code: Select all

from machine import Pin, Timer
from time import sleep_ms
import ubluetooth

class BLE():
    def __init__(self, name):   
        self.name = name
        self.ble = ubluetooth.BLE()
        self.ble.active(True)

        self.led = Pin(2, Pin.OUT)
        self.timer1 = Timer(0)
        self.timer2 = Timer(1)
        
        self.disconnected()
        self.ble.irq(self.ble_irq)
        self.register()
        self.advertiser()
        
        self.message = ""

    def connected(self):        
        self.timer1.deinit()
        self.timer2.deinit()

    def disconnected(self):        
        self.timer1.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(1))
        sleep_ms(200)
        self.timer2.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(0))   

    def ble_irq(self, event, data):
        #global message
        
        if event == 1:
            '''Central disconnected'''
            self.connected()
            self.led(1)
        
        elif event == 2:
            '''Central disconnected'''
            self.advertiser()
            self.disconnected()
        
        elif event == 3:
            '''New message received'''            
            buffer = self.ble.gatts_read(self.rx)
            self.message = buffer.decode('UTF-8').strip()
            
    def get_message(self):
        return self.message
           
    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'
            
        BLE_NUS = ubluetooth.UUID(NUS_UUID)
        BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
        BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY)
            
        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 + '\n')

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


User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32_BLE - How to get message

Post by jimmo » Fri Jun 24, 2022 12:37 am

rnemeth wrote:
Wed Jun 22, 2022 8:33 pm
could you please help me get the message in main.py? I would like to further use the message I get via ble.
This code:

Code: Select all

ble = BLE("ESP32")
print(ble.get_message())
won't work because it will just run immediately. You need to wait until there is a message before you can call get_message.

I highly recommend taking a look at the aioble library for working with BLE https://github.com/micropython/micropyt ... oth/aioble

In your case it will mean you don't have to write any of the event/irq handling, and you can use `await` to wait for the message to arrive.

Post Reply