BLEを使って,PCからm5stackを操作したい時のお手軽な手段.
ブラウザでBLE通信を行う場合はweb bluetooth APIを使用.
ローカルかhttps://・・・でないと動かないので注意が必要.
今回はjavascriptで適当にブラウザ上にボタンを作って,
押したボタンに合わせて,m5stackのディスプレイの表示を変更するサンプルを作成しました.
ちゃんとサーバに上げて,https化すれば,スマホでm5stackを操作するってことができますね(^^)
では,まずm5stackのサンプルコードから.
main.inoに加えて,
BLE用にmy_ble.hとm_ble.cppをインクルードさせています.
mainのsetupでBLEの初期設定だけを書いてあげれば,ok.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <M5Stack.h> #include <M5Stack.h> //BLE関連は別ファイルに分けておくと整理できて良い. #include "my_ble.h" void setup() { //========================================== //init M5stack ect. M5.begin(); M5.Lcd.setTextSize(3); //========================================== // init BLE BLEInit("m5stack-111");//BLE接続する際にデバイス名が「my-m5stack」で見つかる // Create the BLE Device delay(1000);//一応delay Serial.println("set up BLE"); //========================================== M5.Lcd.setCursor(20,20); M5.Lcd.printf("finish set up");//setupが終わったら,m5stackに表示 } void loop() { } |
続いて,BLE関連
my_ble.hは以下の感じで.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#ifndef MY_BLE #ifndef MY_BLE #define MY_BLE #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #define SERVICE_UUID "28a9e388-2cc8-46f1-a125-a5b17860411f" #define CHARACTERISTIC_UUID "f1445c0c-a803-4ca7-abeb-651ac724c103" /** * The power level can be one of: * * ESP_PWR_LVL_N14 弱い * * ESP_PWR_LVL_N11 * * ESP_PWR_LVL_N8 * * ESP_PWR_LVL_N5 * * ESP_PWR_LVL_N2 * * ESP_PWR_LVL_P1 * * ESP_PWR_LVL_P4 * * ESP_PWR_LVL_P7 強い */ #define TX_POWER ESP_PWR_LVL_P7 void BLEInit(char deviceName[]); void checkConnectionState(); class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer);//接続されたとき void onDisconnect(BLEServer* pServer);//切断されたとき }; class MyRequestCallbacks: public BLECharacteristicCallbacks { void onRead(BLECharacteristic *pCharacteristic);//特に何もせず void onWrite(BLECharacteristic *pCharacteristic);//データを受信した時に呼ばれる }; #endif |
続いて,my_ble.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#include "M5Stack.h" #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include "my_ble.h" BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; void BLEInit(char deviceName[]) { Serial.println("Initializing BLE..."); // Create the BLE Device BLEDevice::init(deviceName); // 上でdefineされている送信パワーを設定する BLEDevice::setPower(TX_POWER); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // コマンドを受けるcharacteristicを作る // PROPERTYシリーズはたぶん最低READ, WRITE_NRがあればOKな気がする pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor pCharacteristic->addDescriptor(new BLE2902()); pCharacteristic->setCallbacks(new MyRequestCallbacks()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); //pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue pAdvertising->setMinPreferred(0x12); pAdvertising->setScanResponse(true); BLEDevice::startAdvertising(); Serial.println("Waiting for connection"); } // デバイスが接続したときや切断したときに呼ばれる void MyServerCallbacks::onConnect(BLEServer *pServer) { Serial.println("New device connected"); // 複数接続を許可するのでAdvertisingを再開 BLEDevice::startAdvertising(); } void MyServerCallbacks::onDisconnect(BLEServer *pServer) { Serial.println("Device disconnected"); } // コマンドの処理 // BLEでつながっているPCから書き込みが流れてきたときに呼ばれる void MyRequestCallbacks::onWrite(BLECharacteristic *pCharacteristic) { //PCからテキストデータを送るときはこれ. std::string Value = pCharacteristic->getValue(); //byteデータを読み込みたいときは以下の通り(この例では8byteのデータ読み取る) /* * uint8_t *request = pCharacteristic->getData(); Serial.println("************"); Serial.println("Incoming write request from Bluetooth device"); uint8_t command[8]; for(int i = 0; i < 8; i++) { uint8_t c = *(request + i * sizeof(uint8_t)); command[i] = c; // PC側のシリアルに流す // print upper 4 bits Serial.write(set[c>>4]); // print lower 4 bits Serial.write(set[c&0x0F]); Serial.write(" "); } Serial.write("\n"); Serial.println("************"); */ M5.Lcd.println("Receive"); M5.Lcd.println(Value.c_str()); } // とくに何もしない void MyRequestCallbacks::onRead(BLECharacteristic *pCharacteristic) { Serial.println("Incoming read request from Bluetooth device"); } |
これで,m5stack側は接続されたデバイスからテキストを受信して,
受信テキストがディスプレイに表示されるはず.
次はPC側を作っていきます(^^)