Nicla Sense MEプラスArduino IDEでBLEペリフェラル＜その2＞

　前回のセンサのデータをBLEで飛ばします。

###読みだすデータ

- 温度　temp
- 湿度　hum
- 気圧　baro
- 加速度　accel
- ジャイロ　gyro
- 磁気　magnet
- オイラー角　euler
- クオータニオン　quater
- ガス　gas



###プログラム

```C
#include "Nicla_System.h"
#include <ArduinoBLE.h>
#include "Arduino.h"
#include "Arduino_BHY2.h"

Sensor   temp(SENSOR_ID_TEMP);
Sensor   hum(SENSOR_ID_HUM);
Sensor   baro(SENSOR_ID_BARO);
SensorXYZ  accel(SENSOR_ID_ACC);
SensorXYZ  gyro(SENSOR_ID_GYRO);
SensorXYZ  magnet(SENSOR_ID_MAG);
SensorOrientation  euler(SENSOR_ID_ORI);
SensorQuaternion   quater(SENSOR_ID_RV);
Sensor   gas(SENSOR_ID_GAS);

// BLE Service
#define BHI260_SERVICE_UUID   "F000AA30-0451-4000-B000-000000000000"
BLEService Sensor_BHI260_Service(BHI260_SERVICE_UUID);

// BLE  Characteristic
#define BHI260_Temp_Characteristic_UUID       "F000AA31-0451-4000-B000-000000000000"
#define BHI260_Hum_Characteristic_UUID        "F000AA32-0451-4000-B000-000000000000"
#define BHI260_Baro_Characteristic_UUID       "F000AA33-0451-4000-B000-000000000000"
#define BHI260_Acceleration_Characteristic_UUID    "F000AA34-0451-4000-B000-000000000000"
#define BHI260_Gyroscope_Characteristic_UUID       "F000AA35-0451-4000-B000-000000000000"
#define BHI260_Magnet_Characteristic_UUID      "F000AA36-0451-4000-B000-000000000000"
#define BHI260_Euler_Characteristic_UUID       "F000AA37-0451-4000-B000-000000000000"
#define BHI260_Quater_Characteristic_UUID      "F000AA38-0451-4000-B000-000000000000"
#define BHI260_Gas_Characteristic_UUID         "F000AA39-0451-4000-B000-000000000000"
BLEStringCharacteristic    BHI260_Temperature(BHI260_Temp_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Humidity(BHI260_Hum_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Barometer(BHI260_Baro_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Acceleration(BHI260_Acceleration_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Gyroscope(BHI260_Gyroscope_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Magnetometer(BHI260_Magnet_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Euler(BHI260_Euler_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Quaternion(BHI260_Quater_Characteristic_UUID, BLERead | BLENotify, 24);
BLEStringCharacteristic    BHI260_Gas(BHI260_Gas_Characteristic_UUID, BLERead | BLENotify, 24);

// BLE  Descriptor
#define BHI260_Temp_Descriptor_UUID       "2901"
#define BHI260_Hum_Descriptor_UUID        "2901"
#define BHI260_Baro_Descriptor_UUID       "2901"
#define BHI260_Acceleration_Descriptor_UUID    "2901"
#define BHI260_Gyroscope_Descriptor_UUID       "2901"
#define BHI260_Magnet_Descriptor_UUID      "2901"
#define BHI260_Euler_Descriptor_UUID       "2901"
#define BHI260_Quater_Descriptor_UUID      "2901"
#define BHI260_Gas_Descriptor_UUID         "2901"
BLEDescriptor   BHI260_Temp_Descriptor(BHI260_Temp_Descriptor_UUID, "Temperature ; `C");
BLEDescriptor   BHI260_Hum_Descriptor(BHI260_Hum_Descriptor_UUID, "Humidity ; %");
BLEDescriptor   BHI260_Baro_Descriptor(BHI260_Baro_Descriptor_UUID, "Barometer ; hPa");
BLEDescriptor   BHI260_Acceleration_Descriptor(BHI260_Acceleration_Descriptor_UUID, "Acceleration x,y,z");
BLEDescriptor   BHI260_Gyroscope_Descriptor(BHI260_Gyroscope_Descriptor_UUID, "Gyroscope x,y,z");
BLEDescriptor   BHI260_Magnet_Descriptor(BHI260_Magnet_Descriptor_UUID, "Magnetometer x,y,z");
BLEDescriptor   BHI260_Euler_Descriptor(BHI260_Euler_Descriptor_UUID, "Euler head,pitch,roll");
BLEDescriptor   BHI260_Quater_Descriptor(BHI260_Quater_Descriptor_UUID, "Quaternion x,y,z,w");
BLEDescriptor   BHI260_Gas_Descriptor(BHI260_Gas_Descriptor_UUID, "Gas");

#define localNAME  "Nicla_BHI260"
#define DeviceNAME "NiclaBLE"

float oldValue = 0;  // last value 
float previousMillis = 0;  // last time value was checked, in ms

