/DevelSoutheastBeings
Created 1 year, 6 months ago...
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEClient.h>
#include <BLEAdvertisedDevice.h>
#include <SparkFun_OWire_Arduino_Library.h>
BLEClient* pClient;
bool connected = false;
BLEUUID customServiceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
BLEUUID customCharUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
OWIRE myLED;
static void ledNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
int value = *pData;
if (value == 0) {
myLED.setModeAndColor(OW_SOLID, OW_RED);
} else {
myLED.setModeAndColor(OW_SOLID, OW_GREEN);
}
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
connected = true;
myLED.setModeAndColor(OW_SOLID, OW_YELLOW);
}
void onDisconnect(BLEClient* pclient) {
ESP.restart();
}
};
void setup() {
Serial.begin(115200);
myLED.begin(4, false);
myLED.setModeAndColor(OW_SOLID, OW_BLUE);
BLEDevice::init("LED Client");
pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
while (!connected) {
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
BLEScanResults scanResults = pBLEScan->start(5);
for (int i = 0; i < scanResults.getCount(); i++) {
BLEAdvertisedDevice device = scanResults.getDevice(i);
if (device.haveServiceUUID() && device.isAdvertisingService(customServiceUUID)) {
pClient->connect(&device);
break;
}
}
if (!connected) {
Serial.println("Connection failed. Waiting 15 seconds before retrying.");
myLED.setModeAndColor(OW_SOLID, OW_CYAN);
delay(15000); // Wait for 15 seconds before retrying
myLED.setModeAndColor(OW_SOLID, OW_BLUE);
}
}
if (connected) {
BLERemoteService* pRemoteService = pClient->getService(customServiceUUID);
if (pRemoteService) {
BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(customCharUUID);
if (pRemoteCharacteristic) {
pRemoteCharacteristic->registerForNotify(ledNotifyCallback);
pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)"\x01", 1);
} else {
Serial.println("Custom characteristic not found.");
}
} else {
Serial.println("Custom service not found.");
}
}
}
void loop() {
// Nothing to do in the loop for this example
}