CircuitPython 10行プログラミング Step4 (5) BLE②セントラル

 Adafruit Feather nRF52840 Senseボードを使って、前回、ペリフェラルのプログラムを実行しました。搭載されているセンサBMP280の温度と気圧を送ります。
 次のプログラムは、前回のプログラムにlocal nameを追加しました。

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
import board
import adafruit_bmp280
import time

ble = BLERadio()
ble.name ="Feather nRF52840 Express S"
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
i2c = board.I2C()
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

while True:
    ble.start_advertising(advertisement)
    while not ble.connected:
        pass
    while ble.connected:
        #print('connected ')
        #print(bmp280.temperature, bmp280.pressure)
        uart.write('{:.1f}`C, {:.0f}hPa'.format(bmp280.temperature, bmp280.pressure))
        time.sleep(1)

Adafruit Feather nRF52840でセントラル

 Adafruit Feather nRF52840 Senseボードとほぼ同じ構成のAdafruit Feather nRF52840ボードで、Senseボードの温度と気圧をBLE経由で読み取ります。

 ペリフェラルの動作をするAdafruit Feather nRF52840 Senseボードは、JSTコネクタにLi-Po電池を取り付け、近くで動かしておきます。約3m以内で、コネクトできました。

 オン・セミコンダクターのBLEモジュールとRSL10 Bluetooth Low Enaergy Exploerを使って情報を見ます。セントラルの立場です。

 情報を確認したら、Disconnectします。

 セントラルのプログラムを動かすAdafruit Feather nRF52840ボードを、PCとUSBケーブルでつなぎます。

プログラム

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
import time

ble = BLERadio()
ble.name ="Feather nRF52840 Express"
uart_connection = None
# See if any existing connections are providing UARTService.
if ble.connected:
    for connection in ble.connections:
        if UARTService in connection:
            uart_connection = connection
        break

while True:
    if not uart_connection:
        print("Scanning...")
        for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
            if UARTService in adv.services:
                print("found a UARTService advertisement")
                print(adv.address, adv.rssi)
                print(adv.complete_name, adv.tx_power)
                uart_connection = ble.connect(adv)
                print(uart_connection)
                break
        # Stop scanning whether or not we are connected.
        ble.stop_scan()

    while uart_connection and uart_connection.connected:
        print('connected ')

        data = uart_connection[UARTService].readline()
        print(data)
        #uart_connection.disconnect()
    time.sleep(1)

 実行中の画面です。正しくデータ(温度と気圧)を読んできた後、読めなくなったり、また読めたりと不安定な動作になりました。

 RSL10 Bluetooth Low Enaergy Exploerでは、ほぼずっと安定に読み出しています。ただ、時々、データが更新されないことがあります。

 したがって、次のような動作をしているように推測できます。

  • ペリフェラルのAdafruit Feather nRF52840 SenseボードのBLEはほぼ安定してデータを送っている
  • セントラルのAdafruit Feather nRF52840 ボードのBLEはあまり安定した動作をしていない

 ベータ版も最初のバージョンなので不具合が見つかっていないのか、プログラムが正しく記述できていないのか、原因ははっきりしません。