2019年12月19日木曜日

Raspberry PiからBluetooth経由でmicrobitに接続したGrove水分センサーの値を取得する

Groveシールドで様々なデバイスを利用するすることができます。
以下の手順でmicrobitのP0に接続したGrove水分センサーの値をBluetooth経由でRaspberry Pi上のPythonプログラムで取得する事ができます。

1. Raspberry Piにbluepyをインストール
bluepyをRaspberry Piにインストールするの手順で、bluepyをインストールします

2. micro:bitでbluetoothサービスを使用するプログラム作成
以下のような感じで入出力端子bluetoothサービスを最初に追加します
また、ペアリングをしなくても利用できるようにします
※6桁キーでペアリングしたい場合は「6桁のキーでmicrobitとRaspberry PiをBluetoothペアリングする」を参照してください。

3. Grove水分センサーの値を取得するプログラム

iopin_moisture_sensor.py
from bluepy import btle

per = btle.Peripheral("XX:XX:XX:XX:XX:XX", btle.ADDR_TYPE_RANDOM)

class MyDelegate(btle.DefaultDelegate):
  def __init__(self):
    btle.DefaultDelegate.__init__(self)

  def handleNotification(self, hd, data):
    print("Pin{}={}".format(str(data[0]), str(data[1])))

# IO Pin Service
svcIOPIN = per.getServiceByUUID("E95D127B-251D-470A-A062-FA1922DFA9A8")
# pin0 as Angle
chADC = svcIOPIN.getCharacteristics("E95D5899-251D-470A-A062-FA1922DFA9A8")[0]
chADC.write(b"\x01\x00\x00\x00")
# pin0 as Input
chPIOC = svcIOPIN.getCharacteristics("E95DB9FE-251D-470A-A062-FA1922DFA9A8")[0]
chPIOC.write(b"\x01\x00\x00\x00")
# Pin Data
chDATA = svcIOPIN.getCharacteristics("E95D8D00-251D-470A-A062-FA1922DFA9A8")[0]
ch_cccd=chDATA.getDescriptors(forUUID=0x2902)[0]
ch_cccd.write(b"\x01\x00", False)

per.setDelegate(MyDelegate() )

while True:
  if per.waitForNotifications(0.1):
    continue

4. Grove水分センサーの値を取得するプログラムの実行
python3 iopin_moisture_sensor.py
Pin0=11
Pin0=10
Pin0=12
Pin0=8

〇GroveシールドとGrove水分センサーを接続したmicrobit

関連情報

Groveデバイスまとめ

Bluetooth Developer Studio Level 3 Profile Report


0 件のコメント:

コメントを投稿