From 655087cc225e98491742d101559c9d06ae630cf8 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Mon, 12 Oct 2020 08:28:50 +0200 Subject: [PATCH] Initial commit --- README.md | 0 WLAN_Thermometer.ino | 264 +++++++++++++++++++++++++++++++++++++++++++ debug.h | 26 +++++ 3 files changed, 290 insertions(+) create mode 100644 README.md create mode 100644 WLAN_Thermometer.ino create mode 100644 debug.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/WLAN_Thermometer.ino b/WLAN_Thermometer.ino new file mode 100644 index 0000000..aecc789 --- /dev/null +++ b/WLAN_Thermometer.ino @@ -0,0 +1,264 @@ +// Use from 0 to 4. Higher number, more debugging messages and memory usage. +#define _WIFIMGR_LOGLEVEL_ 1 +#define DEBUG 1 + +#include +#include +#include "debug.h" +#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 35 +#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 MEDIAN_SAMPLES 60 +#define ONBOARD_LED 2 +#define NTP_TIMEOUT 15000 + +LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COL, LCD_ROW); +looper sched; +RunningMedian samples = RunningMedian(MEDIAN_SAMPLES); +float temp_min = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; +float temp_max = temp_min; +float temp = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; +bool led_state; +String header; +WiFiServer server(80); + +void setup() +{ + pinMode(AP_DATA_RESET_PIN, INPUT_PULLUP); + pinMode(ONBOARD_LED, OUTPUT); + + Serial.begin(115200); + Serial.println(F("WLANThermometer (c)2020 H. Wirtz ")); + + lcd.init(); + lcd.backlight(); + lcd.clear(); + lcd.noCursor(); + lcd.setCursor(0, 0); + lcd.print(F("WLAN THERMOMETER")); + lcd.setCursor(0, 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; + + if (digitalRead(AP_DATA_RESET_PIN) != 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.clear(); + lcd.setCursor(0, 0); + lcd.print(F("Mode Config-AP")); + lcd.setCursor(0, 1); + lcd.print(F("192.168.4.1")); + + if (!wm.autoConnect(AP_SSID_CONFIG_NAME, AP_CONFIG_PASSWORD)) + { + DEBUG_MSG("Failed to connect\n"); + lcd.setCursor(0, 2); + lcd.print(F("Failed ")); + 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 + DEBUG_MSG("Connected\n"); + + + if (!MDNS.begin(MDNS_NAME)) + { + DEBUG_MSG("Error setting up MDNS responder!\n"); + } + else + { + DEBUG_MSG("mDNS started.\n"); + } + + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print(F("Mode WiFi client")); + lcd.setCursor(0, 1); + lcd.print(WiFi.localIP()); + + delay(500); + } + + lcd.setCursor(0, 3); + lcd.print("Getting time..."); + DateTime.setTimeZone(2); + DateTime.begin(NTP_TIMEOUT); + while (!DateTime.isTimeValid()) + { + Serial.println("Failed to get time from server."); + DateTime.forceUpdate(); + } + + server.begin(); + + sched.addJob(show_temperature, KRATE_TEMP); + sched.addJob(show_time, KRATE_TIME); + + lcd.clear(); + show_time(); + show_temperature(); +} + +void loop() +{ + sched.scheduler(); + + 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 + 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, + 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 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) { + // checking if header is valid + // YWRtaW46c3RhYWdhcg== (user:pass) admin:staagar + if (header.indexOf("YWRtaW46c3RhYWdhcg==") >= 0) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println("Connection: close"); + client.println(); + client.println("\n"); + client.println("\n"); + client.println("\n"); + client.println("\n"); + client.println("
\n"); + client.println("

Temperatur Kirche

\n"); + client.print("

Aktuell: "); + client.print(temp); + client.println(" °C

\n"); + client.print("

Minimum: "); + client.print(temp_min); + client.println(" °C

\n"); + client.print("

Maximum: "); + client.print(temp_max); + client.println(" °C

\n"); + client.println("
\n"); + client.println("\n"); + client.println(""); + + break; + } + // Wrong user or password, so HTTP request fails... + else { + client.println("HTTP/1.1 401 Unauthorized"); + client.println("WWW-Authenticate: Basic realm=\"Secure\""); + client.println("Content-Type: text/html"); + client.println(); + client.println("Authentication failed"); + break; + } + } else { // if you got a newline, then clear currentLine + currentLine = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine + } + } + } + // Clear the header variable + header = ""; + // Close the connection + client.stop(); + Serial.println("Client disconnected."); + Serial.println(""); + } +} + +void show_time(void) +{ + DateTimeParts p = DateTime.getParts(); + char dt[21]; + + Serial.println(DateTime.format(DateFormatter::DATE_ONLY)); + Serial.println(DateTime.format(DateFormatter::TIME_ONLY)); + + sprintf(dt, "%02d.%02d.%4d %02d:%02d:%02d", p.getMonthDay(), p.getMonth() + 1, p.getYear(), p.getHours(), p.getMinutes(), p.getSeconds()); + lcd.setCursor(0, 0); + lcd.print(dt); + + digitalWrite(ONBOARD_LED, led_state); + led_state = !led_state; +} + +void show_temperature(void) +{ + float new_temp = (analogRead(TEMP_SENS_PIN) / 2048.0) * 330.0; + + Serial.println(temp, 1); + + 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(); + + lcd.setCursor(0, 1); + lcd.print("Temperatur: "); + lcd.print(temp); + lcd.setCursor(0, 2); + lcd.print("Minimum: "); + lcd.print(temp_min); + lcd.setCursor(0, 3); + lcd.print("Maximum: "); + lcd.print(temp_max); +} diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..ab8dfca --- /dev/null +++ b/debug.h @@ -0,0 +1,26 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#if defined(DEBUG) && DEBUG > 0 + +#define DEBUG_ESP_PORT Serial +#define DEBUG_MSG(...) Serial.printf( __VA_ARGS__ ) + +#define DEBUG_OSC_MESSAGE(msg) \ + do { \ + char address[100]; \ + msg.getAddress(address, 0, sizeof(address)); \ + Serial.printf("osc message: [%d] %s ", msg.size(), address); \ + for (int i = 0; i < msg.size(); i++) { \ + if (msg.isFloat(i)) { Serial.printf("f:%f\t", msg.getFloat(i)); } \ + if (msg.isInt(i)) { Serial.printf("i:%d\t", msg.getInt(i)); } \ + } \ + Serial.print(F("\n")); \ + } while(0); + +#else +#define DEBUG_MSG(...) +#define DEBUG_OSC_MESSAGE(...) +#endif + +#endif