Now using a DHT22.

Fixes and extensions for more values of the DHT22.
master
Holger Wirtz 4 years ago
parent a1a82fe9a2
commit 1202322754
  1. 185
      WLAN_Thermometer.ino

@ -9,15 +9,15 @@
#include <ESPmDNS.h>
#include <LiquidCrystal_I2C.h>
#include <looper.h>
#include <RunningMedian.h>
#include <ESPDateTime.h>
#include <Time.h>
#include <DHT.h>
#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("<meta http-equiv=\"refresh\" content=\"5\">\n");
client.println("<body>\n");
client.println("<center>\n");
client.println("<h1 style=\"color:blue;\">Temperatur Kirche</h1>\n");
client.println("<h1 style=\"color:green;\">Temperatur Kirche</h1>\n");
client.print("<h2 style=\"color:green;\">Aktuell: ");
client.print(temp);
client.print(temp[ACT]);
client.println(" &deg;C</h2>\n");
client.print("<h2 style=\"color:green;\">Minimum: ");
client.print(temp_min);
client.print(temp[MIN]);
client.println(" &deg;C</h2>\n");
client.print("<h2 style=\"color:green;\">Maximum: ");
client.print(temp_max);
client.print(temp[MAX]);
client.println(" &deg;C</h2>\n");
client.println("<h1 style=\"color:blue;\">Luftfeuchtigkeit</h1>\n");
client.print("<h2 style=\"color:blue;\">Aktuell: ");
client.print(hum[ACT]);
client.println(" %</h2>\n");
client.print("<h2 style=\"color:blue;\">Minimum: ");
client.print(hum[MIN]);
client.println(" %</h2>\n");
client.print("<h2 style=\"color:blue;\">Maximum: ");
client.print(hum[MAX]);
client.println(" %</h2>\n");
client.println("<h1 style=\"color:gray;\">Gef&uuml;hlte Temperatur</h1>\n");
client.print("<h2 style=\"color:gray;\">Aktuell: ");
client.print(heat[ACT]);
client.println(" &deg;C</h2>\n");
client.print("<h2 style=\"color:gray;\">Minimum: ");
client.print(heat[MIN]);
client.println(" &deg;C</h2>\n");
client.print("<h2 style=\"color:gray;\">Maximum: ");
client.print(heat[MAX]);
client.println(" &deg;C</h2>\n");
DateTimeParts p = DateTime.getParts();
char dt[21];
if (is_wintertime(DateTime.getTime()) == false)
add_summertime = 1;
else
add_summertime = 0;
client.print("<h4 style=\"color:black;\">");
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("</h2>\n");
client.println("</center>\n");
client.println("</body>\n");
client.println("</html>");
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];
}

Loading…
Cancel
Save