created bluetooth server
This commit is contained in:
parent
e2f57bad76
commit
87fd0f35f6
4 changed files with 130 additions and 5 deletions
92
src/ble/BluetoothServer.cpp
Normal file
92
src/ble/BluetoothServer.cpp
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLE2902.h>
|
||||
#include "BluetoothServer.h"
|
||||
|
||||
#define INTERNAL_LED_PIN 2
|
||||
#define BLE_SERVER_NAME "ClimateGO"
|
||||
#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"
|
||||
|
||||
//Setup callbacks onConnect and onDisconnect
|
||||
class ServerCallbacks : public BLEServerCallbacks {
|
||||
void onConnect(BLEServer *pServer) override {
|
||||
Serial.println("New device connected.");
|
||||
digitalWrite(INTERNAL_LED_PIN, HIGH);
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer *pServer) override {
|
||||
Serial.println("Device disconnected.");
|
||||
digitalWrite(INTERNAL_LED_PIN, LOW);
|
||||
//restart advertising on device disconnect
|
||||
pServer->getAdvertising()->start();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents the Bluetooth server that handles all interactions
|
||||
*/
|
||||
BluetoothServer::BluetoothServer() {
|
||||
// Create the BLE Device
|
||||
BLEDevice::init(BLE_SERVER_NAME);
|
||||
|
||||
// Create the BLE Server
|
||||
this->bleServer = BLEDevice::createServer();
|
||||
bleServer->setCallbacks(new ServerCallbacks());
|
||||
|
||||
// Create the BLE Service
|
||||
this->sensorService = bleServer->createService(SERVICE_UUID);
|
||||
|
||||
//create characteristics that will enable data sharing
|
||||
BLECharacteristic *temperatureCharacteristic = sensorService->createCharacteristic(
|
||||
TEMPERATURE_CHARACTERISTIC_UUID,
|
||||
BLECharacteristic::PROPERTY_READ
|
||||
);
|
||||
BLECharacteristic *pressureCharacteristic = sensorService->createCharacteristic(
|
||||
PRESSURE_CHARACTERISTIC_UUID,
|
||||
BLECharacteristic::PROPERTY_READ
|
||||
);
|
||||
|
||||
//set initial values
|
||||
temperatureCharacteristic->setValue("NA");
|
||||
pressureCharacteristic->setValue("NA");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to start the Bluetooth server and enable advertising to allow other devices to connect
|
||||
*/
|
||||
void BluetoothServer::startServer() {
|
||||
// Start the service
|
||||
this->sensorService->start();
|
||||
|
||||
// Start advertising
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
this->bleServer->getAdvertising()->start();
|
||||
Serial.println("Started BLE Server.");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Convert temperature to string somehow
|
||||
* Publish a new temperature to the appropriate characteristic
|
||||
* @param temperature the new temperature
|
||||
*/
|
||||
void BluetoothServer::setTemperature(float temperature) {
|
||||
sensorService
|
||||
->getCharacteristic(TEMPERATURE_CHARACTERISTIC_UUID)
|
||||
->setValue(temperature);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Convert pressure to string somehow
|
||||
* Publish a new pressure to the appropriate characteristic
|
||||
* @param pressure the new pressure
|
||||
*/
|
||||
void BluetoothServer::setPressure(float pressure) {
|
||||
sensorService
|
||||
->getCharacteristic(PRESSURE_CHARACTERISTIC_UUID)
|
||||
->setValue(pressure);
|
||||
}
|
||||
|
||||
|
||||
25
src/ble/BluetoothServer.h
Normal file
25
src/ble/BluetoothServer.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
||||
#define NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#define temperatureCelsius
|
||||
|
||||
class BluetoothServer {
|
||||
private:
|
||||
BLEService *sensorService;
|
||||
BLEServer *bleServer;
|
||||
public:
|
||||
BluetoothServer();
|
||||
|
||||
void startServer();
|
||||
void setTemperature(float temperature);
|
||||
void setPressure(float pressure);
|
||||
};
|
||||
|
||||
|
||||
#endif //NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
||||
15
src/main.cpp
15
src/main.cpp
|
|
@ -1,21 +1,28 @@
|
|||
#include <Arduino.h>
|
||||
#include <sensors/BmpSensor.h>
|
||||
#include <ble/BluetoothServer.h>
|
||||
|
||||
#define SLEEP_TIME 2
|
||||
#define BAUD_RATE 112500
|
||||
#define INTERNAL_LED_PIN 2
|
||||
|
||||
|
||||
Sensor *bmpSensor;
|
||||
BluetoothServer *server;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(BAUD_RATE);
|
||||
pinMode(INTERNAL_LED_PIN, OUTPUT);
|
||||
|
||||
bmpSensor = new BmpSensor();
|
||||
|
||||
server = new BluetoothServer();
|
||||
server->startServer();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sensor_data_t sample = ((BmpSensor *) bmpSensor)->sampleLowEnergy();
|
||||
Serial.printf("Temperature: %f | Pressure: %f \n",
|
||||
sample.temperature,
|
||||
sample.pressure
|
||||
);
|
||||
server->setPressure(sample.pressure);
|
||||
server->setTemperature(sample.temperature);
|
||||
delay(SLEEP_TIME * 1000);
|
||||
}
|
||||
|
|
@ -74,7 +74,8 @@ sensor_data_t BmpSensor::sample() {
|
|||
}
|
||||
|
||||
/**
|
||||
* Read a sample and put sensor to standby mode
|
||||
* Read a sample and put sensor to standby mode.
|
||||
* !!! Do not use this if sampling time is <= 500ms !!!
|
||||
* @return the sample read
|
||||
*/
|
||||
sensor_data_t BmpSensor::sampleLowEnergy() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue