55 lines
1.6 KiB
Arduino
55 lines
1.6 KiB
Arduino
#include <BLEDevice.h>
|
|
#include <BLEServer.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLE2902.h>
|
|
|
|
#define SERVICE_UUID "7cddf5af-453f-40fa-808e-37ae6ad8facd"
|
|
BLECharacteristic temphumidSense (BLEUUID("3383a686-a53c-42e4-a88c-e0e1de4c4bda"), BLECharacteristic::PROPERTY_READ
|
|
| BLECharacteristic::PROPERTY_NOTIFY
|
|
| BLECharacteristic::PROPERTY_INDICATE);
|
|
BLEDescriptor temphumidDescriptor(BLEUUID((uint16_t)0x2901));
|
|
|
|
bool clientConnected = false;
|
|
|
|
|
|
class MyServerCallbacks: public BLEServerCallbacks {
|
|
void onConnect (BLEServer * pServer) {
|
|
clientConnected = true;
|
|
};
|
|
void onDisconnect (BLEServer * pServer) {
|
|
clientConnected = false;
|
|
}
|
|
};
|
|
|
|
void setup () {
|
|
Serial.begin(115200);
|
|
BLEDevice::init("esp32-Server");
|
|
BLEServer *pServer = BLEDevice::createServer();
|
|
pServer->setCallbacks(new MyServerCallbacks());
|
|
|
|
BLEService *pSensor = pServer-> createService(SERVICE_UUID);
|
|
|
|
pSensor-> addCharacteristic(&temphumidSense);
|
|
temphumidDescriptor.setValue("In Degree Celcius");
|
|
temphumidSense.addDescriptor(&temphumidDescriptor);
|
|
temphumidSense.addDescriptor(new BLE2902());
|
|
|
|
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
|
|
pSensor-> start(); // Start the service
|
|
pServer-> getAdvertising()->start ();// Starts the discovery of ESP32
|
|
|
|
Serial.println("Waiting for a client to connect ...");
|
|
}
|
|
|
|
void loop () {
|
|
if (clientConnected) {
|
|
temphumidSense.setValue("1234");
|
|
temphumidSense.notify(); // Send the Temperature reading to the application!
|
|
Serial.println("Sent string");
|
|
}
|
|
else {
|
|
Serial.println("Not connected");
|
|
}
|
|
delay(2000);
|
|
}
|