Created
April 7, 2020 10:36
-
-
Save gresan-gits/612623db154414b7caa5a23af6a0affb to your computer and use it in GitHub Desktop.
ESP32 BLE GPIO
This file contains hidden or 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> | |
BLEServer* pServer = NULL; | |
BLECharacteristic* pCharacteristic = NULL; | |
bool deviceConnected = false; | |
bool oldDeviceConnected = false; | |
uint32_t value = 0; | |
//Clone HM10 | |
#define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb" | |
#define CHARACTERISTIC_UUID "0000ffe1-0000-1000-8000-00805f9b34fb" | |
// | |
const int ledPin = GPIO_NUM_2; | |
String inputString = ""; // a String to hold incoming data | |
bool stringComplete = false; // whether the string is complete | |
bool ledOn = false; // whether the string is complete | |
class ServerCallbacks: public BLEServerCallbacks { | |
void onConnect(BLEServer* pServer) { | |
deviceConnected = true; | |
BLEDevice::startAdvertising(); | |
}; | |
void onDisconnect(BLEServer* pServer) { | |
deviceConnected = false; | |
} | |
}; | |
class WriteCallbacks: public BLECharacteristicCallbacks { | |
void onWrite(BLECharacteristic *pCharacteristic) { | |
std::string rxValue = pCharacteristic->getValue(); | |
if (rxValue.length() > 0) { | |
for (int i = 0; i < rxValue.length(); i++) | |
{ | |
char inChar = (char)rxValue[i]; | |
// add it to the inputString: | |
inputString += inChar; | |
// if the incoming character is a newline, set a flag so the main loop can | |
// do something about it: | |
if (inChar == '\n') { | |
stringComplete = true; | |
} | |
} | |
} | |
} | |
}; | |
void blePrint(std::string value) { | |
pCharacteristic->setValue(value); | |
pCharacteristic->notify(); | |
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms | |
} | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
Serial.begin(115200); | |
// Create the BLE Device | |
BLEDevice::init("ESP32BLE"); | |
// Create the BLE Server | |
pServer = BLEDevice::createServer(); | |
pServer->setCallbacks(new ServerCallbacks()); | |
// Create the BLE Service | |
BLEService *pService = pServer->createService(SERVICE_UUID); | |
// Create a BLE Characteristic | |
pCharacteristic = pService->createCharacteristic( | |
CHARACTERISTIC_UUID, | |
BLECharacteristic::PROPERTY_READ | | |
BLECharacteristic::PROPERTY_WRITE | | |
BLECharacteristic::PROPERTY_NOTIFY | |
); | |
// 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 WriteCallbacks()); | |
// Start the service | |
pService->start(); | |
// Start advertising | |
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | |
pAdvertising->addServiceUUID(SERVICE_UUID); | |
pAdvertising->setScanResponse(false); | |
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter | |
BLEDevice::startAdvertising(); | |
Serial.println("Waiting a client connection to notify..."); | |
} | |
void loop() { | |
if (stringComplete) { | |
inputString.trim(); | |
Serial.println(inputString); | |
if (inputString == "ON") { | |
digitalWrite(ledPin, HIGH); | |
blePrint("ON\r\n"); | |
ledOn = true; | |
} | |
else if (inputString == "OFF") { | |
digitalWrite(ledPin, LOW); | |
blePrint("OFF\r\n"); | |
ledOn = false; | |
} | |
else if (inputString == "Sync") { | |
if (ledOn) | |
blePrint("ON\r\n"); | |
else | |
blePrint("OFF\r\n"); | |
} | |
// clear the string: | |
inputString = ""; | |
stringComplete = false; | |
delay(20);//Time for Bluetooth stack handle | |
} | |
// disconnecting | |
if (!deviceConnected && oldDeviceConnected) { | |
delay(500); // give the bluetooth stack the chance to get things ready | |
pServer->startAdvertising(); // restart advertising | |
Serial.println("start advertising"); | |
oldDeviceConnected = deviceConnected; | |
} | |
// connecting | |
if (deviceConnected && !oldDeviceConnected) { | |
// do stuff here on connecting | |
oldDeviceConnected = deviceConnected; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment