初めてのBLE (13) ESP32でペリフェラル①ESP32のCPU温度

 ESP32ボード(Wemos LOLIN32)をペリフェラルにする実験をします。利用するライブラリESP32 BLE for Arduinoでは、ペリフェラルはサーバ、セントラルはクライアントと呼んでいます。

ESP32 BLE for Arduinoのインストール

 ESP32でArduino IDEを利用する方法は、こちらを参照してください。BLEは、ライブラリ管理から、「ble ESP32」の検索で見つかったESP32 BLE for Arduinoをインストールします。

(2020/4/7)ESP32のシステム導入したのち、最新の状態になっているとBLEのライブラリが含まれているというご指摘をいただきました。サンプルの中にすでにESP32 BLE Arduinoが見えている場合は、この作業は不要です。 

BLE_scan

 正常にライブラリが組み込まれたかどうかを確認するために、サンプル・スケッチのBLE_scanを読み込み、コンパイル、実行します。

 micro:bitを見つけてきました。

BLE_server

 サンプル・スケッチからBLE_serverを読み込み、コンパイル、実行します。

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

 スケッチ内のServiceとCharacteristicが正しく読み取れています。

 ESP32のCPU温度は、temperatureRead()で読み取れます。この値をpCharacteristic->setValue()で送ります。

21行で、Local Nameを変更しました。

30~32行で温度をValueにセットします。

/*
    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> // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" void setup() { Serial.begin(115200); Serial.println("Starting BLE work!"); BLEDevice::init("ESP32 peripheral"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); uint8_t temp = temperatureRead(); Serial.println(temp); pCharacteristic->setValue((uint8_t*)&temp,1); pService->start(); // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); // Serial.println("Characteristic defined! Now you can read it in your phone!"); } void loop() { // put your main code here, to run repeatedly: delay(2000); }

 実行します。温度は届いていました。

前へ

初めてのBLE (12) Raspberry Piでペリフェラル④Nano 33 BLE Senseをセントラルに

次へ

初めてのBLE (14) ESP32でペリフェラル②Nano 33 BLE Senseのセントラル