User Tools

Site Tools


esp32:ble_serial_v1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
esp32:ble_serial_v1 [2019/09/29 19:40] – created aistisesp32:ble_serial_v1 [2019/10/01 21:55] (current) – aistis
Line 1: Line 1:
-<code c>+ESP32 bluetooth BLE serial/UART example - forward: serial <=> BLE  
 + 
 +{{:esp32:pasted:20190930-090018.png}} 
 + 
 +<code cpp>
 #include <BLEDevice.h> #include <BLEDevice.h>
 #include <BLEServer.h> #include <BLEServer.h>
 #include <BLEUtils.h> #include <BLEUtils.h>
 #include <BLE2902.h> #include <BLE2902.h>
 + 
 BLECharacteristic *pCharacteristic; BLECharacteristic *pCharacteristic;
 bool deviceConnected = false; bool deviceConnected = false;
 + 
 #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
 #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
 #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
 + 
 class MyServerCallbacks: public BLEServerCallbacks { class MyServerCallbacks: public BLEServerCallbacks {
     void onConnect(BLEServer* pServer) { deviceConnected = true; Serial.println("connected"); };     void onConnect(BLEServer* pServer) { deviceConnected = true; Serial.println("connected"); };
     void onDisconnect(BLEServer* pServer) { deviceConnected = false; Serial.println("disconnected"); }     void onDisconnect(BLEServer* pServer) { deviceConnected = false; Serial.println("disconnected"); }
 }; };
 + 
 class MyCallbacks: public BLECharacteristicCallbacks { class MyCallbacks: public BLECharacteristicCallbacks {
     void onWrite(BLECharacteristic *pCharacteristic) {     void onWrite(BLECharacteristic *pCharacteristic) {
       std::string rxValue = pCharacteristic->getValue();       std::string rxValue = pCharacteristic->getValue();
-      if (rxValue.length() > 0)  +      for (int i = 0; i < rxValue.length(); i++) Serial.print(rxValue[i]);
-        for (int i = 0; i < rxValue.length(); i++) Serial.print(rxValue[i]);+
     }     }
 }; };
 + 
 void setup() { void setup() {
   Serial.begin(115200);   Serial.begin(115200);
Line 39: Line 42:
   Serial.println("Waiting a client connection to notify...");   Serial.println("Waiting a client connection to notify...");
 } }
 + 
 void loop() { void loop() {
   if (deviceConnected) {   if (deviceConnected) {
     if (Serial.available()) {     if (Serial.available()) {
-      char txString[2];  +      std::string s = ""; 
-      txString[0] = Serial.read(); +      while (Serial.available()) s += Serial.read(); 
-      txString[1] = 0; +      pCharacteristic->setValue(s.c_str()); 
-      pCharacteristic->setValue(txString); +      pCharacteristic->notify(); 
-      pCharacteristic->notify(); // Send the value to the app!+
     }     }
   }   }
-  delay(1); 
 } }
- 
 </code> </code>
esp32/ble_serial_v1.1569775246.txt.gz · Last modified: by aistis