Mqtt + bme280

This commit is contained in:
2020-11-24 20:44:08 +01:00
parent 2fbbaf7972
commit d078d7a699
2 changed files with 63 additions and 15 deletions

View File

@@ -8,9 +8,9 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:ttgo-t1] [env:esp32doit-devkit-v1]
platform = espressif32 platform = espressif32
board = ttgo-t1 board = esp32doit-devkit-v1
framework = arduino framework = arduino
monitor_port = COM[1367] monitor_port = COM[1367]
monitor_speed = 115200 monitor_speed = 115200
@@ -27,3 +27,4 @@ lib_deps =
aharshac/StringSplitter@^1.0.0 aharshac/StringSplitter@^1.0.0
sstaub/NTP@^1.4 sstaub/NTP@^1.4
aster94/Utilities@^0.4.6 aster94/Utilities@^0.4.6
adafruit/Adafruit BME280 Library@^2.1.2

View File

@@ -1,3 +1,7 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h> #include <WiFi.h>
#include <SPI.h> #include <SPI.h>
#include <FastLED.h> #include <FastLED.h>
@@ -25,9 +29,11 @@
#define PSTR // Make Arduino Due happy #define PSTR // Make Arduino Due happy
#endif #endif
Adafruit_BME280 bme; // I2C
#define COMMAND_PARAMETER_LENGTH 30 #define COMMAND_PARAMETER_LENGTH 30
#define MAX_TIMES_SCROLLED 5 #define MAX_TIMES_SCROLLED 5
#define PIN 27 #define PIN 13
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
@@ -125,6 +131,8 @@ void wifiConnected()
needMqttConnect = true; needMqttConnect = true;
} }
#define SEALEVELPRESSURE_HPA (1027.3)
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -159,6 +167,36 @@ void setup()
scroller.SetColors(colors); scroller.SetColors(colors);
scroller.SetMatrix(matrix); 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() void handleRoot()
@@ -189,6 +227,7 @@ boolean reconnect() {
Serial.println("connected"); Serial.println("connected");
// Once connected, publish an announcement... // Once connected, publish an announcement...
client.subscribe("home/scroller"); client.subscribe("home/scroller");
client.subscribe("get/home/sensor/decka/bme280");
return true; return true;
} else { } else {
Serial.print("failed, rc="); Serial.print("failed, rc=");
@@ -241,6 +280,9 @@ void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived ["); Serial.print("Message arrived [");
Serial.print(topic); Serial.print(topic);
Serial.print("] "); Serial.print("] ");
if (String("home/scroller") == String(topic)) {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]); Serial.print((char)payload[i]);
} }
@@ -254,6 +296,11 @@ void callback(char* topic, byte* payload, unsigned int length) {
scroller.DeleteLines(); scroller.DeleteLines();
decode_payload(msg); decode_payload(msg);
showTextMqtt = true; 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());
}
} }
void loop() void loop()