Last active
March 11, 2018 16:40
-
-
Save kosso/bde36b334a084a03bd5e9f59cbd340eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BLEDevice.h> | |
#include <BLEServer.h> | |
#include <BLEUtils.h> | |
#include <BLE2902.h> | |
#include <esp_heap_caps.h> | |
#include <esp_heap_trace.h> | |
BLECharacteristic *pCharacteristic; | |
bool deviceConnected = false; | |
uint8_t txValue = 0; | |
// See the following for generating UUIDs: | |
// https://www.uuidgenerator.net/ | |
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // 'UART service' UUID | |
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" | |
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" | |
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]); | |
Serial.println(); | |
Serial.println("*********"); | |
} | |
} | |
}; | |
void setup() { | |
Serial.begin(115200); | |
// Create the BLE Device | |
BLEDevice::init("UART Service"); | |
// 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 | |
pServer->getAdvertising()->start(); | |
Serial.println("Waiting a client connection to notify..."); | |
} | |
int val = 0; | |
void loop() { | |
if (deviceConnected) { | |
Serial.printf("*** Sent Value: %d ***\n", txValue); | |
String str = "hello world " + String(val); | |
pCharacteristic->setValue((uint8_t *)str.c_str(),(size_t)str.length()); | |
pCharacteristic->notify(); | |
txValue++; | |
val++; | |
if(val == 255){ | |
val = 0; | |
} | |
} | |
// Serial.println(xPortGetFreeHeapSize()); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment