初めてのBLE (12) Raspberry Piでペリフェラル④Nano 33 BLE Senseをセントラルに
前回、ラズパイで、センサBME280の出力をBLE経由でアドバタイジングするペリフェラルを作りました。ここでは、Arduino Nano 33 BLE Senseにセントラルの機能をもたせてアクセスします。
●scan
サンプル・スケッチのscanをNano 33 BLE Senseに入れて実行します。Local Nameは[RPi]を登録していたのですが、今日は、変更前の名前が出ています。AddressもAA:AA:AA:AA:AA:AAと異常な値です。最初のラズパイを動かしたときは、DC:...でしたが、bluetoothctlを使った後に、このアドレスになりました。
●PeripheralExplorer
第7回で動かしたPeripheralExplorerのLocal NameをBCM4345C0に変更して実行します。ラズパイで公開しているものを全部読み出したようです。
●BME280の値だけの出力
UUIDが12345678で始まるのだけに絞って表示します。
/* Peripheral Explorer This example scans for BLE peripherals until one with a particular name ("LED") is found. Then connects, and discovers + prints all the peripheral's attributes. The circuit: - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. You can use it with another board that is compatible with this library and the Peripherals -> LED example. This example code is in the public domain. */ #include <ArduinoBLE.h> void setup() { Serial.begin(9600); while (!Serial); // begin initialization if (!BLE.begin()) { Serial.println("starting BLE failed!"); while (1); } Serial.println("start BLE Central - Peripheral Explorer"); // start scanning for peripherals BLE.scan(); } int i=0; void loop() { // check if a peripheral has been discovered BLEDevice peripheral = BLE.available(); delay(2000); if (peripheral) { // discovered a peripheral, print out address, local name, and advertised service Serial.print("\nFound "); Serial.print(peripheral.address()); Serial.print(" '"); Serial.print(peripheral.localName()); Serial.print("' "); // Serial.print(peripheral.advertisedServiceUuid()); Serial.println(); // see if peripheral is a LED if (peripheral.localName() == "BCM4345C0") { // stop scanning BLE.stopScan(); // Serial.print("Service "); // Serial.println(service.uuid()); // explorerPeripheral(peripheral); // peripheral disconnected, we are done while (1) { // do nothing } } } delay(100);BLE.scan(); // i = i +1;Serial.print(i); } void explorerPeripheral(BLEDevice peripheral) { // connect to the peripheral Serial.print("Connecting ..."); if (peripheral.connect()) { Serial.println(" Connected"); } else { Serial.println("Failed to connect!"); return; } // discover peripheral attributes Serial.println("Discovering attributes ..."); if (peripheral.discoverAttributes()) { // Serial.println("Attributes discovered"); } else { Serial.println("Attribute discovery failed!"); peripheral.disconnect(); return; } // read and print device name of peripheral Serial.println(); Serial.print("Device name: "); Serial.println(peripheral.deviceName()); // Serial.print("Appearance: 0x"); // Serial.println(peripheral.appearance(), HEX); Serial.println(); // loop the services of the peripheral and explore each for (int i = 0; i < peripheral.serviceCount(); i++) { BLEService service = peripheral.service(i); exploreService(service); } Serial.println(); // we are done exploring, disconnect Serial.print("\nDisconnecting ..."); peripheral.disconnect(); Serial.println("Disconnected"); } void exploreService(BLEService service) { // print the UUID of the service // Serial.print("Service "); // Serial.println(service.uuid()); // loop the characteristics of the service and explore each for (int i = 0; i < service.characteristicCount(); i++) { BLECharacteristic characteristic = service.characteristic(i); exploreCharacteristic(characteristic); } } void exploreCharacteristic(BLECharacteristic characteristic) { // print the UUID and properties of the characteristic //Serial.print((String(characteristic.uuid())).substring(0,7)); if ((String(characteristic.uuid())).substring(0,8) == "12345678") { Serial.print("\nCharacteristic "); Serial.print(characteristic.uuid()); // Serial.print(", properties 0x"); // Serial.print(characteristic.properties(), HEX); // check if the characteristic is readable if (characteristic.canRead()) { // read the characteristic value characteristic.read(); if (characteristic.valueLength() > 0) { // print out the value of the characteristic Serial.print(", value "); printData(characteristic.value(), characteristic.valueLength()); } } } // Serial.println(); // loop the descriptors of the characteristic and explore each for (int i = 0; i < characteristic.descriptorCount(); i++) { BLEDescriptor descriptor = characteristic.descriptor(i); exploreDescriptor(descriptor); } } void exploreDescriptor(BLEDescriptor descriptor) { // print the UUID of the descriptor // Serial.print("\t\tDescriptor "); // Serial.print(descriptor.uuid()); // read the descriptor value descriptor.read(); // print out the value of the descriptor // Serial.print(", value"); printData(descriptor.value(), descriptor.valueLength()); Serial.println(); } void printData(const unsigned char data[], int length) { for (int i = 0; i < length; i++) { unsigned char b = data[i]; if (b < 16) { Serial.print("0"); } Serial.print(b); } }
実行結果です。
第9回の後半で調べたように、connectしてそのままにしておくと約30秒でdisconnectされます。