Wetterstation v2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
17 KiB

/*
* Blink
* Turns on an LED on for one second,
* then off for one second, repeatedly.
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <Wire.h>
#include <SPI.h>
#include <GxEPD2_BW.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Org_01.h>
#include "bitmaps/Bitmaps128x250.h"
#include <Adafruit_GFX.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME280.h"
#include "Adafruit_BME680.h"
#include "Adafruit_VEML6075.h"
#define ARDUINO_SAMD_VARIANT_COMPLIANCE
#include "SdsDustSensor.h"
#include "network/XD0OTA.h"
#include "network/XD0MQTT.h"
#include "icons.h"
extern "C" {
uint8_t temprature_sens_read();
}
static const char* TAG = "MAIN";
WiFiMulti wifiMulti;
GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=SS*/ TFT_CS, /*DC=*/ TFT_DC, /*RST=*/ TFT_RST, /*BUSY=*/ -1)); // GDEH0213B72
Adafruit_BME280 bme280; // I2C (also available: hardware SPI
Adafruit_BME680 bme680; // I2C (also available: hardware SPI
//HardwareSerial Serial2(2);
SdsDustSensor sds(Serial2);
Adafruit_VEML6075 uv = Adafruit_VEML6075();
XD0OTA ota("esp32-weatherstation");
XD0MQTT mqtt;
struct __attribute__((packed)) sensor_readings_t {
float temperature = NAN; // °C
float humidity = NAN; // %H
float pressure = NAN; // Pa
uint32_t voc = 0; // Ohm
float pm10 = NAN; // µg/m³
float pm25 = NAN; // µg/m³
float uvi = NAN;
float uva = NAN;
float uvb = NAN;
float temperature_max = NAN; // °C
float temperature_min = NAN; // °C
} sensor_readings;
sensor_readings_t sensors_a4cf1211c3e4, sensors_246f28d1fa5c, sensors_246f28d1a080, sensors_30aea47b0568;
uint32_t lastDisplayUpdate = 0;
bool bme280_active = false;
bool bme680_active = false;
bool uv_active = false;
bool sds_active = false;
void helloWorld()
{
const char HelloWorld[] = "IchbinsBens!";
//Serial.println("helloWorld");
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
// center bounding box by transposition of origin:
uint16_t x = ((display.width() - tbw) / 2) - tbx;
uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print(HelloWorld);
display.setCursor(5, display.height()-5);
display.setFont(&Org_01);
display.print(FW_VERSION);
}
while (display.nextPage());
//Serial.println("helloWorld done");
}
void displayIcoPartial(const uint8_t bitmap[], uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
display.setPartialWindow(x, y, w, h);
display.firstPage(); do {
display.drawInvertedBitmap(x, y, bitmap, w, h, GxEPD_BLACK);
} while (display.nextPage());
}
void getTime(char* ptr, size_t maxsize, const char* format) {
time_t now;
struct tm timeinfo;
time(&now); // update 'now' variable with current time
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1);
tzset();
localtime_r(&now, &timeinfo);
strftime(ptr, maxsize, format, &timeinfo);
}
void getSensorMeasurements() {
if (bme280_active) {
bme280.takeForcedMeasurement();
sensor_readings.temperature = bme280.readTemperature();
sensor_readings.humidity = bme280.readHumidity();
sensor_readings.pressure = bme280.readPressure();
}
if (bme680_active) {
if (bme680.performReading()) {
sensor_readings.temperature = bme680.temperature;
sensor_readings.humidity = bme680.humidity;
sensor_readings.pressure = bme680.pressure;
sensor_readings.voc = bme680.gas_resistance;
} else {
Serial.println("Failed to perform reading :(");
}
}
if (sensor_readings.temperature > sensor_readings.temperature_max
|| isnan(sensor_readings.temperature_max)) {
sensor_readings.temperature_max = sensor_readings.temperature;
}
if (sensor_readings.temperature < sensor_readings.temperature_min
|| isnan(sensor_readings.temperature_min)) {
sensor_readings.temperature_min = sensor_readings.temperature;
}
if (uv_active) {
sensor_readings.uvi = uv.readUVI();
sensor_readings.uva = uv.readUVA();
sensor_readings.uvb = uv.readUVB();
}
if (sds_active) {
PmResult pm = sds.readPm();
if (pm.isOk()) {
sensor_readings.pm10 = pm.pm10;
sensor_readings.pm25 = pm.pm25;
}
}
}
void receiveMqtt(const char* topic, const char* data) {
sensor_readings_t* sensor = NULL;
if (strstr(topic, "thomas/sensor/a4cf1211c3e4") == topic) {
sensor = &sensors_a4cf1211c3e4;
} else if (strstr(topic, "thomas/sensor/246f28d1fa5c") == topic) {
sensor = &sensors_246f28d1fa5c;
} else if (strstr(topic, "thomas/sensor/246f28d1a080") == topic) {
sensor = &sensors_246f28d1a080;
} else if (strstr(topic, "thomas/sensor/30aea47b0568") == topic) {
sensor = &sensors_30aea47b0568;
}
char* topic_last = strrchr(topic, '/');
if (topic_last && sensor) {
if (strcmp("/temperature", topic_last) == 0) {
sensor->temperature = atof(data);
} else if (strcmp("/humidity", topic_last) == 0) {
sensor->humidity = atof(data);
} else if (strcmp("/pressure", topic_last) == 0) {
sensor->pressure = atof(data);
} else if (strcmp("/pm10", topic_last) == 0) {
sensor->pm10 = atof(data);
} else if (strcmp("/pm25", topic_last) == 0) {
sensor->pm25 = atof(data);
} else if (strcmp("/uvi", topic_last) == 0) {
sensor->uvi = atof(data);
} else if (strcmp("/uva", topic_last) == 0) {
sensor->uva = atof(data);
} else if (strcmp("/uvb", topic_last) == 0) {
sensor->uvb = atof(data);
} else if (strcmp("/voc", topic_last) == 0) {
sensor->voc = atof(data);
}
}
}
void displayValues() {
static constexpr uint8_t y_offset = 8;
display.setRotation(1);
display.setFont(NULL);
display.setTextColor(GxEPD_BLACK);
display.setTextSize(1);
display.setTextWrap(false);
char timeStr[40];
getTime(timeStr, sizeof(timeStr), "%d. %b %Y %H:%M:%S");
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
// Title
display.setCursor(30,y_offset+0);
display.println(timeStr);
display.drawLine(0,y_offset+10,display.width(), y_offset+10, GxEPD_BLACK);
// Temp
display.drawRect(0,y_offset+10,66,50,GxEPD_BLACK);
display.setFont(NULL);
display.setCursor(5,y_offset+15);
display.printf("max: %.1f", sensor_readings.temperature_max);
display.setFont(&FreeSansBold9pt7b);
display.setCursor(5,y_offset+40);
display.printf("%.1f*C", sensor_readings.temperature);
display.setFont(NULL);
display.setCursor(5,y_offset+45);
display.printf("min: %.1f", sensor_readings.temperature_min);
// Humidity
display.drawRect(65,y_offset+10,66,50,GxEPD_BLACK);
display.setFont(NULL);
display.setCursor(70,y_offset+15);
display.print("Humidity");
display.setFont(&FreeSansBold9pt7b);
display.setCursor(70,y_offset+40);
display.printf("%.1f", sensor_readings.humidity);
display.setFont(NULL);
display.setCursor(70,y_offset+45);
if (sensor_readings.humidity < 30) {
display.print("low");
} else if (sensor_readings.humidity < 60) {
display.print("comfort");
} else {
display.print("high");
}
// Pressure
display.drawRect(130,y_offset+10,66,50,GxEPD_BLACK);
display.setFont(NULL);
display.setCursor(135,y_offset+15);
display.print("Pressure");
display.setFont(&FreeSansBold9pt7b);
display.setCursor(135,y_offset+40);
display.printf("%.1f", sensor_readings.pressure / 100.0F);
// Other
display.drawRect(195,y_offset+10,56,122-10,GxEPD_BLACK);
display.setFont(NULL);
// VOC
display.setCursor(200,y_offset+15);
display.println("VOC:");
display.setCursor(200,y_offset+25);
display.printf("%.1f", sensor_readings.voc / 1000.0F);
// PM
float pm10, pm25;
if (sds_active) {
pm10 = sensor_readings.pm10;
pm25 = sensor_readings.pm25;
} else if (!isnan(sensors_a4cf1211c3e4.pm10) || !isnan(sensors_a4cf1211c3e4.pm25)) {
pm10 = sensors_a4cf1211c3e4.pm10;
pm25 = sensors_a4cf1211c3e4.pm25;
} else if (!isnan(sensors_246f28d1fa5c.pm10) || !isnan(sensors_246f28d1fa5c.pm25)) {
pm10 = sensors_246f28d1fa5c.pm10;
pm25 = sensors_246f28d1fa5c.pm25;
} else if (!isnan(sensors_246f28d1a080.pm10) || !isnan(sensors_246f28d1a080.pm25)) {
pm10 = sensors_246f28d1a080.pm10;
pm25 = sensors_246f28d1a080.pm25;
} else {
pm10 = NAN;
pm25 = NAN;
}
display.setCursor(200,y_offset+45);
display.println("PM10 / 2.5:");
display.setCursor(200,y_offset+55);
display.printf("%.1f", pm10);
display.setCursor(200,y_offset+65);
display.printf("%.1f", pm25);
// UV
float uvi, uva, uvb;
if (uv_active) {
uvi = sensor_readings.uvi;
uva = sensor_readings.uva;
uvb = sensor_readings.uvb;
} else if (!isnan(sensors_a4cf1211c3e4.uvi) || !isnan(sensors_a4cf1211c3e4.uva) || !isnan(sensors_a4cf1211c3e4.uvb)) {
uvi = sensors_a4cf1211c3e4.uvi;
uva = sensors_a4cf1211c3e4.uva;
uvb = sensors_a4cf1211c3e4.uvb;
} else if (!isnan(sensors_246f28d1fa5c.uvi) || !isnan(sensors_246f28d1fa5c.uva) || !isnan(sensors_246f28d1fa5c.uvb)) {
uvi = sensors_246f28d1fa5c.uvi;
uva = sensors_246f28d1fa5c.uva;
uvb = sensors_246f28d1fa5c.uvb;
} else if (!isnan(sensors_246f28d1a080.uvi) || !isnan(sensors_246f28d1a080.uva) || !isnan(sensors_246f28d1a080.uvb)) {
uvi = sensors_246f28d1a080.uvi;
uva = sensors_246f28d1a080.uva;
uvb = sensors_246f28d1a080.uvb;
} else {
uvi = NAN;
uva = NAN;
uvb = NAN;
}
display.setCursor(200,y_offset+85);
display.println("UV Index/A/B:");
display.setCursor(200,y_offset+95);
display.printf("%.1f", uvi);
display.setCursor(200,y_offset+105);
display.printf("%.1f", uva);
display.setCursor(200,y_offset+115);
display.printf("%.1f", uvb);
// other nodes
display.setFont(NULL);
display.setCursor(0, y_offset+70);
display.printf("246f28d1fa5c: %4.1f %4.1f %6.1f\n", sensors_246f28d1fa5c.temperature, sensors_246f28d1fa5c.humidity, sensors_246f28d1fa5c.pressure);
display.printf("a4cf1211c3e4: %4.1f %4.1f %6.1f\n", sensors_a4cf1211c3e4.temperature, sensors_a4cf1211c3e4.humidity, sensors_a4cf1211c3e4.pressure);
display.printf("246f28d1a080: %4.1f %4.1f %6.1f\n", sensors_246f28d1a080.temperature, sensors_246f28d1a080.humidity, sensors_246f28d1a080.pressure);
display.printf("30aea47b0568: %4.1f %4.1f %6.1f\n", sensors_30aea47b0568.temperature, sensors_30aea47b0568.humidity, sensors_30aea47b0568.pressure);
}
while (display.nextPage());
display.powerOff();
}
void printValues() {
if (bme280_active || bme680_active) {
#define SEALEVELPRESSURE_HPA (1013.25)
Serial.print("Temperature = ");
Serial.print(sensor_readings.temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(sensor_readings.pressure / 100.0F);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(sensor_readings.humidity);
Serial.println(" %");
}
if (bme680_active) {
Serial.print("VOC = ");
Serial.print(sensor_readings.voc / 1000.0F);
Serial.println(" hPa");
}
Serial.println();
if (uv_active) {
Serial.print("UV Index reading: "); Serial.println(sensor_readings.uvi);
Serial.print("Raw UVA reading: "); Serial.println(sensor_readings.uva);
Serial.print("Raw UVB reading: "); Serial.println(sensor_readings.uvb);
Serial.println();
}
if (sds_active) {
Serial.print("PM2.5 = ");
Serial.print(sensor_readings.pm25);
Serial.print(", PM10 = ");
Serial.println(sensor_readings.pm10);
}
}
void sendValues() {
/* send values MQTT */
if (bme280_active || bme680_active) {
String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity");
String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure");
mqtt.publish(topic_temperature.c_str(), sensor_readings.temperature, "%.2f");
mqtt.publish(topic_humidity.c_str(), sensor_readings.humidity, "%.2f");
mqtt.publish(topic_pressure.c_str(), sensor_readings.pressure / 100.0F, "%.2f");
}
if (bme680_active) {
String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc");
mqtt.publish(topic_voc.c_str(), sensor_readings.voc / 1000.0F, "%.2f");
}
if (!bme280_active && !bme680_active) {
String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
float esp32_temperature = (temprature_sens_read() - 32) / 1.8;
char temperature[8]; sprintf(temperature, "%.2f", esp32_temperature-29.40);
mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature));
}
if (uv_active) {
String topic_uvi = String("thomas/sensor/") + ota.getMAC() + String("/uvi");
String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva");
String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb");
mqtt.publish(topic_uvi.c_str(), sensor_readings.uvi, "%.2f");
mqtt.publish(topic_uva.c_str(), sensor_readings.uva, "%.2f");
mqtt.publish(topic_uvb.c_str(), sensor_readings.uvb, "%.2f");
}
if (sds_active) {
String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10");
String topic_pm25 = String("thomas/sensor/") + ota.getMAC() + String("/pm25");
mqtt.publish(topic_pm10.c_str(), sensor_readings.pm10, "%.2f");
mqtt.publish(topic_pm25.c_str(), sensor_readings.pm25, "%.2f");
}
}
/**
* \brief Setup function
*
* is run once on startup
*/
void setup()
{
Serial.begin(115200);
delay(10);
ESP_LOGD(TAG, "setup hardware and sensors");
// initialize LED digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
// initialize e-paper display
SPI.begin(18, 19, 23, TFT_CS);
display.init();
#define BME_SDA 21
#define BME_SCL 22
Wire.begin(BME_SDA, BME_SCL);
if (bme280.begin()) {
bme280_active = true;
} else {
ESP_LOGE(TAG, "Could not find a valid BME280 sensor, check wiring!");
}
if (bme680.begin()) {
bme680_active = true;
// Set up oversampling and filter initialization
bme680.setTemperatureOversampling(BME680_OS_8X);
bme680.setHumidityOversampling(BME680_OS_2X);
bme680.setPressureOversampling(BME680_OS_4X);
bme680.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme680.setGasHeater(320, 150); // 320*C for 150 ms
} else {
ESP_LOGE(TAG, "Could not find a valid BME680 sensor, check wiring!");
}
if (uv.begin()) {
uv_active = true;
uv.setIntegrationTime(VEML6075_100MS); // Set the integration constant
uv.setHighDynamic(true); // Set the high dynamic mode
uv.setForcedMode(false);
// Set the calibration coefficients
uv.setCoefficients(2.22, 1.33, // UVA_A and UVA_B coefficients
2.95, 1.74, // UVB_C and UVB_D coefficients
0.001461, 0.002591); // UVA and UVB responses
} else {
Serial.println("Failed to communicate with VEML6075 sensor, check wiring?");
}
sds.begin();
FirmwareVersionResult sds_fw = sds.queryFirmwareVersion();
if (sds_fw.isOk()) {
sds_active = true;
sds.setActiveReportingMode(); // ensures sensor is in 'active' reporting mode
sds.setCustomWorkingPeriod(5); // sensor sends data every 3 minutes
} else {
Serial.println("Failed to communicate with SDS011 sensor, check wiring?");
}
display.clearScreen();
display.refresh();
ESP_LOGD(TAG, "displaying welcome screen");
helloWorld();
display.powerOff();
ESP_LOGD(TAG, "connecting to WiFi");
WiFi.setHostname("esp32-weatherstation");
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWD);
wifiMulti.addAP(WIFI_SSID2, WIFI_PASSWD2);
wifiMulti.addAP(WIFI_SSID3, WIFI_PASSWD3);
for (int tries=0; wifiMulti.run() != WL_CONNECTED && tries < 10; tries++) {
Serial.print(".");
delay(500);
}
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
displayIcoPartial(ico_wifi16, display.width()-20, 0, ico_wifi16_width, ico_wifi16_height);
}
ESP_LOGD(TAG, "trying to fetch over-the-air update");
if (WiFi.status() == WL_CONNECTED) {
ota.update();
}
WiFi.setSleep(true);
ESP_LOGD(TAG, "connecting to MQTT");
mqtt.begin();
mqtt.subscribe("thomas/sensor/a4cf1211c3e4/#", receiveMqtt);
mqtt.subscribe("thomas/sensor/246f28d1fa5c/#", receiveMqtt);
mqtt.subscribe("thomas/sensor/246f28d1a080/#", receiveMqtt);
mqtt.subscribe("thomas/sensor/30aea47b0568/#", receiveMqtt);
ESP_LOGD(TAG, "setup done");
}
/**
* \brief Arduino main loop
*/
void loop()
{
/* Do an e-paper display refresh every 2 minutes */
if (millis() - lastDisplayUpdate >= 1*60*1000) {
lastDisplayUpdate = millis();
getSensorMeasurements();
displayValues();
printValues();
sendValues();
}
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
}
delay(2000);
}