From e2f57bad7690cac8022a72b1852bc4d11949616f Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Sun, 6 Feb 2022 13:04:01 +0100 Subject: [PATCH] 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