Update MEMORY.md, MEMORY.md, bachelor-thesis-summary.md, and 76 more files
This commit is contained in:
commit
4426388063
79 files changed
+6169
No files matched your search
@@ -0,0 +1,124 @@
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
#include <BLEBeacon.h>
|
||||
#define DEVICE_NAME "ESP32 iBeacon"
|
||||
#define SERVICE_UUID "87b99b2c-9080-4d04-8f00-a5d9a3f9a0e7"
|
||||
#define BEACON_UUID "87b99b2c-9080-4d04-8f00-a5d9a3f9a0e7"
|
||||
#define BEACON_UUID_REV "e7a0f9a3-d9a5-008f-044d8090-2c9bb987"
|
||||
#define CHARACTERISTIC_UUID "87b99b2c-9080-4d04-8f00-a5d9a3f9a0e7"
|
||||
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) {
|
||||
|
||||
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 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(0x004C);
|
||||
myBeacon.setMajor(1);
|
||||
myBeacon.setMinor(3); // ESP32-ID: 1 to n
|
||||
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);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user