From 7c3eaeffc4b97dbff2c8ec8493b9a731df16527d Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sat, 5 Feb 2022 16:44:23 +0100 Subject: [PATCH 1/6] initial firmware project creation --- .gitignore | 7 +++++++ .idea/.gitignore | 8 ++++++++ .idea/.name | 1 + .idea/misc.xml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/untitled.iml | 2 ++ .idea/vcs.xml | 6 ++++++ CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ include/README | 39 +++++++++++++++++++++++++++++++++++++++ lib/README | 46 ++++++++++++++++++++++++++++++++++++++++++++++ platformio.ini | 14 ++++++++++++++ src/main.cpp | 8 ++++++++ test/README | 11 +++++++++++ 13 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/untitled.iml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..355a40d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.pio +CMakeListsPrivate.txt +cmake-build-*/ + + +# IDE +./.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..64f7f1b --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +untitled \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a54a8fc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..aeb7613 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/untitled.iml b/.idea/untitled.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/untitled.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7967a4d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +# !!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE +# https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags +# +# If you need to override existing CMake configuration or add extra, +# please create `CMakeListsUser.txt` in the root of project. +# The `CMakeListsUser.txt` will not be overwritten by PlatformIO. + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_C_COMPILER_WORKS 1) +set(CMAKE_CXX_COMPILER_WORKS 1) + +project("untitled" C CXX) + +include(CMakeListsPrivate.txt) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt) +include(CMakeListsUser.txt) +endif() + +add_custom_target( + Production ALL + COMMAND platformio -c clion run "$<$>:-e${CMAKE_BUILD_TYPE}>" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_custom_target( + Debug ALL + COMMAND platformio -c clion debug "$<$>:-e${CMAKE_BUILD_TYPE}>" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_executable(Z_DUMMY_TARGET ${SRC_LIST}) diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..4b30716 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e55b7e4 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,8 @@ +#include +void setup() { +// write your initialization code here +} + +void loop() { +// write your code here +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html From ad8b07167b75a7d0110f853e16e89fcc13d59f3e Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sat, 5 Feb 2022 19:08:41 +0100 Subject: [PATCH 2/6] basic code for reading pressure and temperatre sensor --- .idea/.name | 1 - CMakeLists.txt | 2 +- platformio.ini | 1 + src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 4 deletions(-) delete mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 64f7f1b..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -untitled \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7967a4d..b6ad71a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_C_COMPILER_WORKS 1) set(CMAKE_CXX_COMPILER_WORKS 1) -project("untitled" C CXX) +project("new_climte_go" C CXX) include(CMakeListsPrivate.txt) diff --git a/platformio.ini b/platformio.ini index 4b30716..1a0090b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,3 +12,4 @@ platform = espressif32 board = esp32dev framework = arduino +lib_deps = adafruit/Adafruit BMP280 Library@^2.6.1 diff --git a/src/main.cpp b/src/main.cpp index e55b7e4..3cafe30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,44 @@ #include +#include +#include + +Adafruit_BMP280 bmp; // use I2C interface +Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor(); +Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor(); + void setup() { -// write your initialization code here + Serial.begin(112500); + while ( !Serial ) delay(100); // wait for native usb + + unsigned status; + status = bmp.begin(0x76); //set the correct I2C port + + //query status and reboot the board if no sensor is detected + if (!status) { + ESP.restart(); + } + + /* Default settings from datasheet. */ + bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ + Adafruit_BMP280::FILTER_X16, /* Filtering. */ + Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ + + bmp_temp->printSensorDetails(); } void loop() { -// write your code here + sensors_event_t temp_event, pressure_event; + bmp_temp->getEvent(&temp_event); + bmp_pressure->getEvent(&pressure_event); + + Serial.print(F("Temperature = ")); + Serial.print(temp_event.temperature); + Serial.print(" *C | "); + + Serial.print(F("Pressure = ")); + Serial.print(pressure_event.pressure); + Serial.println(" hPa"); + delay(100); } \ No newline at end of file From db1750ef53a4f5d1db1d549f34a21da32a1de1ff Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sun, 6 Feb 2022 12:32:49 +0100 Subject: [PATCH 3/6] restructured classes to provide sensor interface --- src/main.cpp | 44 +++++---------------------- src/sensors/BmpSensor.cpp | 64 +++++++++++++++++++++++++++++++++++++++ src/sensors/BmpSensor.h | 28 +++++++++++++++++ src/sensors/Sensor.h | 23 ++++++++++++++ 4 files changed, 123 insertions(+), 36 deletions(-) create mode 100644 src/sensors/BmpSensor.cpp create mode 100644 src/sensors/BmpSensor.h create mode 100644 src/sensors/Sensor.h diff --git a/src/main.cpp b/src/main.cpp index 3cafe30..832d5b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,44 +1,16 @@ #include -#include -#include +#include -Adafruit_BMP280 bmp; // use I2C interface -Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor(); -Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor(); +#define SLEEP_TIME 2 + +Sensor *bmpSensor; void setup() { - Serial.begin(112500); - while ( !Serial ) delay(100); // wait for native usb - - unsigned status; - status = bmp.begin(0x76); //set the correct I2C port - - //query status and reboot the board if no sensor is detected - if (!status) { - ESP.restart(); - } - - /* Default settings from datasheet. */ - bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ - Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ - Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ - Adafruit_BMP280::FILTER_X16, /* Filtering. */ - Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ - - bmp_temp->printSensorDetails(); + bmpSensor = new BmpSensor(); } void loop() { - sensors_event_t temp_event, pressure_event; - bmp_temp->getEvent(&temp_event); - bmp_pressure->getEvent(&pressure_event); - - Serial.print(F("Temperature = ")); - Serial.print(temp_event.temperature); - Serial.print(" *C | "); - - Serial.print(F("Pressure = ")); - Serial.print(pressure_event.pressure); - Serial.println(" hPa"); - delay(100); + bmpSensor->sample(); + bmpSensor->enableStandbyMode(); + delay(SLEEP_TIME * 1000); } \ No newline at end of file diff --git a/src/sensors/BmpSensor.cpp b/src/sensors/BmpSensor.cpp new file mode 100644 index 0000000..a5d2814 --- /dev/null +++ b/src/sensors/BmpSensor.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include "BmpSensor.h" + +BmpSensor::BmpSensor() { + this->BmpSensor::sensor_setup(); + this->state = SensorState::ASLEEP; +} + +void BmpSensor::sensor_setup() { + Serial.begin(112500); + while (!Serial) delay(100); // wait for native usb + + unsigned status; + status = this->bmp.begin(0x76); //set the correct I2C port + + //query status and reboot the board if no sensor is detected + if (!status) { + ESP.restart(); + + } + +/* Default settings from datasheet. */ + this->enableStandbyMode(); +} + +void BmpSensor::wakeUp() { + this->bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ + Adafruit_BMP280::FILTER_X16, /* Filtering. */ + Adafruit_BMP280::STANDBY_MS_500 /* Standby time. */ + ); +} + +void BmpSensor::enableStandbyMode() { + this->bmp.setSampling(Adafruit_BMP280::MODE_SLEEP, /* Operating Mode. */ + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ + Adafruit_BMP280::FILTER_X16, /* Filtering. */ + Adafruit_BMP280::STANDBY_MS_500 /* Standby time. */ + ); +} + +void BmpSensor::sample() { + //wake sensor up if it is in standby mode + if (this->state == SensorState::ASLEEP) + this->wakeUp(); + + sensors_event_t temp_event, pressure_event; + this->bmp_temp->getEvent(&temp_event); + bmp_pressure->getEvent(&pressure_event); + + Serial.print(F("Temperature = ")); + Serial.print(temp_event.temperature); + Serial.print(" °C | "); + + Serial.print(F("Pressure = ")); + Serial.print(pressure_event.pressure); + Serial.println(" hPa"); +} + + diff --git a/src/sensors/BmpSensor.h b/src/sensors/BmpSensor.h new file mode 100644 index 0000000..b99084b --- /dev/null +++ b/src/sensors/BmpSensor.h @@ -0,0 +1,28 @@ +#ifndef NEW_CLIMTE_GO_BMPSENSOR_H +#define NEW_CLIMTE_GO_BMPSENSOR_H + +#include "Sensor.h" +#include "Adafruit_BMP280.h" + +class BmpSensor : public Sensor { +private: + Adafruit_BMP280 bmp; // use I2C interface + Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor(); + Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor(); + +public: + BmpSensor(); + + void sample() override; + + void enableStandbyMode() override; + + void wakeUp() override; + +protected: + void sensor_setup() override; + +}; + +#endif //NEW_CLIMTE_GO_BMPSENSOR_H + diff --git a/src/sensors/Sensor.h b/src/sensors/Sensor.h new file mode 100644 index 0000000..ea15fc9 --- /dev/null +++ b/src/sensors/Sensor.h @@ -0,0 +1,23 @@ +#ifndef NEW_CLIMTE_GO_SENSOR_H +#define NEW_CLIMTE_GO_SENSOR_H + +enum class SensorState : unsigned short { + AWAKE, ASLEEP +}; + +class Sensor { +public: + virtual void sample() = 0; + + virtual void wakeUp() = 0; + + virtual void enableStandbyMode() = 0; + +protected: + SensorState state = SensorState::ASLEEP; + + virtual void sensor_setup() = 0; + +}; + +#endif //NEW_CLIMTE_GO_SENSOR_H From e2f57bad7690cac8022a72b1852bc4d11949616f Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sun, 6 Feb 2022 13:04:01 +0100 Subject: [PATCH 4/6] comments and low energy sampling --- src/main.cpp | 9 +++++-- src/sensors/BmpSensor.cpp | 54 ++++++++++++++++++++++++++----------- src/sensors/BmpSensor.h | 3 ++- src/sensors/Sensor.h | 4 ++- src/sensors/sensor_data_t.h | 16 +++++++++++ 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 src/sensors/sensor_data_t.h diff --git a/src/main.cpp b/src/main.cpp index 832d5b9..5dad5cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,15 +2,20 @@ #include #define SLEEP_TIME 2 +#define BAUD_RATE 112500 Sensor *bmpSensor; void setup() { + Serial.begin(BAUD_RATE); bmpSensor = new BmpSensor(); } void loop() { - bmpSensor->sample(); - bmpSensor->enableStandbyMode(); + sensor_data_t sample = ((BmpSensor *) bmpSensor)->sampleLowEnergy(); + Serial.printf("Temperature: %f | Pressure: %f \n", + sample.temperature, + sample.pressure + ); delay(SLEEP_TIME * 1000); } \ No newline at end of file diff --git a/src/sensors/BmpSensor.cpp b/src/sensors/BmpSensor.cpp index a5d2814..97dd6fb 100644 --- a/src/sensors/BmpSensor.cpp +++ b/src/sensors/BmpSensor.cpp @@ -3,28 +3,33 @@ #include #include "BmpSensor.h" +/** + * Creates a new BMP Sensor and initializes it + */ BmpSensor::BmpSensor() { this->BmpSensor::sensor_setup(); this->state = SensorState::ASLEEP; } +/** + * Function responsible for setting up the sensor and the I2C connection + */ void BmpSensor::sensor_setup() { - Serial.begin(112500); while (!Serial) delay(100); // wait for native usb - unsigned status; - status = this->bmp.begin(0x76); //set the correct I2C port + unsigned status = this->bmp.begin(0x76); //set the correct I2C port //query status and reboot the board if no sensor is detected - if (!status) { + if (!status) ESP.restart(); - } - -/* Default settings from datasheet. */ + //put sensor to standby mode this->enableStandbyMode(); } +/** + * Function to wake sensor up from standby mode + */ void BmpSensor::wakeUp() { this->bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ @@ -34,6 +39,9 @@ void BmpSensor::wakeUp() { ); } +/** + * Function to put sensor to standby mode + */ void BmpSensor::enableStandbyMode() { this->bmp.setSampling(Adafruit_BMP280::MODE_SLEEP, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ @@ -43,22 +51,38 @@ void BmpSensor::enableStandbyMode() { ); } -void BmpSensor::sample() { +/** + * read a sample from the sensor + * @return a struct containing all necessary sensor data + */ +sensor_data_t BmpSensor::sample() { //wake sensor up if it is in standby mode if (this->state == SensorState::ASLEEP) this->wakeUp(); + //sample pressure and temperature sensors_event_t temp_event, pressure_event; this->bmp_temp->getEvent(&temp_event); - bmp_pressure->getEvent(&pressure_event); + this->bmp_pressure->getEvent(&pressure_event); - Serial.print(F("Temperature = ")); - Serial.print(temp_event.temperature); - Serial.print(" °C | "); - Serial.print(F("Pressure = ")); - Serial.print(pressure_event.pressure); - Serial.println(" hPa"); + return sensor_data_t + { + temp_event.temperature, + pressure_event.pressure + }; +} + +/** + * Read a sample and put sensor to standby mode + * @return the sample read + */ +sensor_data_t BmpSensor::sampleLowEnergy() { + //read sample + sensor_data_t sampledData = this->sample(); + //put sensor to standby + this->enableStandbyMode(); + return sampledData; } diff --git a/src/sensors/BmpSensor.h b/src/sensors/BmpSensor.h index b99084b..f5949fc 100644 --- a/src/sensors/BmpSensor.h +++ b/src/sensors/BmpSensor.h @@ -13,7 +13,8 @@ private: public: BmpSensor(); - void sample() override; + sensor_data_t sample() override; + sensor_data_t sampleLowEnergy(); void enableStandbyMode() override; diff --git a/src/sensors/Sensor.h b/src/sensors/Sensor.h index ea15fc9..7f3a1f8 100644 --- a/src/sensors/Sensor.h +++ b/src/sensors/Sensor.h @@ -1,13 +1,15 @@ #ifndef NEW_CLIMTE_GO_SENSOR_H #define NEW_CLIMTE_GO_SENSOR_H +#include + enum class SensorState : unsigned short { AWAKE, ASLEEP }; class Sensor { public: - virtual void sample() = 0; + virtual sensor_data_t sample() = 0; virtual void wakeUp() = 0; diff --git a/src/sensors/sensor_data_t.h b/src/sensors/sensor_data_t.h new file mode 100644 index 0000000..0d96e8d --- /dev/null +++ b/src/sensors/sensor_data_t.h @@ -0,0 +1,16 @@ +#ifndef NEW_CLIMTE_GO_SENSOR_DATA_T_H +#define NEW_CLIMTE_GO_SENSOR_DATA_T_H + +#include + +/** + * This struct represents data read from any sensor. + * This implies, reading temperature from e.g. the CO2 Sensor + * does not make any sense + */ +struct sensor_data_t { + float temperature; + float pressure; +}; + +#endif From 87fd0f35f68565dffcdf5445fcbefc68f9f59423 Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sun, 6 Feb 2022 19:30:51 +0100 Subject: [PATCH 5/6] created bluetooth server --- src/ble/BluetoothServer.cpp | 92 +++++++++++++++++++++++++++++++++++++ src/ble/BluetoothServer.h | 25 ++++++++++ src/main.cpp | 15 ++++-- src/sensors/BmpSensor.cpp | 3 +- 4 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/ble/BluetoothServer.cpp create mode 100644 src/ble/BluetoothServer.h diff --git a/src/ble/BluetoothServer.cpp b/src/ble/BluetoothServer.cpp new file mode 100644 index 0000000..4586e42 --- /dev/null +++ b/src/ble/BluetoothServer.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#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); +} + + diff --git a/src/ble/BluetoothServer.h b/src/ble/BluetoothServer.h new file mode 100644 index 0000000..4afd1ec --- /dev/null +++ b/src/ble/BluetoothServer.h @@ -0,0 +1,25 @@ +#ifndef NEW_CLIMTE_GO_BLUETOOTHSERVER_H +#define NEW_CLIMTE_GO_BLUETOOTHSERVER_H + +#include +#include +#include +#include +#include + +#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 diff --git a/src/main.cpp b/src/main.cpp index 5dad5cb..38b5852 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,28 @@ #include #include +#include #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); } \ No newline at end of file diff --git a/src/sensors/BmpSensor.cpp b/src/sensors/BmpSensor.cpp index 97dd6fb..7f68a25 100644 --- a/src/sensors/BmpSensor.cpp +++ b/src/sensors/BmpSensor.cpp @@ -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() { From 7515726a8ff1e6644372bac4ddcee06cf437d219 Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sun, 6 Feb 2022 19:38:01 +0100 Subject: [PATCH 6/6] restructured project --- .idea/misc.xml | 5 ++++- README.md | 1 + CMakeLists.txt => firmware/CMakeLists.txt | 0 {include => firmware/include}/README | 0 {lib => firmware/lib}/README | 0 platformio.ini => firmware/platformio.ini | 0 {src => firmware/src}/ble/BluetoothServer.cpp | 0 {src => firmware/src}/ble/BluetoothServer.h | 0 {src => firmware/src}/main.cpp | 0 {src => firmware/src}/sensors/BmpSensor.cpp | 0 {src => firmware/src}/sensors/BmpSensor.h | 0 {src => firmware/src}/sensors/Sensor.h | 0 {src => firmware/src}/sensors/sensor_data_t.h | 0 {test => firmware/test}/README | 0 14 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 README.md rename CMakeLists.txt => firmware/CMakeLists.txt (100%) rename {include => firmware/include}/README (100%) rename {lib => firmware/lib}/README (100%) rename platformio.ini => firmware/platformio.ini (100%) rename {src => firmware/src}/ble/BluetoothServer.cpp (100%) rename {src => firmware/src}/ble/BluetoothServer.h (100%) rename {src => firmware/src}/main.cpp (100%) rename {src => firmware/src}/sensors/BmpSensor.cpp (100%) rename {src => firmware/src}/sensors/BmpSensor.h (100%) rename {src => firmware/src}/sensors/Sensor.h (100%) rename {src => firmware/src}/sensors/sensor_data_t.h (100%) rename {test => firmware/test}/README (100%) diff --git a/.idea/misc.xml b/.idea/misc.xml index a54a8fc..80cd00a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,11 @@ - + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ef6ea8d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +#Climate GO \ No newline at end of file diff --git a/CMakeLists.txt b/firmware/CMakeLists.txt similarity index 100% rename from CMakeLists.txt rename to firmware/CMakeLists.txt diff --git a/include/README b/firmware/include/README similarity index 100% rename from include/README rename to firmware/include/README diff --git a/lib/README b/firmware/lib/README similarity index 100% rename from lib/README rename to firmware/lib/README diff --git a/platformio.ini b/firmware/platformio.ini similarity index 100% rename from platformio.ini rename to firmware/platformio.ini diff --git a/src/ble/BluetoothServer.cpp b/firmware/src/ble/BluetoothServer.cpp similarity index 100% rename from src/ble/BluetoothServer.cpp rename to firmware/src/ble/BluetoothServer.cpp diff --git a/src/ble/BluetoothServer.h b/firmware/src/ble/BluetoothServer.h similarity index 100% rename from src/ble/BluetoothServer.h rename to firmware/src/ble/BluetoothServer.h diff --git a/src/main.cpp b/firmware/src/main.cpp similarity index 100% rename from src/main.cpp rename to firmware/src/main.cpp diff --git a/src/sensors/BmpSensor.cpp b/firmware/src/sensors/BmpSensor.cpp similarity index 100% rename from src/sensors/BmpSensor.cpp rename to firmware/src/sensors/BmpSensor.cpp diff --git a/src/sensors/BmpSensor.h b/firmware/src/sensors/BmpSensor.h similarity index 100% rename from src/sensors/BmpSensor.h rename to firmware/src/sensors/BmpSensor.h diff --git a/src/sensors/Sensor.h b/firmware/src/sensors/Sensor.h similarity index 100% rename from src/sensors/Sensor.h rename to firmware/src/sensors/Sensor.h diff --git a/src/sensors/sensor_data_t.h b/firmware/src/sensors/sensor_data_t.h similarity index 100% rename from src/sensors/sensor_data_t.h rename to firmware/src/sensors/sensor_data_t.h diff --git a/test/README b/firmware/test/README similarity index 100% rename from test/README rename to firmware/test/README