implemented make discoverable button
This commit is contained in:
parent
2ef91e2810
commit
361b092d4e
3 changed files with 44 additions and 1 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -2,13 +2,20 @@
|
|||
#include <sensors/BmpSensor.h>
|
||||
#include <ble/BluetoothServer.h>
|
||||
|
||||
#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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue