comments and low energy sampling

This commit is contained in:
Clemens-Dautermann 2022-02-06 13:04:01 +01:00 committed by Clemens-Dautermann
parent db1750ef53
commit e2f57bad76
5 changed files with 67 additions and 19 deletions

View file

@ -2,15 +2,20 @@
#include <sensors/BmpSensor.h>
#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);
}

View file

@ -3,28 +3,33 @@
#include <Adafruit_BMP280.h>
#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;
}

View file

@ -13,7 +13,8 @@ private:
public:
BmpSensor();
void sample() override;
sensor_data_t sample() override;
sensor_data_t sampleLowEnergy();
void enableStandbyMode() override;

View file

@ -1,13 +1,15 @@
#ifndef NEW_CLIMTE_GO_SENSOR_H
#define NEW_CLIMTE_GO_SENSOR_H
#include <sensors/sensor_data_t.h>
enum class SensorState : unsigned short {
AWAKE, ASLEEP
};
class Sensor {
public:
virtual void sample() = 0;
virtual sensor_data_t sample() = 0;
virtual void wakeUp() = 0;

View file

@ -0,0 +1,16 @@
#ifndef NEW_CLIMTE_GO_SENSOR_DATA_T_H
#define NEW_CLIMTE_GO_SENSOR_DATA_T_H
#include <types.h>
/**
* 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