CO2センサを使う⑧AdafruitのSCD-30ボードをESP32につないでBLEペリフェラルに

 前回、ESP32 BLE for Arduinoのインストールしました。GitHubにサンプルがあります。BLE_uart.inoを読み込んで修正します。

 キャラクトリスティックはCHARACTERISTIC_UUID_TXだけを使います。


/*
    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
*/
 
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t txValue = 0;
 
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
 
#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

#include <Adafruit_SCD30.h>
Adafruit_SCD30  scd30;
float CO2 = 0;
 
float readSCD30() {
  if (scd30.dataReady()){
    if (!scd30.read()){ Serial.println("Error reading sensor data"); return -1; }
      return scd30.CO2;
  }
}
 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting BLE work!");
 
  if (!scd30.begin()) {
    Serial.println("Failed to find SCD30 chip");
    while (1) { delay(10); }
  }
  
  BLEDevice::init("ESP32 SCD30_CO2_UART");  // local name
  pServer = BLEDevice::createServer();  // Create the BLE Device
  pServer->setCallbacks(new MyServerCallbacks());
  
  BLEService *pService = pServer->createService(SERVICE_UUID);  // Create the BLE Service
  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
                    CHARACTERISTIC_UUID_TX,
                    BLECharacteristic::PROPERTY_NOTIFY
                  );
    
  pService->start();  // Start the service
  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}
 
void loop() {
  Serial.print("\n--CO2 ");
  float CO2 = readSCD30();
  Serial.print(CO2);Serial.println("ppm");
  txValue = (int)CO2;  

  if (deviceConnected) {
        pTxCharacteristic->setValue(txValue);
        pTxCharacteristic->notify();
    delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  }

    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
    }
    // connecting
    if (deviceConnected && !oldDeviceConnected) {
    // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    }
    
   delay(2000);
}

 実行結果です。

 オン・セミコンダクターのBLEモジュールRSL10-USB001GEVK: RSL10 USB DongleとソフトのRSL10 Bluetooth Low Enaergy Exploerを使って情報を見ます。セントラルの立場です。

 Connectし、Discover Servicesをクリックすると、CO2の整数値が送られてきます。32ビットのリトル・エンディアン・フォーマットです。

OLEDディスプレイにも表示する

 前回、動作テスト時に使ったサンプルoled_co2_monitor から、表示に必要な部分をコピペしてきます。


/*
    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
*/
 
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t txValue = 0;
 
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
 
#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

#include <Adafruit_SCD30.h>
#include <Adafruit_SSD1306.h>
Adafruit_SCD30  scd30;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
float CO2 = 0;
 
float readSCD30() {
  if (scd30.dataReady()){
    if (!scd30.read()){ Serial.println("Error reading sensor data"); return -1; }
      return scd30.CO2;
  }
}
 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting BLE work!");
 
  if (!scd30.begin()) {
    Serial.println("Failed to find SCD30 chip");
    while (1) { delay(10); }
  }

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  if (!scd30.setMeasurementInterval(2)){
    Serial.println("Failed to set measurement interval");
    while(1) {delay(10);}
  }
  Serial.print("Measurement Interval: "); 
  Serial.print(scd30.getMeasurementInterval()); 
  Serial.println(" seconds");
  
  display.display();
  delay(500); // Pause for half second

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setRotation(0);
  
  BLEDevice::init("ESP32 SCD30_CO2_UART");  // local name
  pServer = BLEDevice::createServer();  // Create the BLE Device
  pServer->setCallbacks(new MyServerCallbacks());
  
  BLEService *pService = pServer->createService(SERVICE_UUID);  // Create the BLE Service
  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
                    CHARACTERISTIC_UUID_TX,
                    BLECharacteristic::PROPERTY_NOTIFY
                    );
    
  pService->start();  // Start the service
  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}
 
void loop() {
  Serial.print("\n--CO2 ");
  float CO2 = readSCD30();
  Serial.print(CO2);Serial.println("ppm");
  txValue = (int)CO2;

    display.clearDisplay();
    display.setCursor(0,0);
    display.setTextSize(2);
    display.println("CO2:");
    display.print(CO2, 2);

    display.setTextSize(1);

    display.setCursor(100, 20);
    display.println(" ppm");
    display.display();
    
  if (deviceConnected) {
        pTxCharacteristic->setValue(txValue);
        pTxCharacteristic->notify();
    delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  }

    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
    }
    // connecting
    if (deviceConnected && !oldDeviceConnected) {
    // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    }
    
   delay(2000);
}

 

前へ

CO2センサを使う⑦AdafruitのSCD-30ボードをESP32につなぐ

次へ

CO2センサを使う⑨Windows10でBLEセントラル