M5Stack Core2をBLEペリフェラルとし、LightBlueによりコマンドをM5Stack Core2に出力します。なお、LightBlueについては、「LightBlueを使ってSensorTagの加速度データの入力」で使用しています。

コマンド出力アプリの作成

コマンド出力アプリは受け取ったデータをシリアル端末に表示します。

  • コマンドを受け取ると28行目のクラスに制御が移り、受け取ったデータをシリアル端末に表示します。
  • 67行目でコールバック「MyCallbacks」を設定します。

blewritetest.ino


#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;

int bleRX, bleTX;
uint8_t notifySend[2] = {0, 0};

#define SERVICE_UUID           "00002220-0000-1000-8000-00805F9B34FB"
#define CHARACTERISTIC_UUID_RX "00002222-0000-1000-8000-00805F9B34FB"  // write
//#define CHARACTERISTIC_UUID_TX "00002221-0000-1000-8000-00805F9B34FB"  // notify

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

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

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");
        for (int i = 0; i < rxValue.length(); i++)
          Serial.print(rxValue[i], HEX);
      }
    }
};

void setup() {
  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("BleDevice");

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  /*  pCharacteristic = pService->createCharacteristic(
                        CHARACTERISTIC_UUID_TX,
                        BLECharacteristic::PROPERTY_NOTIFY
                      );

    pCharacteristic->addDescriptor(new BLE2902());
  */
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify");
}

void loop() {
  // notify changed value
  /*
    if (deviceConnected) {
    if (bleRX < 100) bleTX++;
    else bleTX = 0;
    notifySend[0] = bleTX;
    notifySend[1] = bleRX;
    pCharacteristic->setValue(notifySend, 2);
    pCharacteristic->notify();
    delay(1000);
    // bluetooth stack will go into congestion, if too many packets are sent,
    // in 6 hours test i was able to go as low as 3ms
    }
  */
  // 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;
  }
}

コマンド出力アプリの実行

作成したコマンド出力アプリに、LightBlueを使ってBLEで接続して次のように操作します。

LightBlueを使って次のように16進数で「123ABC」を出力します。

LightBlueを使って入力したデータ「123ABC」が次のようにシリアル端末に表示されます。