From 08c22d31a76776a39974a5d04a39f3a2dc050974 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Tue, 15 Apr 2025 14:37:59 +0200 Subject: tighten Bluetooth beacon routines --- sensor/sensor.ino | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'sensor/sensor.ino') diff --git a/sensor/sensor.ino b/sensor/sensor.ino index b9c13ab..f051b0b 100644 --- a/sensor/sensor.ino +++ b/sensor/sensor.ino @@ -63,9 +63,6 @@ #define LEDC_CALM_PACE 3000 #define LEDC_STRESSED_PACE 400 -// Bluetooth beacon -#define BEACON_POWER ESP_PWR_LVL_N12 - int stress = 0; bool touch_detected = false; @@ -75,6 +72,7 @@ int pace = LEDC_STRESSED_PACE; bool fade_ended = false; bool fade_in = true; +// pointer to control Bluetooth networking BLEAdvertising *pAdvertising; // Touch sensor callback @@ -148,21 +146,25 @@ int getGapeAngle() { return ledcRead(LED_PIN); } -// Construct Bluetooth beacon -void setBeacon(int angle) { +// Encode static Bluetooth beacon advertisement data +void setBeaconAdvertisement() { + BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); + oAdvertisementData.setName(BEACON_NAME); + pAdvertising->setAdvertisementData(oAdvertisementData); +} + +// Encode variable Bluetooth beacon service data +void setBeaconServiceData(int angle) { BLEEddystoneTLM EddystoneTLM; EddystoneTLM.setTemp(angle); log_i("Gape angle: %.2f°", EddystoneTLM.getTemp()); - BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); oScanResponseData.setServiceData(BLEUUID( (uint16_t)0xFEAA), String(EddystoneTLM.getData().c_str(), EddystoneTLM.getData().length())); - oAdvertisementData.setName(BEACON_NAME); - pAdvertising->setAdvertisementData(oAdvertisementData); pAdvertising->setScanResponseData(oScanResponseData); } @@ -171,21 +173,21 @@ void setup() { // enable logging to serial Serial.begin(115200); esp_log_level_set("*", ESP_LOG_DEBUG); + if (BEACON_UUID == "00000000-0000-0000-0000-000000000000") + Serial.println("Please set a deployment-wide unique BEACON_UUID"); beginPace(); beginTouchDetection(); // setup Bluetooth - if (BEACON_UUID == "00000000-0000-0000-0000-000000000000") - log_w("Please set a unique BEACON_UUID"); - BLEDevice::init("TLMBeacon"); - BLEDevice::setPower(BEACON_POWER); + BLEDevice::init(BEACON_NAME); pAdvertising = BLEDevice::getAdvertising(); - setBeacon(getGapeAngle()); + setBeaconAdvertisement(); + setBeaconServiceData(getGapeAngle()); pAdvertising->start(); } void loop() { - setBeacon(getGapeAngle()); + setBeaconServiceData(getGapeAngle()); delay(500); } -- cgit v1.2.3