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