Nano 33 BLE Senseでマルチセンサ・ペリフェラルを作る (5) 近接センサ APDS9960
近接センサ APDS9960を追加します。光センサを搭載していて、ジェスチャ機能などを実装できるようです。
●APDS9960のおもなスペック
- 動作電圧 2.4~3.6V
- 消費電流 待機時38uA、動作時790uA
- 光による近接接近検知(Proximity detection)
- ジェスチャ検知( Gesture detection)
- 環境光感知(Ambient Light Sense)
- 色感知(Color Sense (RGBC))
- インターフェース I2C
●APDS9960のライブラリ
ライブラリ管理でAPDS9960を検索して出てきたArduino_APDS9960をインストールします。
サンプルのFullExampleを読み込んで動かします。PRはセンサからの距離です。0~255の値を取ります。r、g、bはRed、Green、Blueの光強度です。0~255の値を取ると思われます。
サンプル・スケッチGestureSensorを単独で動かしました。
USBコネクタからRFモジュール側へ指を動かすとUP、逆はDownと表示が出ます。左右も検知し、戻り値は0~3です。
前回までに、四つのファイルを作りました。それぞれ内容を更新します。
●センサの値を読み取る関数 nano33senseに追加
それぞれの値を読み取る途中にdelay(100);を入れていますが、不要かもしれません。
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_APDS9960.h>
float HTS221_temperature;
float HTS221_humidity;
float LPS22HB_pressure;
uint8_t APDS9960_proximity = 0;
int APDS9960_gesture = 0;
int r = 0, g = 0, b = 0;
int APDS9960_color_Red = 0, APDS9960_color_Green = 0, APDS9960_color_Blue = 0;
void readHTS221() {
HTS221_temperature = HTS.readTemperature();
HTS221_humidity = HTS.readHumidity();
}
void readLPS22HB() {
LPS22HB_pressure = BARO.readPressure() * 10; // hpa
}
void readAPDS9960() {
if (APDS.proximityAvailable()) {
APDS9960_proximity = APDS.readProximity(); // -0 => close, -255 => far, - -1 => error
}
delay(100);
if (APDS.gestureAvailable()) {
APDS9960_gesture = APDS.readGesture(); // GESTURE_UP, GESTURE_DOWN, GESTURE_LEFT, GESTURE_RIGHT
}
delay(100);
if (APDS.colorAvailable()) {
APDS.readColor(r, g, b);
}
APDS9960_color_Red = r; APDS9960_color_Green = g; APDS9960_color_Blue = b;
delay(100);
}
●nano33senseServiceに追加
サービス名とサービスのUUIDを記述を追加しました。
#include <ArduinoBLE.h> #define HTS221_SERVICE_UUID "F000AA20-0451-4000-B000-000000000000" #define LPS22HB_SERVICE_UUID "F000AA40-0451-4000-B000-000000000000" #define APDS9960_SERVICE_UUID "F000AA00-0451-4000-B000-000000000000" // BLE Service BLEService Sensor_HTS221_Service(HTS221_SERVICE_UUID); BLEService Sensor_LPS22HB_Service(LPS22HB_SERVICE_UUID); BLEService Sensor_APDS9960_Service(APDS9960_SERVICE_UUID);
●nano33senseCharacteristicに追加
キャラ名とキャラのUUIDを記述を追加しました。notifyがうまく動いていないので、プロパティはreadのみにしました。
#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"
#define APDS9960_Proximity_Characteristic_UUID "F000AA01-0451-4000-B000-000000000000"
#define APDS9960_Gesture_Characteristic_UUID "F000AA02-0451-4000-B000-000000000000"
#define APDS9960_Color_Red_Characteristic_UUID "F000AA03-0451-4000-B000-000000000000"
#define APDS9960_Color_Blue_Characteristic_UUID "F000AA04-0451-4000-B000-000000000000"
#define APDS9960_Color_Green_Characteristic_UUID "F000AA05-0451-4000-B000-000000000000"
// BLE Characteristic
BLEUnsignedCharCharacteristic 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);
BLEUnsignedCharCharacteristic APDS9960_Proximity(APDS9960_Proximity_Characteristic_UUID,
BLERead);// | BLENotify);
BLEUnsignedCharCharacteristic APDS9960_Gesture(APDS9960_Gesture_Characteristic_UUID,
BLERead);// | BLENotify);
BLEUnsignedCharCharacteristic APDS9960_Color_Red(APDS9960_Color_Red_Characteristic_UUID,
BLERead);// | BLENotify);
BLEUnsignedCharCharacteristic APDS9960_Color_Blue(APDS9960_Color_Blue_Characteristic_UUID,
BLERead);// | BLENotify);
BLEUnsignedCharCharacteristic APDS9960_Color_Green(APDS9960_Color_Green_Characteristic_UUID,
BLERead);// | BLENotify);
●メインnano33sense_Peripheral
localNAMEをHTS221_LPS22HB_APDS9960に変更しました。
setup()内のAPDS.begin()で初期化を行った後に一度空読みをしていますが、それでも、光強度は正しく読めないようです。2回目からは正常に読み取ってきます。
前回まで、データの更新は温度が変化したときでしたが、レスポンスが悪いので、温度と湿度の合計値を変数にしました。このほうが、マイコン・ボードを手で触ったときにしっかりデータが更新されます。
#include <ArduinoBLE.h>
#define localNAME "HTS221_LPS22HB_APDS9960"
#define DeviceNAME "nano33BLE"
float 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
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor.");
while (true); // Stop forever
}
readAPDS9960(); // karayomi
BLE.setLocalName(localNAME);
BLE.setDeviceName(DeviceNAME);
// add the service UUID
BLE.setAdvertisedService(Sensor_HTS221_Service);
BLE.setAdvertisedService(Sensor_LPS22HB_Service);
BLE.setAdvertisedService(Sensor_APDS9960_Service);
// add characteristic
Sensor_HTS221_Service.addCharacteristic(HTS221_Temp);
Sensor_HTS221_Service.addCharacteristic(HTS221_Humi);
Sensor_LPS22HB_Service.addCharacteristic(LPS22HB_Press);
Sensor_APDS9960_Service.addCharacteristic(APDS9960_Proximity);
Sensor_APDS9960_Service.addCharacteristic(APDS9960_Gesture);
Sensor_APDS9960_Service.addCharacteristic(APDS9960_Color_Red);
Sensor_APDS9960_Service.addCharacteristic(APDS9960_Color_Blue);
Sensor_APDS9960_Service.addCharacteristic(APDS9960_Color_Green);
// Add service
BLE.addService(Sensor_HTS221_Service);
BLE.addService(Sensor_LPS22HB_Service);
BLE.addService(Sensor_APDS9960_Service);
// set initial value for this characteristic
HTS221_Temp.writeValue(oldValue);
HTS221_Humi.writeValue(oldValue);
LPS22HB_Press.writeValue(oldValue);
APDS9960_Proximity.writeValue(oldValue);
APDS9960_Gesture.writeValue(oldValue);
APDS9960_Color_Red.writeValue(oldValue);
APDS9960_Color_Blue.writeValue(oldValue);
APDS9960_Color_Green.writeValue(oldValue);
// 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) {
delay(100);
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();
delay(1000);
}
}
// 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);
readLPS22HB();
int valueof_LPS22HB_pressure = int(LPS22HB_pressure);
readAPDS9960();
uint8_t valueof_APDS9960_proximity = int(APDS9960_proximity);
uint8_t valueof_APDS9960_gesture = int(APDS9960_gesture);
uint8_t valueof_APDS9960_color_Red = int(APDS9960_color_Red);
uint8_t valueof_APDS9960_color_Green = int(APDS9960_color_Green);
uint8_t valueof_APDS9960_color_Blue = int(APDS9960_color_Blue);
// if value has changed
if ((valueof_HTS221_temperature+valueof_HTS221_humidity) != oldValue) {
Serial.print("- - = - -\nHTS221_temperature % is now: ");
Serial.println(valueof_HTS221_temperature);
Serial.print("HTS221_humidity % is now: ");
Serial.println(valueof_HTS221_humidity);
Serial.print("---\nLPS22HB_Pressure % is now: ");
Serial.println(valueof_LPS22HB_pressure);
Serial.print("---\nAPDS9960_proximity % is now: ");
Serial.println(valueof_APDS9960_proximity);
Serial.print("APDS9960_gesture % is now: ");
Serial.println(valueof_APDS9960_gesture);
Serial.print("APDS9960_color_Red % is now: ");
Serial.println(valueof_APDS9960_color_Red);
Serial.print("APDS9960_color_Green % is now: ");
Serial.println(valueof_APDS9960_color_Green);
Serial.print("APDS9960_color_Blue % is now: ");
Serial.println(valueof_APDS9960_color_Blue);
// update characteristic
HTS221_Temp.writeValue(valueof_HTS221_temperature);
HTS221_Humi.writeValue(valueof_HTS221_humidity);
LPS22HB_Press.writeValue(valueof_LPS22HB_pressure);
APDS9960_Proximity.writeValue(valueof_APDS9960_proximity);
APDS9960_Gesture.writeValue(valueof_APDS9960_gesture);
APDS9960_Color_Red.writeValue(valueof_APDS9960_color_Red);
APDS9960_Color_Blue.writeValue(valueof_APDS9960_color_Green);
APDS9960_Color_Green.writeValue(valueof_APDS9960_color_Blue);
oldValue = valueof_HTS221_temperature+valueof_HTS221_humidity; // save the level for next comparison
}
}
実行している様子です。
コネクトした後、Discover Servicesをクリックしても、前回のようにすぐにデータを送ってきません。10秒以上たってからデータが送られてきます。