M5StickC(ESP32)でダイソーのBluetoothシャッターを操作(1.0.4対応版)

現時点の情報ですので、最新情報はM5StickC非公式日本語リファレンスを確認してください。

概要

ESP32の1.0.4がでましたので、Bluetoothまわりを再検証してみました。

現状のところHIDディバイスをBluetooth接続して利用する場合に、致命的なバグはなくなっていると思います。

修正点

  • BLEUUIDのテキスト表示が修正
  • m_characteristicMapByHandleが取得できるようになった

Scanのサンプル(GitHub)

#include "BLEDevice.h"
// 検索するBLEデバイス。serviceUUIDを調べる場合には空にする(例はHuman Interface Device"00001812-0000-1000-8000-00805f9b34fb")
static BLEUUID serviceUUID("1812");
static BLEAdvertisedDevice* myDevice;
// 接続してCharacteristic一覧を取得
bool connectToServer() {
  Serial.print("接続先 : ");
  Serial.println(myDevice->getAddress().toString().c_str());
  Serial.println("start createClient");
  BLEClient*  pClient  = BLEDevice::createClient();
  Serial.println("end createClient");
  Serial.println("start connect");
  pClient->connect(myDevice);
  Serial.println("end connect");
  // サービス取得
  Serial.println("start getService");
  BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  Serial.println("end getService");
  if (pRemoteService == nullptr) {
    pClient->disconnect();
    return false;
  }
  // Characteristic一覧
  Serial.println("characteristic一覧");
  std::map<uint16_t, BLERemoteCharacteristic*>* mapCharacteristics = pRemoteService->getCharacteristicsByHandle();
  for (std::map<uint16_t, BLERemoteCharacteristic*>::iterator i = mapCharacteristics->begin(); i != mapCharacteristics->end(); ++i) {
    Serial.print(" - characteristic UUID : ");
    Serial.print(i->second->getUUID().toString().c_str());
    Serial.print(" Broadcast:");
    Serial.print(i->second->canBroadcast()?'O':'X');
    Serial.print(" Read:");
    Serial.print(i->second->canRead()?'O':'X');
    Serial.print(" WriteNoResponse:");
    Serial.print(i->second->canWriteNoResponse()?'O':'X');
    Serial.print(" Write:");
    Serial.print(i->second->canWrite()?'O':'X');
    Serial.print(" Notify:");
    Serial.print(i->second->canNotify()?'O':'X');
    Serial.print(" Indicate:");
    Serial.print(i->second->canIndicate()?'O':'X');
    Serial.println();
  }
  // stop
  Serial.println("プログラム停止!");
  while (1) delay(1000);
  return true;
}
// 検索したデバイスを受信するコールバック関数
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.print("BLE デバイス発見 : ");
      Serial.println(advertisedDevice.toString().c_str());
      if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
        // 指定デバイスだったら接続する
        BLEDevice::getScan()->stop();
        myDevice = new BLEAdvertisedDevice(advertisedDevice);
      }
    }
};
void setup() {
  Serial.begin(115200);
  Serial.println("BLEデバイス検索開始...");
  BLEDevice::init("");
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
}
void loop() {
  if (myDevice != NULL) {
    connectToServer();
  }
  delay(1000);
}

特殊なことをしないでも利用できるようになりました。

本家スケッチ例との差分として、同一UUID対策でgetCharacteristicsByHandle()を利用してCharacteristicsの処理を行ってください。

BLEデバイス検索開始...
BLE デバイス発見 : Name: AB Shutter3       , Address: ff:ff:XX:XX:XX:XX, appearance: 961, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb
接続先 : ff:ff:XX:XX:XX:XX
start createClient
end createClient
start connect
end connect
start getService
end getService
characteristic一覧
 - characteristic UUID : 00002a4e-0000-1000-8000-00805f9b34fb Broadcast:X Read:O WriteNoResponse:O Write:X Notify:X Indicate:X
 - characteristic UUID : 00002a4d-0000-1000-8000-00805f9b34fb Broadcast:X Read:O WriteNoResponse:X Write:X Notify:O Indicate:X
 - characteristic UUID : 00002a4d-0000-1000-8000-00805f9b34fb Broadcast:X Read:O WriteNoResponse:X Write:X Notify:O Indicate:X
 - characteristic UUID : 00002a4b-0000-1000-8000-00805f9b34fb Broadcast:X Read:O WriteNoResponse:X Write:X Notify:X Indicate:X
 - characteristic UUID : 00002a4a-0000-1000-8000-00805f9b34fb Broadcast:X Read:O WriteNoResponse:X Write:X Notify:X Indicate:X
 - characteristic UUID : 00002a4c-0000-1000-8000-00805f9b34fb Broadcast:X Read:X WriteNoResponse:O Write:X Notify:X Indicate:X
プログラム停止!

たぶん、こんな出力になるはずです。このデバイスは同一UUIDがたくさんあるので、本家スケッチ例だと1つしかcharacteristicが取得できないはずです。

ボタン状態取得のサンプル(GitHub)

