0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

日記;回すだけⅥ ② C++でプログラミング <BLEセントラル>

Posted at

 前回、ジョイスティックの動きをBLEで送信するペリフェラルをCircuitPythonを使って作りました。
 ここではその値を受けるセントラル側を作ります。

マイコンはFeather nRF52840 Sense

 ペリフェラルで使ったFeather nRF52840でもよかったのですが、ほとんど同じマイコン・ボードFeather nRF52840 Senseを使います。いくつかのセンサが搭載されています。
 入手先の例 マルツ FEATHER NRF52840 SENSE【4516】
  STEAM Tokyoストア Feather nRF52840 Sense

IMG_4742.png

UART用セントラル

 うまく動きません。

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 ')

        while not uart_connection[UARTService].read(1)==b';':
            pass #uart_connection[UARTService].read(1)

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

 UARTが送っている文字列の終端(あるかどうかは不明)がわからないのです。12文字ずつ読むと、読み始めるところから12文字を取得します。いろいろ工夫してみたのですが、途中であきらめました。

実数を受けるセントラル

 前回と同じSensorServiceクラスのSwitchBot.pyです。

# SPDX-FileCopyrightText: 2020 Mark Raleson
# SPDX-License-Identifier: MIT
from adafruit_ble.uuid import VendorUUID
from adafruit_ble.services import Service
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.float import FloatCharacteristic

class SensorService(Service):

    uuid = VendorUUID("51ad213f-e568-4e35-84e4-67af89c79ef0")

    sensorsTheta = FloatCharacteristic(
        uuid=VendorUUID("e077bdec-f18b-4944-9e9e-8b3a815162b4"),
        properties=Characteristic.READ | Characteristic.NOTIFY,
    )

    sensorsVector = FloatCharacteristic(
        uuid=VendorUUID("528ff74b-fdb8-444c-9c64-3dd5da4135ae"),
        properties=Characteristic.READ | Characteristic.NOTIFY,
    )

    def __init__(self, service=None):
        super().__init__(service=service)
        self.connectable = True

 本体です。

# SPDX-FileCopyrightText: 2020 Mark Raleson
# SPDX-License-Identifier: MIT
from SwitchBot import SensorService
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
import time
import struct

ble = BLERadio()
ble.name ="Feather nRF52840 Express"
connection = None

while True:
    if not connection:
        print("Scanning")
        for adv in ble.start_scan(ProvideServicesAdvertisement):
            addr = adv.address
            s = ProvideServicesAdvertisement.matches
            #address = str(addr)[9:26]
            #print(address, adv)
            if SensorService in adv.services:
                connection = ble.connect(adv)
                print("Connected")
                break
            print(".")
        ble.stop_scan()
        print("stopped scan")
        if connection and connection.connected:
            service = connection[SensorService]
            while connection.connected:
                Theta = service.sensorsTheta
                Vector = service.sensorsVector
                print('Theta:  {:.1f}'.format(Theta))
                print('Vector: {:.2f}'.format(Vector))
                print("")
                time.sleep(1)

 前回の実数で送るプログラムをFeather nRF52840で動かしています。そのペリフェラルの送ってくるデータを表示します。
 実行中の様子です。デコードを特別にしなくても、実数に戻っています。送られてくる4バイトのデータは、通常メモリの内部に保存されているbinary32形式です。
f203.png

次は表示にチャレンジ

IMGP1453.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?