diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-03-04 11:04:57 +0100 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-03-04 11:04:57 +0100 |
commit | 35d5353892fa72a8a8c7b107ca39d9525b2d01e8 (patch) | |
tree | a82126239fc0835960df264396a90614e8b1c42b /mussel2 | |
parent | 30b2e89784d0ffff2ba4942a678363075dc88a0a (diff) |
drop example code frmo mussel1 and mussel2
Diffstat (limited to 'mussel2')
-rw-r--r-- | mussel2/mussel2.ino | 126 |
1 files changed, 3 insertions, 123 deletions
diff --git a/mussel2/mussel2.ino b/mussel2/mussel2.ino index d7ad816..a82fa2c 100644 --- a/mussel2/mussel2.ino +++ b/mussel2/mussel2.ino @@ -1,117 +1,9 @@ /// mussel2 - mussel voter influenced by time /// /// SPDX-License-Identifier: GPL-3.0-or-later -/// SPDX-FileCopyrightText: 2015-2023 Espressif Systems /// SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk> -/// -/// Based on example `libraries/BLE/examples/iBeacon/iBeacon.ino` -/// in project <https://github.com/espressif/arduino-esp32/>. - -/* - 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 <BLEDevice.h> -#include <BLEServer.h> -#include <BLEUtils.h> -#include <BLE2902.h> -#include <BLEBeacon.h> #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); @@ -119,20 +11,11 @@ void setup() { 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", + Serial.printf("Device ready: %s", DEVICE_NAME); } void loop() { - if (deviceConnected) { - // Get second-long cyclic counter 0-11 unsigned long currentTime = millis(); unsigned long totalSeconds = currentTime / 1000; @@ -143,9 +26,6 @@ void loop() { msg = "bad"; Serial.printf("*** NOTIFY: %s ***\n", msg); - pCharacteristic->setValue(msg); - pCharacteristic->notify(); - value++; - } - delay(2000); + + delay(2000); } |