Replies: 2 comments
-
Here is @Allan196's code: from machine import Pin, Timer
from time import sleep_ms
import ubluetooth
from esp32 import raw_temperature
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()
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):
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)
message = buffer.decode('UTF-8')[:-1]
print(message)
if message == 'blue_led':
blue_led.value(not blue_led.value())
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)
# test
blue_led = Pin(2, Pin.OUT)
ble = BLE("ESP32")
while True:
pass |
Beta Was this translation helpful? Give feedback.
-
Following up on my initial post, I thought that the problem was why does the BLE device connect to the ESP32-WROOM and not the ESP32-ROVER. So with two seemingly the same ESP32-WROOM modules, why does one connect and the other doesn't? They are running the same MicroPython code and the same firmware (esp32-20220618-v1.19.1.bin). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I copied some Micropython code from https://forum.micropython.org/viewtopic.php?t=8645 to test BLE.
Code is attached as a txt file because .py files can't be attached!!.
main.txt
I firstly ran the code on a ESP32-WROOM (firmware esp32-20220618-v1.19.1.bin) and I was able to use a mobile phone to connect to the ESP32-WROOM.
I tried the same code on a ESP32-WROVER (firmware esp32spiram-20220618-v1.19.1.bin) and couldn't connect (the code ran with no exceptions or errors). I then changed the firmware on the ESP32-WROVER to esp32-20220618-v1.19.1.bin and that wouldn't connect either when I ran it.
I have used the ESP32-WROVER (firmware esp32spiram-20220618-v1.19.1.bin) successfully with WiFi.
The Espressif ESP32-WROVER datasheet states BLE support.
Any ideas?
Thanks
Allan
Beta Was this translation helpful? Give feedback.
All reactions