From 0f0e64e344a8777c9300a7f4b4ce7812c756253c Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Tue, 25 Feb 2025 21:16:46 +0100 Subject: move arduino code to subdirs --- _iBeacon.c | 133 ------------------------------------------------ _mussel1.c | 144 ---------------------------------------------------- _mussel2.c | 144 ---------------------------------------------------- iBeacon/iBeacon.ino | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ mussel1/mussel1.ino | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mussel2/mussel2.ino | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 421 insertions(+), 421 deletions(-) delete mode 100644 _iBeacon.c delete mode 100644 _mussel1.c delete mode 100644 _mussel2.c create mode 100644 iBeacon/iBeacon.ino create mode 100644 mussel1/mussel1.ino create mode 100644 mussel2/mussel2.ino diff --git a/_iBeacon.c b/_iBeacon.c deleted file mode 100644 index d5db613..0000000 --- a/_iBeacon.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 - which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp -*/ - -/* - Create a BLE server that will send periodic iBeacon frames. - The design of creating the BLE server is: - 1. Create a BLE Server - 2. Create advertising data - 3. Start advertising. - 4. wait - 5. Stop advertising. -*/ -#include -#include -#include -#include -#include - -#define DEVICE_NAME "ESP32" -#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); -} diff --git a/_mussel1.c b/_mussel1.c deleted file mode 100644 index b8a959b..0000000 --- a/_mussel1.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 - which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp -*/ - -/* - Create a BLE server that will send periodic iBeacon frames. - The design of creating the BLE server is: - 1. Create a BLE Server - 2. Create advertising data - 3. Start advertising. - 4. wait - 5. Stop advertising. -*/ -#include -#include -#include -#include -#include - -#define DEVICE_NAME "Current second is below 50" -#define SERVICE_UUID "f9fde96b-d917-4877-ba4f-0813e69410c1" -#define BEACON_UUID "80e972d1-066f-4235-b44c-b6b681e63bf2" -#define BEACON_UUID_REV "ee4dadbb-6c32-48d5-9622-1d2dcc520154" -#define CHARACTERISTIC_UUID "c093f2ae-a81f-4683-b324-7a19f0f5e363" - -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.printf("iBeacon + service defined and advertising: %s", - DEVICE_NAME); -} - -void loop() { - if (deviceConnected) { - - // Get the current second (0-59) - unsigned long currentTime = millis(); - unsigned long totalSeconds = currentTime / 1000; - unsigned long currentSecond = totalSeconds % 60; - - String msg = "good"; - if (currentSecond >= 9) - msg = "bad"; - - Serial.printf("*** NOTIFY: %s ***\n", msg); - pCharacteristic->setValue(msg); - pCharacteristic->notify(); - value++; - } - delay(2000); -} diff --git a/_mussel2.c b/_mussel2.c deleted file mode 100644 index 2867777..0000000 --- a/_mussel2.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 - which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp -*/ - -/* - Create a BLE server that will send periodic iBeacon frames. - The design of creating the BLE server is: - 1. Create a BLE Server - 2. Create advertising data - 3. Start advertising. - 4. wait - 5. Stop advertising. -*/ -#include -#include -#include -#include -#include - -#define DEVICE_NAME "Cycles of five good then one bad" -#define SERVICE_UUID "32eed4b3-e233-4808-a120-c4cee1a8e0c5" -#define BEACON_UUID "84b7f168-a742-49b7-bd15-dc808dddb672" -#define BEACON_UUID_REV "87a3c0f9-695f-4c04-8195-3a89d6cd44f0" -#define CHARACTERISTIC_UUID "738e7085-9717-4288-ab21-c59ff7c5dd96" - -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.printf("iBeacon + service defined and advertising: %s", - DEVICE_NAME); -} - -void loop() { - if (deviceConnected) { - - // Get second-long cyclic counter 0-11 - unsigned long currentTime = millis(); - unsigned long totalSeconds = currentTime / 1000; - unsigned long currentSecond = totalSeconds % 12; - - String msg = "good"; - if (currentSecond >= 9) - msg = "bad"; - - Serial.printf("*** NOTIFY: %s ***\n", msg); - pCharacteristic->setValue(msg); - pCharacteristic->notify(); - value++; - } - delay(2000); -} diff --git a/iBeacon/iBeacon.ino b/iBeacon/iBeacon.ino new file mode 100644 index 0000000..d5db613 --- /dev/null +++ b/iBeacon/iBeacon.ino @@ -0,0 +1,133 @@ +/* + Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 + which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp +*/ + +/* + Create a BLE server that will send periodic iBeacon frames. + The design of creating the BLE server is: + 1. Create a BLE Server + 2. Create advertising data + 3. Start advertising. + 4. wait + 5. Stop advertising. +*/ +#include +#include +#include +#include +#include + +#define DEVICE_NAME "ESP32" +#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); +} diff --git a/mussel1/mussel1.ino b/mussel1/mussel1.ino new file mode 100644 index 0000000..b8a959b --- /dev/null +++ b/mussel1/mussel1.ino @@ -0,0 +1,144 @@ +/* + Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 + which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp +*/ + +/* + Create a BLE server that will send periodic iBeacon frames. + The design of creating the BLE server is: + 1. Create a BLE Server + 2. Create advertising data + 3. Start advertising. + 4. wait + 5. Stop advertising. +*/ +#include +#include +#include +#include +#include + +#define DEVICE_NAME "Current second is below 50" +#define SERVICE_UUID "f9fde96b-d917-4877-ba4f-0813e69410c1" +#define BEACON_UUID "80e972d1-066f-4235-b44c-b6b681e63bf2" +#define BEACON_UUID_REV "ee4dadbb-6c32-48d5-9622-1d2dcc520154" +#define CHARACTERISTIC_UUID "c093f2ae-a81f-4683-b324-7a19f0f5e363" + +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.printf("iBeacon + service defined and advertising: %s", + DEVICE_NAME); +} + +void loop() { + if (deviceConnected) { + + // Get the current second (0-59) + unsigned long currentTime = millis(); + unsigned long totalSeconds = currentTime / 1000; + unsigned long currentSecond = totalSeconds % 60; + + String msg = "good"; + if (currentSecond >= 9) + msg = "bad"; + + Serial.printf("*** NOTIFY: %s ***\n", msg); + pCharacteristic->setValue(msg); + pCharacteristic->notify(); + value++; + } + delay(2000); +} diff --git a/mussel2/mussel2.ino b/mussel2/mussel2.ino new file mode 100644 index 0000000..2867777 --- /dev/null +++ b/mussel2/mussel2.ino @@ -0,0 +1,144 @@ +/* + Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 + which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp +*/ + +/* + Create a BLE server that will send periodic iBeacon frames. + The design of creating the BLE server is: + 1. Create a BLE Server + 2. Create advertising data + 3. Start advertising. + 4. wait + 5. Stop advertising. +*/ +#include +#include +#include +#include +#include + +#define DEVICE_NAME "Cycles of five good then one bad" +#define SERVICE_UUID "32eed4b3-e233-4808-a120-c4cee1a8e0c5" +#define BEACON_UUID "84b7f168-a742-49b7-bd15-dc808dddb672" +#define BEACON_UUID_REV "87a3c0f9-695f-4c04-8195-3a89d6cd44f0" +#define CHARACTERISTIC_UUID "738e7085-9717-4288-ab21-c59ff7c5dd96" + +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.printf("iBeacon + service defined and advertising: %s", + DEVICE_NAME); +} + +void loop() { + if (deviceConnected) { + + // Get second-long cyclic counter 0-11 + unsigned long currentTime = millis(); + unsigned long totalSeconds = currentTime / 1000; + unsigned long currentSecond = totalSeconds % 12; + + String msg = "good"; + if (currentSecond >= 9) + msg = "bad"; + + Serial.printf("*** NOTIFY: %s ***\n", msg); + pCharacteristic->setValue(msg); + pCharacteristic->notify(); + value++; + } + delay(2000); +} -- cgit v1.2.3