初めてのBLE (3) 温度
「ペリフェラル」で使っているマイコン・ボードArduino Nano 33 BLE Senseには、次のようにいろいろなセンサが搭載されています。
- IMU LSM9DS1
- Microphone MP34DT05
- Gesture, light, proximity APDS9960
- Barometric pressure LPS22HB
- Temperature, humidity HTS221
前回、「ペリフェラル」で動かしたサンプル・スケッチBattery Monitorでは、アナログ・ポートA0の電圧を読んで、キャラbatterylevelのValueで送っていました。ここでは、HTS221から温度を取得し、batterylevelの代わりに温度のデータを送るように変更します。
●温湿度センサHTS221のおもなスペック
- 電源電圧 1.7~3.6V
- 温度測定確度 ±0.5°C(15~+40°C)
- 湿度測定確度 ±3.5%rH(20~+80%rH)
- 湿度および温度出力データ 16ビット
- インターフェース SPIおよびI²C
●HTS221のライブラリ
ライブラリ管理でHTS221を検索すると見つかります。インストールします。
サンプル・スケッチからReadSensorを選び、コンパイル、書き込み、実行します。
●BatteryMonitorに組み込む
スケッチBatteryMonitorに、ReadSensorのスケッチの主要部分を統合します。
コンパイル、実行します。
オン・セミコンダクターのBLEツールであるRSL10 Bluetooth Low Enaergy Exploerを使って情報を見ます。Valueは0x17=23℃が返っています。正しく温度が伝わったようです。
●BatteryMonitorをNANO33BLESense_Temperatureに修正
定義済みServiceのコードにBattery Serviceは0x180Fに割り当てられています。このbluetoothのService定義ページを見ても、周辺温度とかいうコードはないようなので、正しくはないですがそのまま0x18fを使います。Characteristicsは、このWebページではBattery Level(0x2A19)なので、0x2A1F(Temperature Celsius)のほうがよいかもしれません。
上記のスケッチの最低限必要な部分だけをまとめました。温度は10倍して、小数点第一位までを整数で送り出します。
#include <Arduino_HTS221.h>
#include <ArduinoBLE.h>
// BLE Battery Service
BLEService NANO33BLESense_Temperature("180f");
BLEUnsignedCharCharacteristic tempChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
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("NANO33BLESense_Temperature");
BLE.setAdvertisedService(NANO33BLESense_Temperature); // add the service UUID
NANO33BLESense_Temperature.addCharacteristic(tempChar); // add the battery level characteristic
BLE.addService(NANO33BLESense_Temperature); // Add the battery service
tempChar.writeValue(250); // 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());
while (central.connected()) {
delay(1000);
updateTemperature();
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
void updateTemperature() {
int temperature = 10.0 * HTS.readTemperature();
Serial.println("temperature*10 is " + String(temperature));
tempChar.writeValue(temperature); // update temperature characteristic
}
上記のペリフェラルのスケッチを動かし、RSL10 Bluetooth Low Enaergy Exploerの表示です。0xf5=245(24.5℃)が返ってきました。正常です。
RSL10 Bluetooth Low Enaergy Exploerは、次のように操作しました。
- ScanをON
- 見つけてきたNANO33BLESense_Temperatureを選び、Connectをクリック
- つながったら、Discover Servicesをクリック
- タブをinfoからServicesに変えて情報を見る

シリアルモニタの様子です。