diff --git a/operame.ino b/operame.ino index 7b7083a..7edb46c 100644 --- a/operame.ino +++ b/operame.ino @@ -11,12 +11,24 @@ #include #include #include +#include +#include +//#include #include "Stream.h" + #define LANGUAGE "nl" + +#define DHTPIN 15 // Digital pin connected to the DHT sensor +// Uncomment the type of sensor in use: +//#define DHTTYPE DHT11 // DHT 11 +#define DHTTYPE DHT22 // DHT 22 (AM2302) +//#define DHTTYPE DHT21 // DHT 21 (AM2301) + OperameLanguage::Texts T; enum Driver { AQC, MHZ }; + Driver driver; MQTTClient mqtt; HardwareSerial hwserial1(1); @@ -24,6 +36,8 @@ TFT_eSPI display; TFT_eSprite sprite(&display); MHZ19 mhz; WiFiClientSecure wificlient; +DHT dht(DHTPIN, DHTTYPE); + const int pin_portalbutton = 35; const int pin_demobutton = 0; @@ -83,6 +97,21 @@ void display_big(const String& text, int fg = TFT_WHITE, int bg = TFT_BLACK) { sprite.pushSprite(0, 0); } +void display_3(const String& co2, const String& temp, const String& hum, int fg = TFT_WHITE, int bg = TFT_BLACK) { + clear_sprite(bg); + sprite.setTextSize(1); + sprite.setTextFont(8); + sprite.setTextDatum(MC_DATUM); + sprite.setTextColor(fg, bg); + sprite.drawString(co2, display.width()/2, display.height()/2 - 25); + sprite.setTextFont(4); + sprite.setTextDatum(ML_DATUM); + sprite.drawString(temp, 10, display.height() - 15); + sprite.setTextDatum(MR_DATUM); + sprite.drawString(hum, display.width() - 10, display.height() - 15); + + sprite.pushSprite(0, 0); +} void display_lines(const std::list& lines, int fg = TFT_WHITE, int bg = TFT_BLACK) { clear_sprite(bg); @@ -126,6 +155,26 @@ void display_ppm(int ppm) { display_big(String(ppm), fg, bg); } +void display_ppm_t_h(int ppm, float t, float h) { + int fg, bg; + if (ppm >= co2_critical) { + fg = TFT_WHITE; + bg = TFT_RED; + } else if (ppm >= co2_warning) { + fg = TFT_BLACK; + bg = TFT_YELLOW; + } else { + fg = TFT_GREEN; + bg = TFT_BLACK; + } + + if (ppm >= co2_blink && millis() % 2000 < 1000) { + std::swap(fg, bg); + } + + display_3(String(ppm), String(t), String(h), fg, bg); +} + void calibrate() { auto lines = T.calibration; for (int count = 60; count >= 0; count--) { @@ -382,7 +431,9 @@ void setup() { Serial.println("Using MHZ driver."); } - + // Initialize DHT device. + dht.begin(); + for (auto& str : T.portal_instructions[0]) { str.replace("{ssid}", WiFiSettings.hostname); } @@ -476,10 +527,20 @@ void post_rest_message(DynamicJsonDocument message, Stream& stream) { void loop() { static int co2; + static float h; + static float t; every(5000) { + // Read CO2, humidity and temperature co2 = get_co2(); + h = dht.readHumidity(); + t = dht.readTemperature(); + // Print data to serial port Serial.print(co2); + Serial.print(","); + Serial.print(t); + Serial.print(","); + Serial.print(h); Serial.println(); } @@ -489,8 +550,16 @@ void loop() { } else if (co2 == 0) { display_big(T.wait); } else { - // some MH-Z19's go to 10000 but the display has space for 4 digits - display_ppm(co2 > 9999 ? 9999 : co2); + // Check if there is a humidity sensor + if (isnan(h) || isnan(t)) { + Serial.println("Failed to read from DHT sensor!"); + // Only display CO2 value (the old way) + // some MH-Z19's go to 10000 but the display has space for 4 digits + display_ppm(co2 > 9999 ? 9999 : co2); + } else { + // Display also humidity and temperature + display_ppm_t_h(co2 > 9999 ? 9999 : co2, t, h); + } } } diff --git a/platformio.ini b/platformio.ini index 2dc9d2a..bdc3c07 100644 --- a/platformio.ini +++ b/platformio.ini @@ -6,7 +6,7 @@ ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples -; http://docs.platformio.org/page/projectconf.html +; https://docs.platformio.org/page/projectconf.html [platformio] src_dir = . @@ -20,12 +20,14 @@ board_build.partitions = default.csv framework = arduino targets = upload monitor_speed = 115200 + lib_deps = ESP-WiFiSettings@^3.7.2 MH-Z19 TFT_eSPI MQTT ArduinoJson + adafruit/DHT sensor library@^1.4.2 build_flags = # ESP-WiFiSettings languages: @@ -55,18 +57,13 @@ build_flags = -DLOAD_GFXFF=1 -DSPI_FREQUENCY=40000000 + [env:serial] upload_protocol = esptool [env:ota] upload_protocol = espota upload_port = operame-HEX_HERE.local -upload_flags = - --port=3232 - --auth=PASSWORD_HERE -; Alternatively, instead of editing this file (which is annoying because it -; might end up being committed in git), you can create extra an extra config -; file. -; Just copy the [env:ota] section to a new file called platformio-NAME.ini -; and change [env:ota] to [env:NAME]. You can use this to OTA-update multiple -; Operames with a single command: pio run -t upload -e NAME -e NAME -e NAME +upload_flags = + --port=3232 + --auth=PASSWORD_HERE