diff --git a/WLAN_Thermometer.ino b/WLAN_Thermometer.ino index 4cd42cc..42ac36c 100644 --- a/WLAN_Thermometer.ino +++ b/WLAN_Thermometer.ino @@ -9,15 +9,15 @@ #include #include #include -#include #include #include +#include #define MDNS_NAME "wlanthermometer" #define AP_SSID_CONFIG_NAME "WLANTHERMOMETER-Config" #define AP_CONFIG_PASSWORD "wlanthermometer" #define AP_DATA_RESET_PIN 25 -#define TEMP_SENS_PIN 34 +#define TEMP_SENS_PIN 27 #define LCD_I2C_ADDR 0x3f #define LCD_COL 20 #define LCD_ROW 4 @@ -29,19 +29,26 @@ #define NTP_TIMEOUT 15000 #define WIFI_CONNECT_TIMEOUT 30 #define CFG_PORTAL_TIMEOUT 90 +#define DHTTYPE DHT22 LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COL, LCD_ROW); looper sched; -RunningMedian samples = RunningMedian(MEDIAN_SAMPLES); -float temp_min; -float temp_max; -float temp; +float temp[3]; +float hum[3]; +float heat[3]; bool led_state; String header; WiFiServer server(80); +DHT dht(TEMP_SENS_PIN, DHTTYPE); const uint8_t degree_sign[8] = { B00010, B00101, B00010, B00000, B00000, B00000, B00000, B00000 }; uint8_t add_summertime = 0; -bool last_config_check = false; +bool last_reset_ap_check = false; + +enum { + ACT, + MIN, + MAX +}; void setup() { @@ -113,7 +120,7 @@ void setup() DateTime.begin(NTP_TIMEOUT); while (!DateTime.isTimeValid()) { - Serial.println("Failed to get time from server."); + DEBUG_MSG("Failed to get time from server.\n"); DateTime.forceUpdate(); } if (is_wintertime(DateTime.getTime()) == false) @@ -124,15 +131,21 @@ void setup() // create a degree sign lcd.createChar(0, (uint8_t*)degree_sign); + dht.begin(); + server.begin(); sched.addJob(show_temperature, KRATE_TEMP); sched.addJob(show_time, KRATE_TIME); - sched.addJob(start_config_portal, KRATE_RESET_AP_DATA); + sched.addJob(check_reset_ap_data, KRATE_RESET_AP_DATA); - temp_min = get_temp_sensor_data(); - temp_max = temp_min; - temp = temp_min; + get_sensor_data(); + temp[MIN] = temp[ACT]; + temp[MAX] = temp[ACT]; + hum[MIN] = hum[ACT]; + hum[MAX] = hum[ACT]; + heat[MIN] = heat[ACT]; + heat[MAX] = heat[ACT]; lcd.clear(); show_time(); @@ -146,21 +159,25 @@ void loop() WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, - Serial.println("New Client."); // print a message out in the serial port + DEBUG_MSG("New Client.\n"); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected - if (client.available()) { // if there's bytes to read from the client, + if (client.available()) + { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; - if (c == '\n') { // if the byte is a newline character + if (c == '\n') + { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: - if (currentLine.length() == 0) { + if (currentLine.length() == 0) + { // checking if header is valid // YWRtaW46c3RhYWdhcg== (user:pass) admin:staagar - if (header.indexOf("YWRtaW46c3RhYWdhcg==") >= 0) { + if (header.indexOf("YWRtaW46c3RhYWdhcg==") >= 0) + { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); @@ -170,20 +187,58 @@ void loop() client.println("\n"); client.println("\n"); client.println("
\n"); - client.println("

Temperatur Kirche

\n"); + client.println("

Temperatur Kirche

\n"); client.print("

Aktuell: "); - client.print(temp); + client.print(temp[ACT]); client.println(" °C

\n"); client.print("

Minimum: "); - client.print(temp_min); + client.print(temp[MIN]); client.println(" °C

\n"); client.print("

Maximum: "); - client.print(temp_max); + client.print(temp[MAX]); + client.println(" °C

\n"); + client.println("

Luftfeuchtigkeit

\n"); + client.print("

Aktuell: "); + client.print(hum[ACT]); + client.println(" %

\n"); + client.print("

Minimum: "); + client.print(hum[MIN]); + client.println(" %

\n"); + client.print("

Maximum: "); + client.print(hum[MAX]); + client.println(" %

\n"); + client.println("

Gefühlte Temperatur

\n"); + client.print("

Aktuell: "); + client.print(heat[ACT]); + client.println(" °C

\n"); + client.print("

Minimum: "); + client.print(heat[MIN]); client.println(" °C

\n"); + client.print("

Maximum: "); + client.print(heat[MAX]); + client.println(" °C

