Nano 33 BLE Senseをデータ入力に使う<BLE>⑦気圧センサ<BLEペリフェラル>
ここまで、複数搭載されているセンサのうち、
を扱いました。ここでは気圧センサ LPS22HBを使います。
●LPS22HBのおもなスペック
- 動作電圧 1.7~3.6V
- 測定範囲 260~1260hPa
- 気圧分解能 24ビット
- 温度分解能 16ビット
- インターフェース I2C(最大400kHz)とSPI
●接続
USBケーブルで、PCと接続します。
●ライブラリの導入
ライブラリを管理からArduino_LPS22HBを検索して見つかったArduino_LPS22HBをインストールします。
●サンプルReadPressure
実行します。
●スケッチ
BLEペリフェラルのスケッチを追加します。
#include <Arduino_LPS22HB.h>
#include <ArduinoBLE.h>
// LPS22HB
// BLE Service
#define Nano33BLESense_Service4_UUID "F000AB00-0451-4000-B000-000000000000"
BLEService Nano33BLESense_Service4(Nano33BLESense_Service4_UUID);
// BLE Characteristic
#define LPS22HB_Press_Characteristic_UUID "F000AB01-0451-4000-B000-000000000000"
BLEFloatCharacteristic LPS22HB_Press(LPS22HB_Press_Characteristic_UUID, BLERead | BLENotify);
// BLE Descriptor
#define LPS22HB_Press_Descriptor_UUID "2901"
BLEDescriptor LPS22HB_Press_Descriptor(LPS22HB_Press_Descriptor_UUID, "Pressure hPa");
#define localNAME "LPS22HB"
#define DeviceNAME "LPS22HBBLE"
float previousMillis = 0; // last time value was checked, in ms
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("\n LPS22HB test");
if (!BARO.begin()) {
Serial.println("Failed to initialize pressure sensor!");
while (1);
}
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName(localNAME);
BLE.setDeviceName(DeviceNAME);
//// set the service4
BLE.setAdvertisedService(Nano33BLESense_Service4);
// add characteristic
Nano33BLESense_Service4.addCharacteristic(LPS22HB_Press);
// add descriptor
LPS22HB_Press.addDescriptor(LPS22HB_Press_Descriptor);
// add service
BLE.addService(Nano33BLESense_Service4);
// start advertising
BLE.advertise();
Serial.println("\nBluetooth 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) {
delay(100);
Serial.print("\n Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
// check data 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();
delay(1000);
}
}
// when the central disconnects
Serial.print("Disconnected from central: ");
Serial.println(central.address());
goto brout;
}
brout: ;
}
void updateValue() {
// read the sensor value
float pressure = BARO.readPressure() * 10.0;
// print the sensor value
Serial.print("Pressure = "); Serial.print(pressure);Serial.println(" hPa");
// update characteristic
LPS22HB_Press.writeValue(pressure);
}
RLS10のBLEセントラルの画面です。実数はIEEE 754 binary32のフォーマットで送られてきます。
最初、updateValue() の最後にdelay(2000);を入れていました。RLS10は何もエラーを出さずに接続できましたが、matlabは接続に失敗しました。