From 361b092d4e63cdcf7480e243e5e4d77b92fdf4ef Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Wed, 9 Feb 2022 20:22:55 +0100 Subject: [PATCH] implemented make discoverable button --- firmware/src/ble/BluetoothServer.cpp | 23 +++++++++++++++++++++++ firmware/src/ble/BluetoothServer.h | 2 ++ firmware/src/main.cpp | 20 +++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/firmware/src/ble/BluetoothServer.cpp b/firmware/src/ble/BluetoothServer.cpp index 4586e42..5c67cae 100644 --- a/firmware/src/ble/BluetoothServer.cpp +++ b/firmware/src/ble/BluetoothServer.cpp @@ -8,6 +8,8 @@ #define SERVICE_UUID "2150123c-af53-4038-bc92-ba3d0870a9e4" #define TEMPERATURE_CHARACTERISTIC_UUID "cba1d466-344c-4be3-ab3f-189f80dd7518" #define PRESSURE_CHARACTERISTIC_UUID "ca73b3ba-39f6-4ab3-91ae-186dc9577d99" +#define BLINK_TIME_DELAY_MS 500 +#define BLINK_TIMES 5 //Setup callbacks onConnect and onDisconnect class ServerCallbacks : public BLEServerCallbacks { @@ -51,6 +53,9 @@ BluetoothServer::BluetoothServer() { //set initial values temperatureCharacteristic->setValue("NA"); pressureCharacteristic->setValue("NA"); + + //allow advertiser function to be called + this->advertiserLock = false; } /** @@ -89,4 +94,22 @@ void BluetoothServer::setPressure(float pressure) { ->setValue(pressure); } +/** + * This method will start advertising via Bluetooth and blink the LED + */ +void BluetoothServer::startAdvertising() { + if (!this->advertiserLock) { + this->advertiserLock = true; + this->bleServer->getAdvertising()->start(); + Serial.println("Starting Bluetooth advertising..."); + for (int i = 0; i < BLINK_TIMES; ++i) { + digitalWrite(INTERNAL_LED_PIN, HIGH); + delay(BLINK_TIME_DELAY_MS); + digitalWrite(INTERNAL_LED_PIN, LOW); + delay(BLINK_TIME_DELAY_MS); + } + this->advertiserLock = false; + } +} + diff --git a/firmware/src/ble/BluetoothServer.h b/firmware/src/ble/BluetoothServer.h index 4afd1ec..ecbe38f 100644 --- a/firmware/src/ble/BluetoothServer.h +++ b/firmware/src/ble/BluetoothServer.h @@ -13,9 +13,11 @@ class BluetoothServer { private: BLEService *sensorService; BLEServer *bleServer; + bool advertiserLock; public: BluetoothServer(); + void startAdvertising(); void startServer(); void setTemperature(float temperature); void setPressure(float pressure); diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 38b5852..fe9f32d 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -2,13 +2,20 @@ #include #include -#define SLEEP_TIME 2 +#define SLEEP_TIME 1 #define BAUD_RATE 112500 #define INTERNAL_LED_PIN 2 +#define INTERRUPT_MAKE_DISCOVERABLE_PIN 15 Sensor *bmpSensor; BluetoothServer *server; +bool makeDiscoverable = false; + + +void IRAM_ATTR startBleServerAdvertising() { + makeDiscoverable = true; +} void setup() { Serial.begin(BAUD_RATE); @@ -18,9 +25,20 @@ void setup() { server = new BluetoothServer(); server->startServer(); + + //set up Interrupt to enable make discoverable button + pinMode(INTERRUPT_MAKE_DISCOVERABLE_PIN, INPUT_PULLDOWN); + attachInterrupt(digitalPinToInterrupt(INTERRUPT_MAKE_DISCOVERABLE_PIN), + startBleServerAdvertising, + RISING); } void loop() { + if (makeDiscoverable) { + server->startAdvertising(); + makeDiscoverable = false; + } + sensor_data_t sample = ((BmpSensor *) bmpSensor)->sampleLowEnergy(); server->setPressure(sample.pressure); server->setTemperature(sample.temperature);