1
0
mirror of https://github.com/revspace/operame synced 2024-11-01 05:57:30 +00:00

Added DHT22 sensor code

If DHT22 humidity sensor is connected, temp and hum is displayed.
This commit is contained in:
tomderyck 2021-06-16 09:52:10 +02:00
parent de55b35627
commit b467bda9eb
2 changed files with 103 additions and 46 deletions

View File

@ -9,8 +9,18 @@
#include <logo.h> #include <logo.h>
#include <list> #include <list>
#include <operame_strings.h> #include <operame_strings.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
//#include <DHT_U.h>
#define LANGUAGE "nl" #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; OperameLanguage::Texts T;
enum Driver { AQC, MHZ }; enum Driver { AQC, MHZ };
@ -20,6 +30,7 @@ HardwareSerial hwserial1(1);
TFT_eSPI display; TFT_eSPI display;
TFT_eSprite sprite(&display); TFT_eSprite sprite(&display);
MHZ19 mhz; MHZ19 mhz;
DHT dht(DHTPIN, DHTTYPE);
const int pin_portalbutton = 35; const int pin_portalbutton = 35;
const int pin_demobutton = 0; const int pin_demobutton = 0;
@ -70,6 +81,21 @@ void display_big(const String& text, int fg = TFT_WHITE, int bg = TFT_BLACK) {
sprite.pushSprite(0, 0); 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<String>& lines, int fg = TFT_WHITE, int bg = TFT_BLACK) { void display_lines(const std::list<String>& lines, int fg = TFT_WHITE, int bg = TFT_BLACK) {
clear_sprite(bg); clear_sprite(bg);
@ -113,6 +139,26 @@ void display_ppm(int ppm) {
display_big(String(ppm), fg, bg); 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() { void calibrate() {
auto lines = T.calibration; auto lines = T.calibration;
for (int count = 60; count >= 0; count--) { for (int count = 60; count >= 0; count--) {
@ -369,6 +415,8 @@ void setup() {
Serial.println("Using MHZ driver."); Serial.println("Using MHZ driver.");
} }
// Initialize DHT device.
dht.begin();
for (auto& str : T.portal_instructions[0]) { for (auto& str : T.portal_instructions[0]) {
str.replace("{ssid}", WiFiSettings.hostname); str.replace("{ssid}", WiFiSettings.hostname);
@ -439,10 +487,20 @@ void setup() {
void loop() { void loop() {
static int co2; static int co2;
static float h;
static float t;
every(5000) { every(5000) {
// Read CO2, humidity and temperature
co2 = get_co2(); co2 = get_co2();
h = dht.readHumidity();
t = dht.readTemperature();
// Print data to serial port
Serial.print(co2); Serial.print(co2);
Serial.print(",");
Serial.print(t);
Serial.print(",");
Serial.print(h);
Serial.println(); Serial.println();
} }
@ -452,8 +510,16 @@ void loop() {
} else if (co2 == 0) { } else if (co2 == 0) {
display_big(T.wait); display_big(T.wait);
} else { } else {
// some MH-Z19's go to 10000 but the display has space for 4 digits // Check if there is a humidity sensor
display_ppm(co2 > 9999 ? 9999 : co2); 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);
}
} }
} }

View File

@ -6,7 +6,7 @@
; Advanced options: extra scripting ; Advanced options: extra scripting
; ;
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[platformio] [platformio]
src_dir = . src_dir = .
@ -21,51 +21,42 @@ framework = arduino
targets = upload targets = upload
monitor_speed = 115200 monitor_speed = 115200
lib_deps = lib_deps =
ESP-WiFiSettings@^3.7.2 ESP-WiFiSettings@^3.7.2
MH-Z19 MH-Z19
TFT_eSPI TFT_eSPI
MQTT MQTT
build_flags = build_flags =
# ESP-WiFiSettings languages: -DLANGUAGE_EN
-DLANGUAGE_EN -DLANGUAGE_NL
-DLANGUAGE_NL -DUSER_SETUP_LOADED=1
# ESP32 debugging: -DST7789_DRIVER=1
# -DCORE_DEBUG_LEVEL=5 -DCGRAM_OFFSET=1
# TFT_eSPI configuration: -DTFT_WIDTH=135
-DUSER_SETUP_LOADED=1 -DTFT_HEIGHT=240
-DST7789_DRIVER=1 -DTFT_MOSI=19
-DCGRAM_OFFSET=1 -DTFT_SCLK=18
-DTFT_WIDTH=135 -DTFT_CS=5
-DTFT_HEIGHT=240 -DTFT_DC=16
-DTFT_MOSI=19 -DTFT_RST=-23
-DTFT_SCLK=18 -DTFT_BL=4
-DTFT_CS=5 -DTFT_BACKLIGHT_ON=HIGH
-DTFT_DC=16 -DLOAD_GLCD=1
-DTFT_RST=-23 -DLOAD_FONT2=1
-DTFT_BL=4 -DLOAD_FONT4=1
-DTFT_BACKLIGHT_ON=HIGH -DLOAD_FONT6=1
-DLOAD_GLCD=1 -DLOAD_FONT7=1
-DLOAD_FONT2=1 -DLOAD_FONT8=1
-DLOAD_FONT4=1 -DLOAD_GFXFF=1
-DLOAD_FONT6=1 -DSPI_FREQUENCY=40000000
-DLOAD_FONT7=1
-DLOAD_FONT8=1
-DLOAD_GFXFF=1
-DSPI_FREQUENCY=40000000
[env:serial] [env:serial]
upload_protocol = esptool upload_protocol = esptool
lib_deps = adafruit/DHT sensor library@^1.4.2
[env:ota] [env:ota]
upload_protocol = espota upload_protocol = espota
upload_port = operame-HEX_HERE.local upload_port = operame-HEX_HERE.local
upload_flags = upload_flags =
--port=3232 --port=3232
--auth=PASSWORD_HERE --auth=PASSWORD_HERE
; Alternatively, instead of editing this file (which is annoying because it lib_deps = adafruit/DHT sensor library@^1.4.2
; 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