diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-15 14:37:59 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-15 15:02:43 +0200 |
commit | 08c22d31a76776a39974a5d04a39f3a2dc050974 (patch) | |
tree | 5cf5d075cabb1ea42236a963259b184b66059d63 /sensor | |
parent | cf8a28dd7eec3b21c7719c70372b44fc31b422f7 (diff) |
tighten Bluetooth beacon routines
Diffstat (limited to 'sensor')
-rw-r--r-- | sensor/sensor.ino | 30 |
1 files changed, 16 insertions, 14 deletions
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); } |