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.

412 lines
12 KiB

5 years ago
/*
* Blink
* Turns on an LED on for one second,
* then off for one second, repeatedly.
*/
#include <Arduino.h>
5 years ago
#include <WiFi.h>
#include <WiFiMulti.h>
5 years ago
5 years ago
#include <Wire.h>
#include <SPI.h>
#include <GxEPD2_BW.h>
#include <Fonts/FreeMonoBold9pt7b.h>
5 years ago
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/Org_01.h>
#include "bitmaps/Bitmaps128x250.h"
#include <Adafruit_GFX.h>
5 years ago
#include <Adafruit_Sensor.h>
5 years ago
#include "Adafruit_BME280.h"
5 years ago
#include "Adafruit_BME680.h"
5 years ago
5 years ago
#include "Adafruit_VEML6075.h"
#define ARDUINO_SAMD_VARIANT_COMPLIANCE
#include "SdsDustSensor.h"
5 years ago
5 years ago
#include "network/XD0OTA.h"
#include "network/XD0MQTT.h"
5 years ago
#include "icons.h"
extern "C" {
uint8_t temprature_sens_read();
}
5 years ago
static const char* TAG = "MAIN";
5 years ago
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
5 years ago
Adafruit_BME280 bme280; // I2C (also available: hardware SPI
Adafruit_BME680 bme680; // I2C (also available: hardware SPI
//HardwareSerial Serial2(2);
SdsDustSensor sds(Serial2);
5 years ago
Adafruit_VEML6075 uv = Adafruit_VEML6075();
5 years ago
XD0OTA ota("esp32-weatherstation");
XD0MQTT mqtt;
struct __attribute__((packed)) {
float temperature = 0.0; // °C
float humidity = 0.0; // %H
float pressure = 0.0; // Pa
uint32_t voc = 0; // Ohm
float pm10 = 0.0; // µg/m³
float pm25 = 0.0; // µg/m³
float uvi = 0.0;
float uva = 0.0;
float uvb = 0.0;
} sensor_readings;
uint32_t lastDisplayUpdate = 0;
5 years ago
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");
}
5 years ago
5 years ago
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());
}
5 years ago
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() {
5 years ago
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 :(");
}
}
5 years ago
if (uv_active) {
sensor_readings.uvi = uv.readUVI();
sensor_readings.uva = uv.readUVA();
sensor_readings.uvb = uv.readUVB();
}
5 years ago
if (sds_active) {
PmResult pm = sds.readPm();
if (pm.isOk()) {
sensor_readings.pm10 = pm.pm10;
sensor_readings.pm25 = pm.pm25;
}
5 years ago
}
}
5 years ago
void printValues() {
if (bme280_active || bme680_active) {
5 years ago
#define SEALEVELPRESSURE_HPA (1013.25)
Serial.print("Temperature = ");
Serial.print(sensor_readings.temperature);
5 years ago
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(sensor_readings.pressure / 100.0F);
5 years ago
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(sensor_readings.humidity);
5 years ago
Serial.println(" %");
}
5 years ago
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");
char temperature[8]; sprintf(temperature, "%.2f", sensor_readings.temperature);
char humidity[7]; sprintf(humidity, "%.2f", sensor_readings.humidity);
char pressure[8]; sprintf(pressure, "%.2f", sensor_readings.pressure / 100.0F);
5 years ago
mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature));
mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity));
mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure));
}
if (bme680_active) {
5 years ago
String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc");
char voc[12]; sprintf(voc, "%.2f", sensor_readings.voc / 1000.0F);
5 years ago
mqtt.publish(topic_voc.c_str(), voc, strlen(voc));
}
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");
char uvi[10]; sprintf(uvi, "%.2f", sensor_readings.uvi);
char uva[10]; sprintf(uva, "%.2f", sensor_readings.uva);
char uvb[10]; sprintf(uvb, "%.2f", sensor_readings.uvb);
5 years ago
mqtt.publish(topic_uvi.c_str(), uvi, strlen(uvi));
5 years ago
mqtt.publish(topic_uva.c_str(), uva, strlen(uva));
mqtt.publish(topic_uvb.c_str(), uvb, strlen(uvb));
}
5 years ago
if (sds_active) {
String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10");
char pm10[10]; sprintf(pm10, "%.2f", sensor_readings.pm10);
5 years ago
String topic_pm25 = String("thomas/sensor/") + ota.getMAC() + String("/pm25");
char pm25[10]; sprintf(pm25, "%.2f", sensor_readings.pm25);
5 years ago
mqtt.publish(topic_pm10.c_str(), pm10, strlen(pm10));
mqtt.publish(topic_pm25.c_str(), pm25, strlen(pm25));
5 years ago
}
5 years ago
}
5 years ago
/**
* \brief Setup function
*
* is run once on startup
*/
5 years ago
void setup()
{
5 years ago
Serial.begin(115200);
5 years ago
delay(10);
5 years ago
5 years ago
ESP_LOGD(TAG, "setup hardware and sensors");
5 years ago
// initialize LED digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
5 years ago
// initialize e-paper display
SPI.begin(18, 19, 23, TFT_CS);
display.init();
5 years ago
#define BME_SDA 21
#define BME_SCL 22
Wire.begin(BME_SDA, BME_SCL);
5 years ago
if (bme280.begin()) {
bme280_active = true;
} else {
5 years ago
ESP_LOGE(TAG, "Could not find a valid BME280 sensor, check wiring!");
5 years ago
}
5 years ago
if (bme680.begin()) {
bme680_active = true;
} else {
ESP_LOGE(TAG, "Could not find a valid BME680 sensor, check wiring!");
}
if (bme680_active) {
// 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
}
5 years ago
if (uv.begin()) {
uv_active = true;
} else {
5 years ago
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?");
}
5 years ago
5 years ago
display.clearScreen();
display.refresh();
ESP_LOGD(TAG, "displaying welcome screen");
helloWorld();
display.powerOff();
5 years ago
ESP_LOGD(TAG, "connecting to WiFi");
5 years ago
WiFi.setHostname("esp32-weatherstation");
5 years ago
5 years ago
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWD);
wifiMulti.addAP(WIFI_SSID2, WIFI_PASSWD2);
5 years ago
wifiMulti.addAP(WIFI_SSID3, WIFI_PASSWD3);
5 years ago
5 years ago
for (int tries=0; wifiMulti.run() != WL_CONNECTED && tries < 10; tries++) {
Serial.print(".");
delay(500);
}
5 years ago
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
5 years ago
displayIcoPartial(ico_wifi16, display.width()-20, 0, ico_wifi16_width, ico_wifi16_height);
5 years ago
}
5 years ago
5 years ago
ESP_LOGD(TAG, "trying to fetch over-the-air update");
5 years ago
if (WiFi.status() == WL_CONNECTED) {
ota.update();
}
5 years ago
ESP_LOGD(TAG, "connecting to MQTT");
mqtt.begin();
5 years ago
ESP_LOGD(TAG, "setup done");
5 years ago
}
5 years ago
/**
* \brief Arduino main loop
*/
5 years ago
void loop()
{
5 years ago
ESP_LOGD(TAG, "loop()");
/* Do an e-paper display refresh every 2 minutes */
if (millis() - lastDisplayUpdate >= 1*60*1000) {
lastDisplayUpdate = millis();
getSensorMeasurements();
display.setFullWindow();
5 years ago
display.setRotation(1);
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
5 years ago
display.setTextWrap(false);
display.setCursor(0, 11);
5 years ago
display.setFont(&FreeSans9pt7b);
display.println("ESP32-Wetterstation");
5 years ago
display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK);
//display.setFont(NULL);
5 years ago
display.setCursor(0, 32);
if (bme280_active || bme680_active) {
display.print("Temperatur: ");
display.print(sensor_readings.temperature);
display.println(" *C");
display.print("Luftfeuchte: ");
display.print(sensor_readings.humidity);
display.println(" %");
display.print("Luftdruck:");
display.print(sensor_readings.pressure / 100.0F);
5 years ago
display.println(" hPa");
}
if (bme680_active) {
5 years ago
display.print("Gas: ");
display.print(sensor_readings.voc / 1000.0);
5 years ago
display.println(" KOhms");
}
if (!bme280_active && !bme680_active) {
display.print("Temperatur: ");
float esp32_temperature = (temprature_sens_read() - 32) / 1.8;
display.println(esp32_temperature-29.40);
display.println("kein BME Sensor");
}
5 years ago
char timeStr[9];
getTime(timeStr, sizeof(timeStr), "%H:%M:%S");
display.setCursor(5, display.height()-5);
display.setFont(&Org_01);
display.print("Zeit: ");
display.println(timeStr);
}
while (display.nextPage());
5 years ago
display.powerOff();
5 years ago
printValues();
sendValues();
5 years ago
}
5 years ago
5 years ago
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
}
5 years ago
delay(2000);
5 years ago
}