3
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 1 year has passed since last update.

秋月電子通商nRF52840使用BLEマイコンボードで温度計ペリフェラル

Posted at

Arduino IDEで利用する

 nRF52840使用BLEマイコンボードのWebにあるボードマネージャー導入方法を開きます。
 IDEの基本設定の追加のボードマネージャーのURLへ、https://adafruit.github.io/arduino-board-index/package_adafruit_index.jsonを書き込みます。
nrf101.png

 ボードマネージャでAdafruit nRF52 by Adafruitをインストールします。
nrf102.png

  マイコン・ボードをPCとつなぎます。自動で検出しますが、手動では、つぎのようにFeather nRF52840 Expressボードを選びます。COMポートも選択します。

nrf103.png

気圧センサBMP280を利用

 ライブラリマネージャで、Adafruit BMP280 Library by Adafruitをインストールします。

nrf104.png

 途中に、パネルが出るので、全てをインストールをクリックします。
nrf105.png

 ライブラリマネージャで、Adafruit BMP280 Library by Adafruitを表示したときに出た詳細情報をクリックします。
 https://github.com/adafruit/Adafruit_BMP280_Libraryに飛びます。
 examplesを開きます。
nrf106.png

 bmp280testに入っているbmp280test.inoのソースをコピペします。
 AdadruitのSTEMMA QTのBMP280を4ピンコネクタで、マイコンボードの4ピンコネクタにつなぎます。

名称未設定3-03738de3.png

 STEMMA QT(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。
 コンパイルします。
2023-02-26 (8).png

I2C専用にスリムにする

//  Written by Limor Fried & Kevin Townsend for Adafruit Industries.

#include <Wire.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp; // I2C

void setup() {
  Serial.begin(9600);
  while ( !Serial ) delay(100);   // wait for native usb
  bmp.begin();
  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    Serial.print("Temperature = ");
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");
    Serial.print(bmp.readPressure()/100);
    Serial.println(" hPa");

    Serial.println();
    delay(2000);
}

 コンパイルして実行します。

image.png

BLEペリフェラルにする

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
// Written by Limor Fried & Kevin Townsend for Adafruit Industries.
*/
 
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>   
 
float temp = 0;
float press = 0;
 
#define SERVICE_UUID          "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_T_UUID "beb5483d-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_P_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
 
BLECharacteristic *tCharacteristic;
BLECharacteristic *pCharacteristic;
 
Adafruit_BMP280 bmp; // I2C
 
void setup() {
  Serial.begin(115200);
  while(!Serial);
  Serial.println("Starting BMP280 work!");
  bmp.begin();
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
                  
  BLEDevice::init("BMP280 peripheral");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  tCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_T_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_NOTIFY
                                       );
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_P_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_NOTIFY
                                       );
  pService->start();
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  BLEDevice::startAdvertising();
}
 
void loop() {
  readBMP280();
  tCharacteristic->setValue(temp);
  tCharacteristic->notify();
  pCharacteristic->setValue(press);
  pCharacteristic->notify();
   
  delay(2000);
}
 
void readBMP280() {  
    Serial.print("Temperature = ");
    temp = bmp.readTemperature();
    Serial.print(temp);
    Serial.println(" *C");
 
    Serial.print("Pressure = ");
    press = bmp.readPressure() / 100.0;
    Serial.print(press);
    Serial.println(" hPa");
 
    Serial.println();
}

 長いです。コンパイルには使っていないAdafruit_AHRSをインストールしておきます。

 オン・セミコンダクターのBLEであるRSL10 Bluetooth Low Enaergy Exploerを使って情報を見ます。セントラルの立場です。connectで接続し、Discover Serviceでサービスを表示しました。

 読み出したデータは、Floating Point to Hex Converterサイトで変換しました。4桁の固定小数点はリトル・エンディアンなので、逆順にします。
 0x9b158044は1024.68、0x703d9641は18.78です。

2023-02-26 (10).png

 短いのを書きましたが。

#include <ArduinoBLE.h>

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

#define BMP280_P_Characteristic_UUID    "F000AA31-0451-4000-B000-000000000000"
#define BMP280_T_Characteristic_UUID    "F000AA32-0451-4000-B000-000000000000"
// BLE  Characteristic
BLEFloatCharacteristic  BMP280_Press_Characteristic(BMP280_P_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic  BMP280_Temperature_Characteristic(BMP280_T_Characteristic_UUID, BLERead | BLENotify);

#define BMP280_P_Descriptor_UUID    "2901"
#define BMP280_T_Descriptor_UUID    "2901"
// BLE  Descriptor
BLEDescriptor   BMP280_P_Descriptor(BMP280_P_Descriptor_UUID, "Press;hPa IEEE-754 format");
BLEDescriptor   BMP280_T_Descriptor(BMP280_T_Descriptor_UUID, "Temperature;`C");

#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // I2C
float Press = 0, Temperature = 0;
 
void readBMP280() {
   Press = bmp.readPressure()/100; 
   Temperature = bmp.readTemperature(); 
}

#define localNAME  "akizukidenshi_Temp"
#define DeviceNAME "akizukidenshi_BLE"
 
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);
  }
 
  bmp.begin();
  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  
  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  // add the service UUID
  BLE.setAdvertisedService(Sensor_BMP280_Service);

  // add characteristic
  Sensor_BMP280_Service.addCharacteristic(BMP280_Press_Characteristic);
  Sensor_BMP280_Service.addCharacteristic(BMP280_Temperature_Characteristic);
  
  // add descriptor
  BMP280_Press_Characteristic.addDescriptor(BMP280_P_Descriptor);
  BMP280_Temperature_Characteristic.addDescriptor(BMP280_T_Descriptor);
    
  // Add service
  BLE.addService(Sensor_BMP280_Service);
 
  // set initial value for this characteristic
  BMP280_Press_Characteristic.writeValue(0);
  BMP280_Temperature_Characteristic.writeValue(0);
    
  // 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  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() {
  readBMP280();

  // if value has changed
  if (Press+Temperature != oldValue) {

    Serial.println(String(Press,2) + "hPa " + String(Temperature,1));
    
    // update  characteristic
    BMP280_Press_Characteristic.writeValue(Press);
    BMP280_Temperature_Characteristic.writeValue(Temperature);
    oldValue = Press+Temperature; // save the level for next comparison
  }
}

Adafruitのボードでは、エラーが出て、アップロードできません。知らないボードというエラーも出ています。
Seeedなどのボードを選択しても、別のエラーが出ますが、アップロードをしようとしますが、アップロードに失敗します。
とりあえず、ここまで。

IMGP1598.png

3
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
3
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?