Nano 33 BLE Senseでマルチセンサ・ペリフェラルを作る (3) 搭載センサHTS221②
前回、BatteryServiceのサンプルをベースに、温湿度センサHTS221の測定した温度と湿度を整数で送るペリフェラル・スケッチを作りました。あちこちにBatteryという名称が残っているので、Nano33BLEに即した名称に変えていきます。
また、連載の第1回でも解説したようにGATTサーバは、複数のServiceとその中に複数のキャラを含んでいます。現在、デバイスはHTS221だけです。しかし、複数のセンサ・デバイスに対応していくとすれば、次のような構成が考えられます。
- センサ・デバイスを一つのサービスとする
- センサが測定できる各種データにそれぞれキャラを割り当てる
- センサの値を読み取る関数
- メイン
現在、
- メイン nano33sense_Peripheral
- センサの値を読み取る関数 nano33sense
の二つを作りました。
最初にサービスを記述したスケッチを作ります。最終的にできた四つのスケッチは、タブで並んだ状態です。本当は一つのスケッチでよいのですが、見通しが悪いので分割しました。通常分割したときは、別のプログラムを呼ぶときにinclude文が必要ですが、Arduino IDEではそれぞれがinoファイルであれば不要です。
次回からセンサを追加していくときは、これら四つに分けたファイルにそれぞれ必要な記述を追加します。
●nano33senseService
新規タブでファイル名をnano33senseServiceにしました。Serviceを記述するスケッチです。中には、サービス名とサービスのUUIDを記述しました。
#include <ArduinoBLE.h> #define HTS221_SERVICE_UUID "F000AA20-0451-4000-B000-000000000000" // BLE Service BLEService Sensor_HTS221_Service(HTS221_SERVICE_UUID);
●nano33senseCharacteristic
新規タブでファイル名をnano33senseCharacteristicにしました。Characteristic(キャラ)を記述するファイルです。中には、キャラ名とキャラUUIDを記述しました。
#include <ArduinoBLE.h> #define HTS221_Temp_Characteristic_UUID "F000AA21-0451-4000-B000-000000000000" #define HTS221_Humi_Characteristic_UUID "F000AA22-0451-4000-B000-000000000000" // BLE Characteristic BLEUnsignedCharCharacteristic HTS221_Temp(HTS221_Temp_Characteristic_UUID, BLERead | BLENotify); BLEUnsignedCharCharacteristic HTS221_Humi(HTS221_Humi_Characteristic_UUID, BLERead | BLENotify);
●メイン nano33sense_Peripheral
Local NameをSensor_HTS221にしました。セントラルからscanして見える名前です。ベースとなったバッテリ・サービスで使われていた用語は取り除きました。
#include <ArduinoBLE.h> #define localNAME "Sensor_HTS221" int oldValue = 0; // last value long previousMillis = 0; // last time value was checked, in ms void setup() { Serial.begin(9600); // initialize serial communication while (!Serial); // begin initialization if (!BLE.begin()) { Serial.println("starting BLE failed!"); while (1); } if (!HTS.begin()) { Serial.println("Failed to initialize humidity temperature sensor!"); while (1); } BLE.setLocalName(localNAME); BLE.setAdvertisedService(Sensor_HTS221_Service); // add the service UUID Sensor_HTS221_Service.addCharacteristic(HTS221_Temp); // add characteristic Sensor_HTS221_Service.addCharacteristic(HTS221_Humi); // add characteristic BLE.addService(Sensor_HTS221_Service); // Add service HTS221_Temp.writeValue(oldValue); // set initial value for this characteristic HTS221_Humi.writeValue(oldValue); // set initial value for this characteristic // start advertising BLE.advertise(); Serial.println("Bluetooth device active, waiting for connections..."); } void loop() { // wait for a BLE central BLEDevice central = BLE.central(); // if a central is connected to the peripheral: if (central) { Serial.print("Connected to central: "); // print the central's BT address: Serial.println(central.address()); // check the battery level every 200ms // while the central is connected: while (central.connected()) { long currentMillis = millis(); // if 200ms have passed, check value: if (currentMillis - previousMillis >= 200) { previousMillis = currentMillis; updateValue(); } } // when the central disconnects Serial.print("Disconnected from central: "); Serial.println(central.address()); } } void updateValue() { readHTS221(); int valueof_HTS221_temperature = int(HTS221_temperature); int valueof_HTS221_humidity = int(HTS221_humidity); if (valueof_HTS221_temperature != oldValue) { // if value has changed Serial.print("HTS221_temperature % is now: "); Serial.println(valueof_HTS221_temperature); Serial.print("HTS221_humidity % is now: "); Serial.println(valueof_HTS221_humidity); HTS221_Temp.writeValue(valueof_HTS221_temperature); // update characteristic HTS221_Humi.writeValue(valueof_HTS221_humidity); // update characteristic oldValue = valueof_HTS221_temperature; // save the level for next comparison } }
●センサの値を読み取る関数 nano33sense
前回から変更ありません。
#include <Arduino_HTS221.h> float HTS221_temperature; float HTS221_humidity; void readHTS221() { HTS221_temperature = HTS.readTemperature(); HTS221_humidity = HTS.readHumidity(); }
複数の#include <ArduinoBLE.h>がありますが、重複しているというようなエラーは出ません。
実行中の様子です。
RSL10 Bluetooth Low Enaergy Exploerの画面です。Notificationにチェックを入れるとエラーになっています。スケッチでは200msごとに温度をチェックし、値が変わっていたら、シリルモニタに最新値を表示します。しかし、セントラル側には通知が行っていません。