Nano 33 BLE Senseでマルチセンサ・ペリフェラルを作る (4) 気圧センサ LPS22HB
ここでは、気圧センサ LPS22HBを追加します。
● LPS22HBのおもなスペック
- 動作電圧 1.7~3.6V
- 測定範囲 260~1260hPa
- 気圧分解能 24ビット
- 温度分解能 16ビット
- インターフェース I2CとSPI
●ライブラリを導入
ライブラリ管理からLPS22HBを検索して見つかったArduino_LPS22HBをインストールします。
サンプル・スケッチReadPressureを読み込み、コンパイル、書き込み、実行します。温度は気圧の補正に使われ、気圧のみの出力です。
前回の温湿度センサHTS221で四つのファイルを作りました。それぞれ内容を更新します。Arduino IDEのタブの様子です。
inoファイルは同じディレクトリに入っています。
●センサの値を読み取る関数 nano33senseに追加
BARO.readPressure()の値はkPaの単位だったので10倍して、hPaにします。何度が実験しましたが、読み出した1回目の値はおかしいです。2度目からの読み取りは正常になります。
#include <Arduino_HTS221.h> #include <Arduino_LPS22HB.h> float HTS221_temperature; float HTS221_humidity; float LPS22HB_pressure; void readHTS221() { HTS221_temperature = HTS.readTemperature(); HTS221_humidity = HTS.readHumidity(); } void readLPS22HB() { LPS22HB_pressure = BARO.readPressure() * 10; // hpa }
●nano33senseServiceに追加
サービス名とサービスのUUIDを記述を追加しました。
#include <ArduinoBLE.h> #define HTS221_SERVICE_UUID "F000AA20-0451-4000-B000-000000000000" #define LPS22HB_SERVICE_UUID "F000AA40-0451-4000-B000-000000000000" // BLE Service BLEService Sensor_HTS221_Service(HTS221_SERVICE_UUID); BLEService Sensor_LPS22HB_Service(LPS22HB_SERVICE_UUID);
●nano33senseCharacteristicに追加
キャラ名とキャラのUUIDを記述を追加しました。前回、温度のコンストラクタをBLEUnsignedCharCharacteristicで宣言していましたが、零下が扱えないので、BLECharCharacteristicに変更しました。また、気圧は256を超える数値なので、16ビットのBLEUnsignedIntCharacteristicにしました。
#include <ArduinoBLE.h> #define HTS221_Temp_Characteristic_UUID "F000AA21-0451-4000-B000-000000000000" #define HTS221_Humi_Characteristic_UUID "F000AA22-0451-4000-B000-000000000000" #define LPS22HB_Press_Characteristic_UUID "F000AA41-0451-4000-B000-000000000000" // BLE Characteristic BLECharCharacteristic HTS221_Temp(HTS221_Temp_Characteristic_UUID, BLERead | BLENotify); BLEUnsignedCharCharacteristic HTS221_Humi(HTS221_Humi_Characteristic_UUID, BLERead | BLENotify); BLEUnsignedIntCharacteristic LPS22HB_Press(LPS22HB_Press_Characteristic_UUID, BLERead | BLENotify);
●メインnano33sense_Peripheral
localNAMEをHTS221_LPS22HBに変更しました。気圧センサ LPS22HBの最初のデータはおかしいので、初期化のあと1回、空読みをしています。
#include <ArduinoBLE.h> #define localNAME "HTS221_LPS22HB" 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); }
if (!BARO.begin()) {
Serial.println("Failed to initialize pressure sensor!");
while (1);
}
readLPS22HB(); // karayomi BLE.setLocalName(localNAME); BLE.setAdvertisedService(Sensor_HTS221_Service); // add the service UUID BLE.setAdvertisedService(Sensor_LPS22HB_Service); // add the service UUID Sensor_HTS221_Service.addCharacteristic(HTS221_Temp); // add characteristic Sensor_HTS221_Service.addCharacteristic(HTS221_Humi); // add characteristic Sensor_LPS22HB_Service.addCharacteristic(LPS22HB_Press); // add characteristic BLE.addService(Sensor_HTS221_Service); // Add service BLE.addService(Sensor_LPS22HB_Service); // Add service HTS221_Temp.writeValue(oldValue); // set initial value for this characteristic HTS221_Humi.writeValue(oldValue); // set initial value for this characteristic LPS22HB_Press.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(); readLPS22HB(); int valueof_HTS221_temperature = int(HTS221_temperature); int valueof_HTS221_humidity = int(HTS221_humidity); int valueof_LPS22HB_Pressure = int(LPS22HB_pressure); 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); Serial.print("LPS22HB_Pressure % is now: "); Serial.println(valueof_LPS22HB_Pressure); HTS221_Temp.writeValue(valueof_HTS221_temperature); // update characteristic HTS221_Humi.writeValue(valueof_HTS221_humidity); // update characteristic LPS22HB_Press.writeValue(valueof_LPS22HB_Pressure); // update characteristic oldValue = valueof_HTS221_temperature; // save the level for next comparison } }
実行結果です。
正しく送れていることがわかります。