/**
   A BLE client example that is rich in capabilities.
   There is a lot new capabilities implemented.
   author unknown
   updated by chegewara
*/
#include "BLEDevice.h"
// The remote service we wish to connect to.
static BLEUUID serviceUUID("1812");
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLEAdvertisedDevice* myDevice;
static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.print("Notify callback for characteristic ");
  Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  Serial.print("(");
  Serial.print(pBLERemoteCharacteristic->getHandle());
  Serial.print(") of data length ");
  Serial.print(length);
  Serial.print(" data: ");
  for ( int i = 0 ; i < length ; i++ ) {
    Serial.printf( "%02X ", pData[i] );
  }
  Serial.println();
}
class MyClientCallback : public BLEClientCallbacks {
    void onConnect(BLEClient* pclient) {
    }
    void onDisconnect(BLEClient* pclient) {
      connected = false;
      Serial.println("onDisconnect");
    }
};
bool connectToServer() {
  Serial.print("Forming a connection to ");
  Serial.println(myDevice->getAddress().toString().c_str());
  BLEClient*  pClient  = BLEDevice::createClient();
  Serial.println(" - Created client");
  pClient->setClientCallbacks(new MyClientCallback());
  // Connect to the remove BLE Server.
  pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
  Serial.println(" - Connected to server");
  // Obtain a reference to the service we are after in the remote BLE server.
  BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  if (pRemoteService == nullptr) {
    Serial.print("Failed to find our service UUID: ");
    Serial.println(serviceUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - Found our service");
  std::map<uint16_t, BLERemoteCharacteristic*>* mapCharacteristics = pRemoteService->getCharacteristicsByHandle();
  for (std::map<uint16_t, BLERemoteCharacteristic*>::iterator i = mapCharacteristics->begin(); i != mapCharacteristics->end(); ++i) {
    if (i->second->canNotify()) {
      Serial.println(" - Add Notify");
      i->second->registerForNotify(notifyCallback);
    }
  }
  connected = true;
  return true;
}
/**
   Scan for BLE servers and find the first one that advertises the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    /**
        Called for each advertising BLE server.
    */
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.print("BLE Advertised Device found: ");
      Serial.println(advertisedDevice.toString().c_str());
      // We have found a device, let us now see if it contains the service we are looking for.
      if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
        BLEDevice::getScan()->stop();
        myDevice = new BLEAdvertisedDevice(advertisedDevice);
        doConnect = true;
        doScan = true;
      } // Found our server
    } // onResult
}; // MyAdvertisedDeviceCallbacks
void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");
  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
} // End of setup.
// This is the Arduino main loop function.
void loop() {
  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }
  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
  } else if (doScan) {
    BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  }
  delay(1000); // Delay a second between loops.
} // End of loop

こちらも普通に使えるようになりました。

Starting Arduino BLE Client application...
BLE Advertised Device found: Name: AB Shutter3       , Address: ff:ff:XX:XX:XX:XX, appearance: 961, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb
Forming a connection to ff:ff:XX:XX:XX:XX
 - Created client
 - Connected to server
 - Found our service
 - Add Notify
 - Add Notify
We are now connected to the BLE Server.
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(19) of data length 2 data: 01 00 
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(19) of data length 2 data: 00 00 
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(23) of data length 2 data: 00 28 
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(19) of data length 2 data: 01 00 
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(19) of data length 2 data: 00 00 
Notify callback for characteristic 00002a4d-0000-1000-8000-00805f9b34fb(23) of data length 2 data: 00 00 

上記が実行結果例です。2つあるキーのうち、iOSキーを押した場合、ハンドルID19の01 00(ボリュームアップ)と00 00(キーアップ)が飛んできます。

Androidキーを押した場合には、ハンドルID23の00 28(エンター)が押されたのち、ハンドルID19の01 00(ボリュームアップ)と00 00(キーアップ)がきて、ハンドルID23の00 00(キーアップ)が飛んできます。

まとめ

まだまだ問題はありそうですが、ハングアップや、利用できないというバグはなくなったと思います。

コメント

  1. 橘昌 より:

    こちらのプログラムを利用させていただいていますが、何点か質問があります。

    ダイソーのリモコンは1.5分程で自動で電源が切れてしまいますが、その際onDisconnectとなって以後再接続されるまでループするようです。
    しかし別のプログラムを走らせているのに再接続待機のループになってしまい動作が止まってしまいます…
    最初から接続しないでESP起動させれば問題ないのですが、リモコンをONにしてESPを起動させるとこの電源offによるループ問題が出てしまいます…

    電源OFF回避すれば良いのですが仕様で自動電源OFFは回避できず頻繁にボタン押すわけにも行かないので詰んでいます…。
    プログラムでリモコンを見失っても再接続待機ループに入らず無視して他のloopのプログラムを継続させる方法はないでしょうか?

    あと新しい方のNimBLEを利用した方は何故か接続するとバグ起こして再起動ループしてしまいます

    • たなかまさゆき より:

      void onDisconnect(BLEClient* pclient) {
      connected = false;

      上記で切断時に接続ループに戻る設定にしているのをはずせば大丈夫だと思います
      再接続するときには、、、ボタン押したら再接続モードとかにするか、再起動かな?

      ESP32のBLE周りはバージョン変わるといろいろ動作も変わるので面倒なんですよね。。。

      • 橘昌 より:

        返信ありがとうございます。
        当該部分をコメントアウトしたら再接続ループは解消しました。
        しかし一度ペアリングが外れるとやはり再度ボタン押してもESPをリセットしない限りダメですね…
        理想はloopを動かしたまま再ペアリング出来ればなのですが…。

        ダメならリモコンの片側のボタンを米粒マイコンとかで定期的に連打してパワーオフ回避するしかないかな~と思い始めてます(ESP側のプログラムはボタンをカチカチと連打する位では動作に支障ないので)

        • たなかまさゆき より:

          ペアリング系は常に無線を動かし続けるので片方のコアを全部持っていかれるんですよね
          マルチタスクとか使えば少しは改善できる気もするのですが、なかかな難しそうです。。。

          • 橘昌 より:

            やはりダイソーのリモコン前提だと難しそうですね…
            物理的にリモコン操作してオートパワーオフを回避する方法を試してみたいと思います。
            いろいろありがとうございました。