This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BME280 environmental sensors
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef BME280_SENSOR_H | ||
#define BME280_SENSOR_H | ||
|
||
#include "Feature.h" | ||
#include <Libraries/Adafruit_BME280_Library/Adafruit_BME280.h> | ||
|
||
#define SEALEVELPRESSURE_HPA (1013.25) | ||
|
||
template<const char* const name, uint8_t addr> | ||
class BME280 : public Feature<name> { | ||
|
||
protected: | ||
using Feature<name>::LOG; | ||
Adafruit_BME280 bme280; | ||
|
||
public: | ||
|
||
BME280(Device* device) : | ||
Feature<name>(device) { | ||
|
||
bool status; | ||
status = bme280.begin(addr); | ||
if (!status) { | ||
Serial.println("Could not detect a BME280 sensor, check wiring!"); | ||
while (1); | ||
} | ||
|
||
delay(100); // wait for the sensor to boot up | ||
|
||
this->updateTimer.initializeMs(15000, TimerDelegate(&BME280::publishCurrentState, this)); | ||
this->updateTimer.start(true); | ||
} | ||
|
||
protected: | ||
virtual void publishCurrentState() { | ||
long pressure = bme280.readPressure(); | ||
LOG.log("Pressure:", pressure); | ||
this->publish("pressure", String(pressure)); | ||
|
||
float temperature = bme280.readTemperature(); | ||
LOG.log("Temperature:", temperature); | ||
this->publish("temperature", String(temperature)); | ||
|
||
float humidity = bme280.readHumidity(); | ||
LOG.log("Humidity", humidity); | ||
this->publish("humidity", String(humidity)); | ||
} | ||
|
||
private: | ||
Timer updateTimer; | ||
|
||
}; | ||
|
||
|
||
#endif |