Smart gadget SHT4x を使って電子百葉箱を作る ③ Arduino MKR WiFi 1010
マイコン・ボードArduino MKR WiFi 1010は、RF部にu-blox NINA-W102を搭載していて、Wi-FiとBLEが使えます。ボードは、秋月電子通商、スイッチサイエンスなどで購入できます。
BLEはArduino BLEライブラリを使ってスケッチを記述します。古めのArduino MKR WiFi 1010のファームウェアは、新しくしておく必要があります。
●スケッチ
Smart gadget SHT4xに接続し、温度と湿度を読み出す前回のスケッチのT_Characteristic_UUIDとH_Characteristic_UUIDが逆になっていたのを修正しました。
#include <ArduinoBLE.h>
#define localNAME "SHT40 Gadget"
#define T_Characteristic_UUID "00002235-b38d-4985-720e-0f993a68ee41"
#define H_Characteristic_UUID "00001235-b38d-4985-720e-0f993a68ee41"
void setup() {
Serial.begin(115200);
while (!Serial);
BLE.begin();
Serial.println("\n start BLE Central - Temperature/Humidity");
// start scanning for peripherals
BLE.scanForName(localNAME);
}
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != localNAME) {
return;
}
// stop scanning
BLE.stopScan();
readData(peripheral);
// peripheral disconnected, start scanning again
BLE.scanForName(localNAME);
}
}
void readData(BLEDevice peripheral) {
// connect to the peripheral
Serial.print("\nConnecting ...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// discover peripheral attributes
Serial.print("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered\n");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// retrieve the characteristic
BLECharacteristic Temperature_Characteristic = peripheral.characteristic(T_Characteristic_UUID);
BLECharacteristic Humidity_Characteristic = peripheral.characteristic(H_Characteristic_UUID);
if (!Temperature_Characteristic) {
Serial.println("Peripheral does not have Temp characteristic!");
peripheral.disconnect();
return;
}
Serial.println("---Found CHARACTERISTIC_UUID");
while (peripheral.connected()) {
if (Temperature_Characteristic.canRead()) {
// read the characteristic value
Temperature_Characteristic.read();
if (Temperature_Characteristic.valueLength() > 0) {
uint32_t data = ((uint8_t)*(Temperature_Characteristic.value()+3) << 24) + ((uint8_t)*(Temperature_Characteristic.value()+2) << 16) + ((uint8_t)*(Temperature_Characteristic.value()+1) << 8) + ((uint8_t)*Temperature_Characteristic.value());
int32_t f = pow(-1, int(bitRead(data,31)));
double k =1 + ((((data<<1)<<8)>>9))/pow(2,23);
int32_t s = pow(2,((((data<<1)>>24))-127));
Serial.print("\ntemp = ");
Serial.println(f*k*s);
Humidity_Characteristic.read();
data = ((uint8_t)*(Humidity_Characteristic.value()+3) << 24) + ((uint8_t)*(Humidity_Characteristic.value()+2) << 16) + ((uint8_t)*(Humidity_Characteristic.value()+1) << 8) + ((uint8_t)*Humidity_Characteristic.value());
f = pow(-1, int(bitRead(data,31)));
k =1 + ((((data<<1)<<8)>>9))/pow(2,23);
s = pow(2,((((data<<1)>>24))-127));
Serial.print("humi = ");
Serial.println(f*k*s);
delay(30000);
}
}
}
Serial.println("Peripheral disconnected");
}
シリアルモニタの様子です。