diff --git a/WLAN_Thermometer.ino b/WLAN_Thermometer.ino index e030dd9..4cd42cc 100644 --- a/WLAN_Thermometer.ino +++ b/WLAN_Thermometer.ino @@ -16,16 +16,19 @@ #define MDNS_NAME "wlanthermometer" #define AP_SSID_CONFIG_NAME "WLANTHERMOMETER-Config" #define AP_CONFIG_PASSWORD "wlanthermometer" -#define AP_DATA_RESET_PIN 35 +#define AP_DATA_RESET_PIN 25 #define TEMP_SENS_PIN 34 #define LCD_I2C_ADDR 0x3f #define LCD_COL 20 #define LCD_ROW 4 #define KRATE_TEMP 5000 #define KRATE_TIME 500 +#define KRATE_RESET_AP_DATA 5000 #define MEDIAN_SAMPLES 60 #define ONBOARD_LED 2 #define NTP_TIMEOUT 15000 +#define WIFI_CONNECT_TIMEOUT 30 +#define CFG_PORTAL_TIMEOUT 90 LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COL, LCD_ROW); looper sched; @@ -36,10 +39,13 @@ float temp; bool led_state; String header; WiFiServer server(80); +const uint8_t degree_sign[8] = { B00010, B00101, B00010, B00000, B00000, B00000, B00000, B00000 }; +uint8_t add_summertime = 0; +bool last_config_check = false; void setup() { - pinMode(AP_DATA_RESET_PIN, INPUT_PULLUP); + pinMode(AP_DATA_RESET_PIN, INPUT_PULLDOWN); pinMode(ONBOARD_LED, OUTPUT); Serial.begin(115200); @@ -49,60 +55,40 @@ void setup() lcd.backlight(); lcd.clear(); lcd.noCursor(); - lcd.setCursor(0, 0); + lcd.setCursor(2, 0); lcd.print(F("WLAN THERMOMETER")); - lcd.setCursor(0, 1); + lcd.setCursor(2, 1); lcd.print(F("(c)parasiTstudio")); - delay(1000); DEBUG_MSG("Mode Client\n"); - WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP WiFiManager wm; - bool bounce[2]; - - bounce[0] = digitalRead(AP_DATA_RESET_PIN); - delay(50); - bounce[1] = digitalRead(AP_DATA_RESET_PIN); - - if (bounce[1] == LOW && bounce[0] == LOW) - { - wm.resetSettings(); - DEBUG_MSG("Resetting AP data\n"); - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print(F("Resetting AP data")); - delay(2000); - } - // Automatically connect using saved credentials, - // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"), - // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect()) - // then goes into a blocking loop awaiting configuration and will return success result + lcd.setCursor(0, 2); + lcd.print(F("Connecting WiFi... ")); + lcd.setCursor(0, 3); + lcd.print(F("CFG-AP: 192.168.4.1")); - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print(F("Mode Config-AP")); - lcd.setCursor(0, 1); - lcd.print(F("192.168.4.1")); + wm.setConnectTimeout(WIFI_CONNECT_TIMEOUT); + wm.setConfigPortalTimeout(CFG_PORTAL_TIMEOUT); + wm.setBreakAfterConfig(true); if (!wm.autoConnect(AP_SSID_CONFIG_NAME, AP_CONFIG_PASSWORD)) { - DEBUG_MSG("Failed to connect\n"); lcd.setCursor(0, 2); lcd.print(F("Failed ")); + DEBUG_MSG("Failed to connect\n"); delay(1000); lcd.setCursor(8, 2); lcd.print(F("- restart")); delay(1000); ESP.restart(); } - else { - //if you get here you have connected to the WiFi + else + { DEBUG_MSG("Connected\n"); - if (!MDNS.begin(MDNS_NAME)) { DEBUG_MSG("Error setting up MDNS responder!\n"); @@ -130,15 +116,23 @@ void setup() Serial.println("Failed to get time from server."); DateTime.forceUpdate(); } + if (is_wintertime(DateTime.getTime()) == false) + add_summertime = 1; + else + add_summertime = 0; + + // create a degree sign + lcd.createChar(0, (uint8_t*)degree_sign); server.begin(); sched.addJob(show_temperature, KRATE_TEMP); sched.addJob(show_time, KRATE_TIME); + sched.addJob(start_config_portal, KRATE_RESET_AP_DATA); - temp_min = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; + temp_min = get_temp_sensor_data(); temp_max = temp_min; - temp = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; + temp = temp_min; lcd.clear(); show_time(); @@ -218,23 +212,57 @@ void loop() } } +void start_config_portal(void) +{ + if (digitalRead(AP_DATA_RESET_PIN) == HIGH && last_config_check == true) + { + DEBUG_MSG("Reset AP data\n"); + + WiFiManager wm; + + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Resetting AP Data"); + delay(2000); + wm.resetSettings(); + lcd.setCursor(0, 1); + lcd.print("Done."); + delay(1000); + ESP.restart(); + } + else if (digitalRead(AP_DATA_RESET_PIN) == HIGH) + { + if (digitalRead(AP_DATA_RESET_PIN) == HIGH) + DEBUG_MSG("Reset AP data pressed\n"); + last_config_check = true; + } + else + last_config_check = false; +} + void show_time(void) { DateTimeParts p = DateTime.getParts(); char dt[21]; - if (is_wintertime(DateTime.getTime()) == false) + if (p.getHours() == 0 && p.getMinutes() == 0 && p.getSeconds() == 0) + DateTime.forceUpdate(); + + if ((p.getHours() == 2 || p.getHours() == 3) && p.getMinutes() == 0 && p.getSeconds() == 0) { - sprintf(dt, "%02d.%02d.%4d %02d:%02d:%02d", p.getMonthDay(), p.getMonth() + 1, p.getYear(), p.getHours() + 1, p.getMinutes(), p.getSeconds()); - Serial.print(dt); - Serial.println(" Summertime"); + if (is_wintertime(DateTime.getTime()) == false) + add_summertime = 1; + else + add_summertime = 0; } + + 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); + if (add_summertime > 0) + Serial.println(" Summertime"); else - { - sprintf(dt, "%02d.%02d.%4d %02d:%02d:%02d", p.getMonthDay(), p.getMonth() + 1, p.getYear(), p.getHours(), p.getMinutes(), p.getSeconds()); - Serial.print(dt); Serial.println(" Wintertime"); - } + lcd.setCursor(0, 0); lcd.print(dt); @@ -245,9 +273,9 @@ void show_time(void) void show_temperature(void) { - float new_temp = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; + float new_temp = get_temp_sensor_data(); - Serial.println(temp, 1); + DEBUG_MSG("Temperature: %02.2f\n", new_temp); if (new_temp > temp) { @@ -272,14 +300,20 @@ void show_temperature(void) temp_max = samples.getHighest(); lcd.setCursor(0, 1); - lcd.print("Temperatur: "); + lcd.print("Temperatur: "); lcd.print(temp); + lcd.write(0); + lcd.print("C"); lcd.setCursor(0, 2); - lcd.print("Minimum: "); + lcd.print("Minimum: "); lcd.print(temp_min); + lcd.write(0); + lcd.print("C"); lcd.setCursor(0, 3); - lcd.print("Maximum: "); + lcd.print("Maximum: "); lcd.print(temp_max); + lcd.write(0); + lcd.print("C"); } bool is_wintertime(time_t t) @@ -318,3 +352,10 @@ bool is_wintertime(time_t t) else return (true); } + +float get_temp_sensor_data(void) +{ + float t = constrain((analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0, 0.0, 50.0); + + return (t); +}