void setup(){
  Serial.begin(9600);
  while(!Serial);
  Serial.println("BHI260 test");
  
  BHY2.begin();
  accel.begin();
  gyro.begin();
  temp.begin();
  gas.begin();
  euler.begin();
  hum.begin();
  baro.begin();
  quater.begin();
  magnet.begin();

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  // set the service
  BLE.setAdvertisedService(Sensor_BHI260_Service);

  // add characteristic
  Sensor_BHI260_Service.addCharacteristic(BHI260_Temperature);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Humidity);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Barometer);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Acceleration);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Gyroscope);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Magnetometer);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Euler);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Quaternion);
  Sensor_BHI260_Service.addCharacteristic(BHI260_Gas);

  // add descriptor
  BHI260_Temperature.addDescriptor(BHI260_Temp_Descriptor);
  BHI260_Humidity.addDescriptor(BHI260_Hum_Descriptor);
  BHI260_Barometer.addDescriptor(BHI260_Baro_Descriptor);
  BHI260_Acceleration.addDescriptor(BHI260_Acceleration_Descriptor);
  BHI260_Gyroscope.addDescriptor(BHI260_Gyroscope_Descriptor);
  BHI260_Magnetometer.addDescriptor(BHI260_Magnet_Descriptor);
  BHI260_Euler.addDescriptor(BHI260_Euler_Descriptor);
  BHI260_Quaternion.addDescriptor(BHI260_Quater_Descriptor);
  BHI260_Gas.addDescriptor(BHI260_Gas_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service);

  // set initial value for this characteristic
  BHI260_Temperature.writeValue("1");
  BHI260_Humidity.writeValue("2");
  BHI260_Barometer.writeValue("3");
  BHI260_Acceleration.writeValue("4");
  BHI260_Gyroscope.writeValue("5");
  BHI260_Magnetometer.writeValue("6");
  BHI260_Euler.writeValue("7");
  BHI260_Quaternion.writeValue("8");
  BHI260_Gas.writeValue("9");

  // 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("\nConnected 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());
  }
}

void updateValue() {
  BHY2.update();
  delay(500);
  // if value has changed
  if (( accel.x() + gyro.y()) != oldValue) {
  
    Serial.println("");
    Serial.println(String("Temperature: ") + String(temp.value(), 1));
    Serial.println(String("Humidity: ") + String(hum.value(), 1));
    Serial.println(String("Barometer : ") + String(baro.value(), 1));
    Serial.println(String("Acceleration :x,y,z ") + String(accel.x()) + ", " +  String(accel.y()) + ", " + String(accel.z()));
    Serial.println(String("Gyroscope :x,y,z ") + String(gyro.x()) + ", " +  String(gyro.y()) + ", " + String(gyro.z()));
    Serial.println(String("Magnetometer :x,y,z ") + String(magnet.x()) + ", " +  String(magnet.y()) + ", " + String(magnet.z()));
    Serial.println(String("Orientation(Euler):head,pitch,roll ") + String(euler.heading(), 3)+", "+ String(euler.pitch(), 3)+", "+ String(euler.roll(), 3));
    Serial.println(String("Quaternion :x,y,z,w ") + String(quater.x()) + ", " +  String(quater.y()) + ", " + String(quater.z())+ ", "+ String(quater.w()));
    Serial.println(String("gas ohm? : ") + String(gas.value(), 1));
        
    String valueof_BHI260_Temperature = String(temp.value());
    String valueof_BHI260_Humidity = String(hum.value());
    String valueof_BHI260_Barometer = String(baro.value());
    String valueof_BHI260_Acceleration = String(accel.x()) + "," +  String(accel.y()) + "," + String(accel.z());
    String valueof_BHI260_Gyroscope = String(gyro.x()) + "," +  String(gyro.y()) + "," + String(gyro.z());
    String valueof_BHI260_Magnetometer = String(magnet.x()) + "," +  String(magnet.y()) + "," + String(magnet.z());
    String valueof_BHI260_Euler =  String(euler.heading())+","+ String(euler.pitch())+","+ String(euler.roll());
    String valueof_BHI260_Quaternion = String(quater.x()) + "," +  String(quater.y()) + "," + String(quater.z()) + "," + String(quater.w());
    String valueof_BHI260_Gas = String(gas.value());

    // update  characteristic
    BHI260_Temperature.writeValue(valueof_BHI260_Temperature);
    BHI260_Humidity.writeValue(valueof_BHI260_Humidity);
    BHI260_Barometer.writeValue(valueof_BHI260_Barometer);
    BHI260_Acceleration.writeValue(valueof_BHI260_Acceleration);
    BHI260_Gyroscope.writeValue(valueof_BHI260_Gyroscope);
    BHI260_Magnetometer.writeValue(valueof_BHI260_Magnetometer);
    BHI260_Euler.writeValue(valueof_BHI260_Euler);
    BHI260_Quaternion.writeValue(valueof_BHI260_Quaternion);
    BHI260_Gas.writeValue(valueof_BHI260_Gas);

  oldValue = accel.x() + gyro.y(); // save the level for next comparison
  }
}
```

###問題点
　Windows10で動いているオンセミのRSL10 Bluetooth Low Enaergy Exploerを使って接続します。
　センサの値はテキストで送っています。
![2021-10-14 (7).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/407635/db58dc33-8eec-c02f-ea28-7d4ddf0e1c80.png)

- オイラー角　euler
- クオータニオン　quater

　文字数が足りなく、データが切れています。24文字の指定をしています。本来32文字送れるという記述をどこかで読んだのですが、現時点でも、修正されていません。
　そこで、二つのセンサ・データを分割するようにしました。キャラが二つ増えます。実際に動かすと、でたらめな値を送り出します。ライブラリに問題があるのか、CPUのメモリがオーバフローしているのか原因はわかりません。