1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ARDUINO NANO RP2040 CONNECT WITH HEADERSプラスArduino IDEでBLEペリフェラル

Last updated at Posted at 2021-05-25

Arduino NANO RP2040 CONNECTはArduino.ccの製品で、Raspberry Pi PicoのCPUを積み、6軸加速度センサやマイクも載っています。技適の通ったNina W102 uBlox moduleを搭載しているので、Wi-Fi、BLEのプログラムが書けます。

ファイル名

 ここでは、Nano 33 BLE Senseでマルチセンサ・ペリフェラルを作る (7) 9軸慣性センサ IMU LSM9DS1をベースに、6軸加速度センサの値をBLEで送信するスケッチを描きます。

開発環境

 Arduino IDEは1.8.15、ボードマネージャで最新に、ライブラリも最新にしておきます。ちょっと前の1.8.13のままだと、コンパイル時にぼろぼろエラーが出ました。
 最初に、IMUのLSM6DSOXTR (6軸)のドライバを入れます。
rp101.png
 同時にサンプル例が入ります。SimpleAccelerometerを読み込み、実行できることを確かめます。

スケッチ

 Notifyに対応したスケッチのつもりですが、ArduinoBLEライブラリでNotifyはうまく動いていないような気がするので、read属性しかつけていません。
 UUIDは、ユニークなUUIDを発行してくれるところのを使っています。

# include <ArduinoBLE.h>

# define LSM6DSOX_SERVICE_UUID   "F000AA30-0451-4000-B000-000000000000"

// BLE Service
BLEService Sensor_LSM6DSOX_Service(LSM6DSOX_SERVICE_UUID);

# define LSM6DSOX_Acceleration_Characteristic_UUID    "F000AA31-0451-4000-B000-000000000000"
# define LSM6DSOX_Gyroscope_Characteristic_UUID       "F000AA32-0451-4000-B000-000000000000"

// BLE  Characteristic
BLEStringCharacteristic LSM6DSOX_Acceleration(LSM6DSOX_Acceleration_Characteristic_UUID, BLERead, 24);
BLEStringCharacteristic LSM6DSOX_Gyroscope(LSM6DSOX_Gyroscope_Characteristic_UUID, BLERead, 24);

# include <Arduino_LSM6DSOX.h>

float AccelerationX = 0, AccelerationY = 0, AccelerationZ = 0;
float GyroscopeX = 0, GyroscopeY = 0, GyroscopeZ = 0;
 
void readLSM6DSOX() {
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(AccelerationX, AccelerationY, AccelerationZ);
  }
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(GyroscopeX, GyroscopeY, GyroscopeZ);
  }
}

# define localNAME  "RP2060_LSM6DSOX"
# define DeviceNAME "RP2060BLE"
 
float oldValue = 0;  // last value 
float 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 (!IMU.begin()) { // LSM6DSOX
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  
  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);
 
  // add the service UUID
  BLE.setAdvertisedService(Sensor_LSM6DSOX_Service);
 
  // add characteristic
  Sensor_LSM6DSOX_Service.addCharacteristic(LSM6DSOX_Acceleration);
  Sensor_LSM6DSOX_Service.addCharacteristic(LSM6DSOX_Gyroscope);
 
  // Add service
  BLE.addService(Sensor_LSM6DSOX_Service);
 
  // set initial value for this characteristic
  LSM6DSOX_Acceleration.writeValue(String(oldValue));
  LSM6DSOX_Gyroscope.writeValue(String(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() {
  readLSM6DSOX();
  String valueof_LSM6DSOX_Acceleration = String(AccelerationX) + "," +  String(AccelerationY) + "," + String(AccelerationZ);
  String valueof_LSM6DSOX_Gyroscope = String(GyroscopeX) + "," +  String(GyroscopeY) + "," + String(GyroscopeZ);

  // if value has changed
  if (( AccelerationX + GyroscopeX) != oldValue) {

    Serial.print("---\nLSM6DSOX_Acceleration % is now: ");
    Serial.println(String(AccelerationX) + "," +  String(AccelerationY) + "," + String(AccelerationZ));
    Serial.print("LSM6DSOX_Gyroscope % is now: ");
    Serial.println(String(GyroscopeX) + "," +  String(GyroscopeY) + "," + String(GyroscopeZ));

    // update  characteristic
    LSM6DSOX_Acceleration.writeValue(valueof_LSM6DSOX_Acceleration);
    LSM6DSOX_Gyroscope.writeValue(valueof_LSM6DSOX_Gyroscope);    
    oldValue = AccelerationX + GyroscopeX; // save the level for next comparison
  }
}

動作を確認

 Windows10で動いているオンセミのRSL10 Bluetooth Low Enaergy Exploerを使って接続します。
 センサの値はテキストで送っています。
 上の表示データはArduino IDEのシリアルモニタで、送信しているデータを表示しています。下が、セントラルの立場で接続しているオンセミのツールです。送っているデータと、受信しているデータが同じであることが確認できました。

rp102.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?