\n"); + + DateTimeParts p = DateTime.getParts(); + char dt[21]; + + if (is_wintertime(DateTime.getTime()) == false) + add_summertime = 1; + else + add_summertime = 0; + + client.print("

"); + + sprintf(dt, "%02d.%02d.%4d %02d:%02d:%02d", p.getMonthDay(), p.getMonth() + 1, p.getYear(), p.getHours() + add_summertime, p.getMinutes(), p.getSeconds()); + client.print(dt); + if (add_summertime > 0) + client.print(" Sommerzeit\n"); + else + client.print(" Winterzeit\n"); + client.println("

\n"); + client.println("
\n"); client.println("\n"); client.println(""); - break; } // Wrong user or password, so HTTP request fails... @@ -207,14 +262,13 @@ void loop() header = ""; // Close the connection client.stop(); - Serial.println("Client disconnected."); - Serial.println(""); + DEBUG_MSG("Client disconnected.\n\n"); } } -void start_config_portal(void) +void check_reset_ap_data(void) { - if (digitalRead(AP_DATA_RESET_PIN) == HIGH && last_config_check == true) + if (digitalRead(AP_DATA_RESET_PIN) == HIGH && last_reset_ap_check == true) { DEBUG_MSG("Reset AP data\n"); @@ -234,10 +288,10 @@ void start_config_portal(void) { if (digitalRead(AP_DATA_RESET_PIN) == HIGH) DEBUG_MSG("Reset AP data pressed\n"); - last_config_check = true; + last_reset_ap_check = true; } else - last_config_check = false; + last_reset_ap_check = false; } void show_time(void) @@ -257,11 +311,11 @@ void show_time(void) } sprintf(dt, "%02d.%02d.%4d %02d:%02d:%02d", p.getMonthDay(), p.getMonth() + 1, p.getYear(), p.getHours() + add_summertime, p.getMinutes(), p.getSeconds()); - Serial.print(dt); + DEBUG_MSG("%d", dt); if (add_summertime > 0) - Serial.println(" Summertime"); + DEBUG_MSG(" Summertime\n"); else - Serial.println(" Wintertime"); + DEBUG_MSG(" Wintertime\n"); lcd.setCursor(0, 0); @@ -273,47 +327,26 @@ void show_time(void) void show_temperature(void) { - float new_temp = get_temp_sensor_data(); + get_sensor_data(); - DEBUG_MSG("Temperature: %02.2f\n", new_temp); - - if (new_temp > temp) - { - if (new_temp - temp > 1.0) - temp = temp + 1.0; - else - temp = new_temp; - } - else if (new_temp < temp) - { - if (new_temp - temp < -1.0) - temp = temp - 1.0; - else - temp = new_temp; - } - - samples.add(temp); - - if (samples.getLowest() < temp_min) - temp_min = samples.getLowest(); - if (samples.getHighest() > temp_max) - temp_max = samples.getHighest(); + DEBUG_MSG("Temperature: %02.2f\n", temp[ACT]); lcd.setCursor(0, 1); - lcd.print("Temperatur: "); - lcd.print(temp); + lcd.print("Temperatur: "); + lcd.print(temp[ACT], 1); lcd.write(0); lcd.print("C"); lcd.setCursor(0, 2); - lcd.print("Minimum: "); - lcd.print(temp_min); - lcd.write(0); - lcd.print("C"); + lcd.print("Min: "); + lcd.print(temp[MIN], 1); + lcd.print(" Max: "); + lcd.print(temp[MAX], 1); lcd.setCursor(0, 3); - lcd.print("Maximum: "); - lcd.print(temp_max); - lcd.write(0); - lcd.print("C"); + lcd.print("LF: "); + lcd.print(hum[ACT], 1); + lcd.print("%"); + lcd.print(" GT: "); + lcd.print(heat[ACT], 1); } bool is_wintertime(time_t t) @@ -353,9 +386,23 @@ bool is_wintertime(time_t t) return (true); } -float get_temp_sensor_data(void) +void get_sensor_data(void) { - float t = constrain((analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0, 0.0, 50.0); - - return (t); + temp[ACT] = constrain(dht.readTemperature(), -20.0, 80.0); + if (temp[ACT] < temp[MIN]) + temp[MIN] = temp[ACT]; + if (temp[ACT] > temp[MAX]) + temp[MAX] = temp[ACT]; + + hum[ACT] = constrain(dht.readHumidity(), 0.0, 100.0); + if (hum[ACT] < hum[MIN]) + hum[MIN] = hum[ACT]; + if (hum[ACT] > hum[MAX]) + hum[MAX] = hum[ACT]; + + heat[ACT] = constrain(dht.computeHeatIndex(temp[ACT], hum[ACT], false), -20.0, 80.0); + if (heat[ACT] < heat[MIN]) + heat[MIN] = heat[ACT]; + if (heat[ACT] > heat[MAX]) + heat[MAX] = heat[ACT]; }