Page 1 of 1

Bluetooth BLE tests

Posted: Sun Oct 06, 2019 11:05 am
by devnull
Just thought I would post this test code on the newly merged bluetooth, many thanks to @jimmo for making this happen and for the sample code from which this was created.

1. import the ble module below
2. on you phone (iphone) settings > bluetooth and connect PYBD device (may be advertised as ESP32)
3. On the iphone, open the Health app.
4. Send a new value to the iphone HEART RATE: ble.send(88)

If anybody else has any useful code snippets for BLE, would be interested to see / test it !

ble.py

Code: Select all

import bluetooth
bt = bluetooth.BLE()
bt.active(True)

UUID_HR = const(0x2A37)

_IRQ_ALL = const(0xffff)
HR_SERVICE = bluetooth.UUID(0x180D)
HR_CHAR = (bluetooth.UUID(UUID_HR), bluetooth.FLAG_READ|bluetooth.FLAG_NOTIFY,)
CID = None

((HR,),) = bt.gatts_register_services(((HR_SERVICE, (HR_CHAR,),),))

def bt_irq(event, data):
  # >> bt irq 1 (64, 1, b'Y\xeb\n@uZ')
  global CID
  CID,EID,DATA = data
  print('connected > evt:{} id:{} eid:{} data:{}'.format(event,CID,EID,DATA))


bt.irq(bt_irq, _IRQ_ALL)

def adv_encode(adv_type, value):
  return bytes((len(value) + 1, adv_type,)) + value

def adv_encode_name(name):
  return adv_encode(0x09, name.encode())

def adv():
  bt.gap_advertise(100, adv_encode(0x01, b'\x06') + adv_encode(0x03, b'\x0d\x18') + adv_encode(0x19, b'\xc1\x03') + adv_encode_name('esp32hr'))

def send(val=0):
  # b'\x00\x42'
  ba = bytearray(2)
  ba[1]=val
  bt.gatts_notify(64, HR, ba)

adv()

BLE n00b question

Posted: Sun Oct 06, 2019 3:03 pm
by pythoncoder
Is it possible to use BLE as a communication channel between Pyboard D's?

Re: BLE n00b question

Posted: Sun Oct 06, 2019 3:53 pm
by jimmo
pythoncoder wrote:
Sun Oct 06, 2019 3:03 pm
Is it possible to use BLE as a communication channel between Pyboard D's?
Yes. Right now you do that by having one of them acting as a peripheral, the other as a central and then they can exchange data over a characteristic value.

There's an abstraction built on top of this called the Nordic Uart Service (NUS) which make it look like a UART.

We're also keen to investigate implementing BLE Mesh.

Re: Bluetooth BLE tests

Posted: Mon Oct 07, 2019 4:55 am
by devnull
@jimmo - Do you have a code snippet for this ??
Yes. Right now you do that by having one of them acting as a peripheral, the other as a central and then they can exchange data over a characteristic value.
Also, I already reported this on github and is a small matter, but the pyboard D seems to advertise itself as a ESP32 device prior to initial pairing / connection.

Re: Bluetooth BLE tests

Posted: Mon Oct 07, 2019 11:05 pm
by jimmo
devnull wrote:
Mon Oct 07, 2019 4:55 am
@jimmo - Do you have a code snippet for this ??
Yeah working on documentation and code samples this week.
devnull wrote:
Mon Oct 07, 2019 4:55 am
Also, I already reported this on github and is a small matter, but the pyboard D seems to advertise itself as a ESP32 device prior to initial pairing / connection.
I'll reply on github in more detail, but are you using the exact same code snippet above. The advertising payload is includes adv_encode_name('esp32hr'), so I'd expect you see "esp32hr" in a scan?

There's no pairing support yet, but on connection you see the device's actual name. On STM32 it defaults to "PYBD", on ESP32 the plan is to make it default to "ESP32". Perhaps this should be "MPY" (or maybe it should be configurable).

Re: Bluetooth BLE tests

Posted: Thu Oct 17, 2019 9:33 am
by devnull
@jimmo - I have just received some BLE 5.0 beacons from Alibaba.

Do you have any example code that shows how I configure as a a Master, scan for and connect to a BLE slave ??

I know you mentioned you were working on some documentation ?

Thanks

PeterC