Add BME280 support to log temperature, humidity, and air pressure

This commit is contained in:
Starbeamrainbowlabs 2021-11-28 21:23:39 +00:00
parent f2d37de5fb
commit e537f9b42f
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -3,9 +3,14 @@
// #include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "./lib/pubsubclient/PubSubClient.h"
#include "./lib/pubsubclient/PubSubClient.cpp"
// These libraries need installing in the Arduino IDE Library Manager (if you install the BME280 one it should prompt for the unified sensor library automatically)
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "settings.h"
#define MULTIPLEX_PINA D5
@ -38,6 +43,8 @@
WiFiClientSecure transport;
PubSubClient mqtt;
Adafruit_BME280 bme; // I2C
float read_analog(int channel) {
const char* bin = std::bitset<8>(channel).to_string().c_str();
@ -134,6 +141,15 @@ void setup() {
while(true) delay(10000);
}
Serial.println("ok");
Serial.print("BME280: ");
if(!bme.begin(0x76)) {
Serial.println("failed!");
}
else {
Serial.println("ok");
}
}
void loop() {
@ -141,6 +157,9 @@ void loop() {
float soil_b = read_analog(SOIL_B);
float water_level_a = read_analog(WATER_LEVEL_A);
float water_level_b = read_analog(WATER_LEVEL_B);
float temp = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;
// StaticJsonDocument<96> data;
@ -165,6 +184,22 @@ void loop() {
water_level_b
)) Serial.println("[mqtt] Failed to send message");
if(!send_mqtt(
"autoplant-a",
"temperature",
temp
)) Serial.println("[mqtt] Failed to send message");
if(!send_mqtt(
"autoplant-a",
"humidity",
humidity
)) Serial.println("[mqtt] Failed to send message");
if(!send_mqtt(
"autoplant-a",
"pressure",
pressure
)) Serial.println("[mqtt] Failed to send message");
// data["id"] = "autoplant-a";
// data["sensor"] = "soil";
// data["value"] = soil_a;