From d078d7a69973dbd320503c2b4303782fa042f57e Mon Sep 17 00:00:00 2001 From: Jaro Date: Tue, 24 Nov 2020 20:44:08 +0100 Subject: [PATCH] Mqtt + bme280 --- platformio.ini | 5 ++-- src/main.cpp | 73 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/platformio.ini b/platformio.ini index ba62067..9e622dc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,9 +8,9 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:ttgo-t1] +[env:esp32doit-devkit-v1] platform = espressif32 -board = ttgo-t1 +board = esp32doit-devkit-v1 framework = arduino monitor_port = COM[1367] monitor_speed = 115200 @@ -27,3 +27,4 @@ lib_deps = aharshac/StringSplitter@^1.0.0 sstaub/NTP@^1.4 aster94/Utilities@^0.4.6 + adafruit/Adafruit BME280 Library@^2.1.2 diff --git a/src/main.cpp b/src/main.cpp index 49369e0..8bcecd4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,7 @@ +#include +#include +#include + #include #include #include @@ -25,9 +29,11 @@ #define PSTR // Make Arduino Due happy #endif +Adafruit_BME280 bme; // I2C + #define COMMAND_PARAMETER_LENGTH 30 #define MAX_TIMES_SCROLLED 5 -#define PIN 27 +#define PIN 13 WiFiClient espClient; PubSubClient client(espClient); @@ -125,6 +131,8 @@ void wifiConnected() needMqttConnect = true; } +#define SEALEVELPRESSURE_HPA (1027.3) + void setup() { Serial.begin(115200); @@ -159,6 +167,36 @@ void setup() scroller.SetColors(colors); scroller.SetMatrix(matrix); + Serial.println(F("BME280 test")); + + if (! bme.begin(0x76, &Wire)) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + } + + Serial.println("-- Default Test --"); + Serial.println("normal mode, 16x oversampling for all, filter off,"); + + bme.takeForcedMeasurement(); // has no effect in normal mode + + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" *C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); + } void handleRoot() @@ -189,6 +227,7 @@ boolean reconnect() { Serial.println("connected"); // Once connected, publish an announcement... client.subscribe("home/scroller"); + client.subscribe("get/home/sensor/decka/bme280"); return true; } else { Serial.print("failed, rc="); @@ -241,19 +280,27 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); + + if (String("home/scroller") == String(topic)) { + + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + strncpy(msg,(char *)payload,length); + msg[length] = '\0'; + + // Switch on the LED if an 1 was received as first character + scroller.TurnOff(); + scroller.DeleteLines(); + decode_payload(msg); + showTextMqtt = true; + } else if (String("get/home/sensor/decka/bme280") == String(topic)) { + client.publish("home/sensor/decka/bme280/temperature",(String("N:") + String(bme.readTemperature())).c_str()); + client.publish("home/sensor/decka/bme280/humidity",(String("N:") + String(bme.readHumidity())).c_str()); + client.publish("home/sensor/decka/bme280/pressure",(String("N:") + String(bme.readPressure() / 100.0F)).c_str()); } - Serial.println(); - - strncpy(msg,(char *)payload,length); - msg[length] = '\0'; - - // Switch on the LED if an 1 was received as first character - scroller.TurnOff(); - scroller.DeleteLines(); - decode_payload(msg); - showTextMqtt = true; } void loop()