Add BMP280 support

This commit is contained in:
Mathieu Maret 2023-04-14 09:23:32 +02:00
parent f9985cc2c1
commit 5bef410167
4 changed files with 70 additions and 2 deletions

View File

@ -0,0 +1,15 @@
#pragma once
#ifdef CONFIG_ENABLE_BMP280
#include "debug_sketch.h"
int BMP280GetTemperature(double &t);
int BMP280GetTempAndPressure(double &t, double &p);
int BMP280Setup();
bool BMP280IsConnected();
#else //CONFIG_ENABLE_BMP80
int BMP280GetTemperature(double &){return -1;};
int BMP280GetTempAndPressure(double &, double &){return -1;};
int BMP280Setup(){SKETCH_DEBUG_PRINTLN("BMP280 is disabled at build time"); return -1;};
bool BMP280IsConnected(){return false;};
#endif

View File

@ -0,0 +1,44 @@
#ifdef CONFIG_ENABLE_BMP280
#include "BMP280.h"
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
int bmpConnected = 0;
int BMP280Setup()
{
bmpConnected = bmp.begin(BMP280_ADDRESS_ALT);
if (!bmpConnected) {
SKETCH_DEBUG_PRINTLN("Cannot connect to BMP280");
return -1;
}
/* 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. */
return 0;
}
bool BMP280IsConnected()
{
return bmpConnected != 0;
}
int BMP280GetTemperature(double &t)
{
if (!BMP280IsConnected())
return -1;
t = bmp.readTemperature();
return 0;
}
int BMP280GetTempAndPressure(double &t, double &p)
{
if (!BMP280IsConnected())
return -1;
t = bmp.readTemperature();
p = bmp.readPressure()/100;
return 0;
}
#endif

View File

@ -31,8 +31,8 @@ void WebHandleRoot() {
"<h1>You are connected to " + String(conf.host) + "</h1>"
"<fieldset>"
"<legend>Sensors</legend>"
#ifdef CONFIG_ENABLE_BMP180
"" + (BMP180IsConnected() ? "<h6>BMP180</h6>Temperature " + String(temp, 2) + "C<br/> Pressure " + String(pressure, 2) + "hPa<br/>" : "BMP180 Disconnected" ) + ""
#if defined CONFIG_ENABLE_BMP180 || defined CONFIG_ENABLE_BMP280
"" + ((BMP180IsConnected() || BMP280IsConnected()) ? "<h6>BMP180/280</h6>Temperature " + String(temp, 2) + "C<br/> Pressure " + String(pressure, 2) + "hPa<br/>" : "BMP180/280 Disconnected" ) + ""
#endif
#ifdef CONFIG_ENABLE_DHT
"" + (DHTIsConnected() ? "<h6>DHT</h6>Temperature " + String(dhtTemp, 0) + "C<br/> Humidity " + String(dhtHumidity, 0) + "%<br/>" : "DHT Disconnected" ) + ""

View File

@ -43,6 +43,7 @@
#include "debug_sketch.h"
#include "BMP180.h"
#include "BMP280.h"
#include "BME680.h"
#include "BME680_BSEC.h"
#include "sensor_DHT.h"
@ -277,6 +278,9 @@ void setup()
if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)) {
SKETCH_DEBUG_PRINTLN("BMP180 init success");
}
if (!BMP280Setup()) {
SKETCH_DEBUG_PRINTLN("BMP280 init success");
}
if (!DHTSetup(CONFIG_DHT_PIN)) {
SKETCH_DEBUG_PRINTLN("DHT init success");
}
@ -320,6 +324,11 @@ void loop() {
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0});
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0});
}
if (!BMP280GetTempAndPressure(temp, pressure)) {
SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure);
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0});
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0});
}
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
SKETCH_DEBUG_PRINTF("Current %f°C %f%% Humidity\n", dhtTemp, dhtHumidity);
batchInfo.push_back({dhtTemp, TEMPERATURE_DHT_FEED_FORMAT, 0, 0});