Mqtt + bme280
This commit is contained in:
@@ -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
|
||||||
|
|||||||
73
src/main.cpp
73
src/main.cpp
@@ -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,19 +280,27 @@ 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("] ");
|
||||||
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()
|
void loop()
|
||||||
|
|||||||
Reference in New Issue
Block a user