Add support for the BMP085/BMP180 sensor

This commit is contained in:
Gergely Polonkai 2021-01-15 10:39:34 +01:00
parent 4c58cacc21
commit 39d3d1e66d
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 67 additions and 8 deletions

View File

@ -35,17 +35,23 @@
# include <DHT.h>
#endif // DHT_TYPE
#ifdef HAVE_BH1750
#if defined(HAVE_BH1750) || defined(HAVE_BMP180)
# include <Wire.h>
#endif // defined(HAVE_BH1750) || defined(HAVE_BMP180)
#ifdef HAVE_BH1750
# include <BH1750.h>
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
# include <Adafruit_BMP085.h>
#endif // HAVE_BMP180
ESP8266WebServer server(80);
unsigned long last_update = 0;
#ifdef DHT_TYPE
DHT dht(DHT_PIN, DHT_TYPE);
float temperature = 0.0;
float humidity = 0.0;
#endif // DHT_TYPE
@ -58,6 +64,15 @@ BH1750 light;
float light_level = 0;
#endif // HAVE_BH1750
#if defined(DHT_TYPE) || defined(HAVE_BMP180)
float temperature = 0.0;
#endif // defined(DHT_TYPE) || defined(HAVE_BMP180)
#ifdef HAVE_BMP180
Adafruit_BMP085 BMP;
uint32_t pressure = 0;
#endif // HAVE_BMP180
static const char TEXT_PLAIN[] PROGMEM = "text/plain";
void
@ -69,13 +84,18 @@ reply_not_found()
void
send_metrics()
{
static const size_t dht_size = 228;
static const size_t temperature_size = 117;
static const size_t humidity_size = 111;
static const size_t rain_size = 86;
static const size_t light_size = 72;
static const size_t pressure_size = 82;
size_t message_size =
#if defined(DHT_TYPE) || defined(HAVE_BMP180)
temperature_size +
#endif // defined(DHT_TYPE) || defined(HAVE_BMP180)
#ifdef DHT_TYPE
dht_size +
humidity_size +
#endif // DHT_TYPE
#ifdef RAIN_PIN
rain_size +
@ -83,14 +103,19 @@ send_metrics()
#ifdef HAVE_BH1750
light_size +
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
pressure_size +
#endif // HAVE_BMP180
1;
char message[message_size];
snprintf(message, message_size,
#ifdef DHT_TYPE
#if defined(DHT_TYPE) || defined(HAVE_BMP180)
"# HELP temperature_celsius Temperature in degrees Celsius\n"
"# TYPE temperature_celsius gauge\n"
"temperature_celsius %.2f\n"
#endif // defined(DHT_TYPE) || defined(HAVE_BMP180)
#ifdef DHT_TYPE
"# HELP relative_humidity Relative humidity, in percents\n"
"# TYPE relative_humidity gauge\n"
"relative_humidity %.2f\n"
@ -105,8 +130,16 @@ send_metrics()
"# TYPE light gauge\n"
"light %.2f\n"
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
"# HELP pressure Atmospheric pressure, in Pa\n"
"# TYPE pressure gauge\n"
"pressure %d\n"
#endif // HAVE_BMP180
#if defined(DHT_TYPE) || defined(HAVE_BMP180)
, temperature
#endif // defined(DHT_TYPE) || defined(HAVE_BMP180)
#ifdef DHT_TYPE
, temperature, humidity
, humidity
#endif // DHT_TYPE
#ifdef RAIN_PIN
, raining ? 1 : 0
@ -114,6 +147,9 @@ send_metrics()
#ifdef HAVE_BH1750
, light_level
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
, pressure
#endif // HAVE_BMP180
);
server.send(200, "text/plain; version=0.0.4; charset=utf-8", message);
@ -178,6 +214,13 @@ setup()
}
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
if (!bmp.begin()) {
Serial.println("Could not find the BMP180 sensor!");
stop();
}
#endif // HAVE_BMP180
Serial.println("All set, starting the loop.");
}
@ -187,10 +230,10 @@ read_sensors()
// Turn on the LED to indicate that were working
digitalWrite(LED_BUILTIN, LOW);
#ifdef DHT_TYPE
#if defined(DHT_TYPE)
temperature = dht.readTemperature();
humidity = dht.readHumidity();
#endif
#endif // DHT_TYPE
#ifdef RAIN_PIN
raining = (digitalRead(RAIN_PIN) == HIGH);
@ -200,6 +243,15 @@ read_sensors()
light_level = light.readLightLevel();
#endif // HAVE_BH1750
#ifdef HAVE_BMP180
pressure = bmp.readPressure();
// If we have a DHT sensor, we already measured the temperature
# ifndef DHT_TYPE
temperature = bmp.readTemperature();
# endif // !DHT_TYPE
#endif // HAVE_BMP180
// Turn off the LED to indicate work is finished
digitalWrite(LED_BUILTIN, HIGH);
}

View File

@ -64,3 +64,10 @@
// and SCL pins using the I2C_SDA_PIN and I2C_SCL_PIN defines above.
#undef HAVE_BH1750
//#define HAVE_BH1750
// If you have a BMP180 or BMP085 pressure + temperature sensor, remove this
// #undef line and uncomment the #define HAVE_BMP180 line. You will also have
// to set the SDA and SCL pins using the I2C_SDA_PIN and I2C_SCL_PIN defines
// above.
#undef HAVE_BMP180
// #define HAVE_BMP180