-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32c6_arduino_cli.ino
119 lines (98 loc) · 3.17 KB
/
esp32c6_arduino_cli.ino
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLEBeacon.h>
#define DEVICE_NAME "ESP32C6"
#define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E"
#define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1"
#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D"
#define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5"
BLEServer *pServer;
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t value = 0;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
Serial.println("deviceConnected = true");
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
Serial.println("deviceConnected = false");
// Restart advertising to be visible and connectable again
BLEAdvertising *pAdvertising;
pAdvertising = pServer->getAdvertising();
pAdvertising->start();
Serial.println("iBeacon advertising restarted");
}
};
class MyCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
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 init_service() {
BLEAdvertising *pAdvertising;
pAdvertising = pServer->getAdvertising();
pAdvertising->stop();
// Create the BLE Service
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->addDescriptor(new BLE2902());
pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
// Start the service
pService->start();
pAdvertising->start();
}
void init_beacon() {
BLEAdvertising *pAdvertising;
pAdvertising = pServer->getAdvertising();
pAdvertising->stop();
// iBeacon
BLEBeacon myBeacon;
myBeacon.setManufacturerId(0x4c00);
myBeacon.setMajor(5);
myBeacon.setMinor(88);
myBeacon.setSignalPower(0xc5);
myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV));
BLEAdvertisementData advertisementData;
advertisementData.setFlags(0x1A);
advertisementData.setManufacturerData(myBeacon.getData());
pAdvertising->setAdvertisementData(advertisementData);
pAdvertising->start();
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Initializing...");
Serial.flush();
BLEDevice::init(DEVICE_NAME);
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
init_service();
init_beacon();
Serial.println("iBeacon + service defined and advertising!");
}
void loop() {
if (deviceConnected) {
Serial.printf("*** NOTIFY: %d ***\n", value);
pCharacteristic->setValue(&value, 1);
pCharacteristic->notify();
value++;
}
delay(2000);
}