[ESP32] M5Stack 簡單藍芽通訊,Server and Client

2018-05-08


最近跟朋友組成一個自行車車隊,因為需要 Leader 那邊能夠警示後方車友能夠左轉右轉,所以才有這想法,主要我採用的是 M5 Stack ,這是啥東西 可以參考這裡 http://m5stack.com/

簡單的說就是一個基於 ESP32 的玩具
image

這邊我只分享遇到的心得跟 Source Code 提供,想玩的可以自己抓下來自已放上去看看。

1. 首先你得先去 這地方安裝 Bluetooth for Arduino ESP32 的 library : https://github.com/espressif/arduino-esp32

2. 接下來你會遇到  Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it. ,接下來你必須要做 的就是去你的 Arduino 下面
arduino\hardware\espressif\esp32\tools\partitions\default.csv

數值改成這樣 :
image

數據可以參考這網站 : https://qiita.com/juosugi/items/5e36afac684055da058e

之後在  arduino\hardware\espressif\esp32\board.txt 將  m5stack-core-esp32.upload.maximum_size=1310720 改成 m5stack-core-esp32.upload.maximum_size=1835008

3. Server Code :


#include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #include <M5Stack.h> #define SERVICE_UUID "0000fff0-0000-1000-8000-00805f9b34fb" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" String Message = "DMESSAGE"; BLEService *pService; BLEAdvertising *pAdvertising; BLECharacteristic *pCharacteristic; void setup() { M5.begin(); Serial.begin(115200); M5.Lcd.println("Starting BLE work!"); BLEDevice::init("D51-MASTER"); BLEServer *pServer = BLEDevice::createServer(); pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setValue(Message.c_str()); pService->start(); pAdvertising = pServer->getAdvertising(); // 這一行很重要&#65292;之前就是這一行沒加入一直沒有成功 pAdvertising->addServiceUUID(pService->getUUID()); pAdvertising->start(); M5.Lcd.println("Success Broadcasting.."); } void loop() { if (M5.BtnA.wasPressed()) { Message = "<<"; pCharacteristic->setValue(Message.c_str()); M5.Lcd.print("Set Value to "); M5.Lcd.println(Message.c_str()); } else if (M5.BtnC.wasPressed()) { Message = ">>"; pCharacteristic->setValue(Message.c_str()); M5.Lcd.print("Set Value to "); M5.Lcd.println(Message.c_str()); } else if (M5.BtnB.wasPressed()) { Message = "D51c"; pCharacteristic->setValue(Message.c_str()); M5.Lcd.print("Set Value to "); M5.Lcd.println(Message.c_str()); } M5.update(); delay(500); }
4. Client Code :
/** * A BLE client example that is rich in capabilities. */ #include "BLEDevice.h" #include <M5Stack.h> //#include "BLEScan.h" // The remote service we wish to connect to. static BLEUUID serviceUUID("0000fff0-0000-1000-8000-00805f9b34fb"); // The characteristic of the remote service we are interested in. static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"); static BLEAddress *pServerAddress; static boolean doConnect = false; static boolean connected = false; static BLERemoteCharacteristic* pRemoteCharacteristic; static void notifyCallback( BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { M5.Lcd.print("Notify callback for characteristic "); M5.Lcd.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); M5.Lcd.print(" of data length "); M5.Lcd.println(length); } bool connectToServer(BLEAddress pAddress) { M5.Lcd.print("Forming a connection to "); M5.Lcd.println(pAddress.toString().c_str()); BLEClient* pClient = BLEDevice::createClient(); M5.Lcd.println(" - Created client"); // Connect to the remove BLE Server. pClient->connect(pAddress); M5.Lcd.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) { M5.Lcd.print("Failed to find our service UUID: "); M5.Lcd.println(serviceUUID.toString().c_str()); return false; } M5.Lcd.println(" - Found our service"); // Obtain a reference to the characteristic in the service of the remote BLE server. pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { M5.Lcd.print("Failed to find our characteristic UUID: "); M5.Lcd.println(charUUID.toString().c_str()); return false; } M5.Lcd.println(" - Found our characteristic"); // Read the value of the characteristic. std::string value = pRemoteCharacteristic->readValue(); M5.Lcd.print("The characteristic value was: "); M5.Lcd.println(value.c_str()); pRemoteCharacteristic->registerForNotify(notifyCallback); } /** * 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) { M5.Lcd.print("BLE Advertised Device found: "); M5.Lcd.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.getServiceUUID().equals(serviceUUID)) { // M5.Lcd.print("Found our device! address: "); advertisedDevice.getScan()->stop(); pServerAddress = new BLEAddress(advertisedDevice.getAddress()); doConnect = true; } // Found our server } // onResult }; // MyAdvertisedDeviceCallbacks void setup() { M5.begin(); M5.Lcd.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 30 seconds. BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); pBLEScan->start(30); } // 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(*pServerAddress)) { M5.Lcd.println("We are now connected to the BLE Server."); connected = true; } else { M5.Lcd.println("We have failed to connect to the server; there is nothin more we will do."); } doConnect = false; } if (connected) { std::string value = pRemoteCharacteristic->readValue(); M5.Lcd.println(value.c_str()); } delay(1000); // Delay a second between loops. } // End of loop

source code 下載:
https://github.com/donma/M5StackBLE

Result:
530533

因為我改過的 Code 太已經太複雜了,所以我分享了一個從範例改得簡單的 Code …

當然我們用的完整版的圖片是這樣:

530436


當麻許的碎念筆記 2014 | Donma Hsu Design.