|
@@ -1,11 +1,19 @@
|
|
|
//#include <Arduino.h>
|
|
|
|
|
|
+#include "fwversion.h"
|
|
|
+#include "fwsettings.h"
|
|
|
+
|
|
|
// ESP8266 core libs
|
|
|
#include <ESP8266HTTPUpdateServer.h>
|
|
|
#include <ESP8266WebServer.h>
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <ESP8266mDNS.h>
|
|
|
-//#include <WiFiClient.h>
|
|
|
+
|
|
|
+#ifdef USE_MQTT_TLS
|
|
|
+#include <WiFiClientSecure.h>
|
|
|
+#else
|
|
|
+#include <WiFiClient.h>
|
|
|
+#endif
|
|
|
|
|
|
// WiFiManager
|
|
|
#include <PersWiFiManagerExt.h>
|
|
@@ -36,193 +44,6 @@
|
|
|
#include <ButtonEventCallback.h>
|
|
|
#include <PushButton.h>
|
|
|
|
|
|
-// PRE-COMPILE CONFIGURATION
|
|
|
-
|
|
|
-//#define FORCE_SPIFFS_FORMAT
|
|
|
-
|
|
|
-//#define DEBUG_VERBOSE
|
|
|
-//#define DEBUGMODE // WARNING - DEBUGMODE reveals saved passwords on serial connection and shows note in web interface
|
|
|
-
|
|
|
-//#define DEBUG_SERIAL true
|
|
|
-#define DEBUG_MQTT true
|
|
|
-
|
|
|
-// ENABLE FEATURES
|
|
|
-#define ENABLE_FEATURE_NTP_TIME
|
|
|
-#define ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
|
|
|
-#define ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
|
|
|
-#define ENABLE_FEATURE_PW_ENCRYPTION
|
|
|
-#define ENABLE_FEATURE_HTTP_UPDATER
|
|
|
-
|
|
|
-#ifdef ENABLE_FEATURE_NTP_TIME
|
|
|
-#include <time.h>
|
|
|
-#endif
|
|
|
-
|
|
|
-//#ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
|
|
|
-#include <WebSocketsServer.h>
|
|
|
-//#endif
|
|
|
-
|
|
|
-// DEFINE NAMES
|
|
|
-#define FIRMWARE_NAME "WiFiThermostat"
|
|
|
-#define FIRMWARE_VERSION "0.6.1"
|
|
|
-#define FIRMWARE_URL "https://git.flokra.at/flo/WiFiThermostat"
|
|
|
-#define FIRMWARE_COPYRIGHT "FloKra"
|
|
|
-#define FIRMWARE_COPYRIGHT_URL "https://www.flokra.at/"
|
|
|
-#define FIRMWARE_SHORTNAME "WTherm"
|
|
|
-
|
|
|
-// ENCRYPTION KEY used for passwords - up to 100 chars
|
|
|
-#define PW_ENCRYPTION_KEY "PLhg2i7fPoYSoipq1hTF1KOqyzp3OPWtUzufCDWmD9KgxbaKYOG5WbxoO4QoLDj3F6Iif4R55UbHX1nRo7GKNqT6QXQCTsdFvFem"
|
|
|
-
|
|
|
-// default values, can later be overridden via configuration
|
|
|
-#define MAX_MEASUREMENT_AGE 30000 // in ms, for measured inside Temp/Hum
|
|
|
-#define MAX_MEASUREMENT_AGE_OUT 300000 // in ms, for received outside Temp/Hum
|
|
|
-
|
|
|
-// NTP defaults
|
|
|
-#ifdef ENABLE_FEATURE_NTP_TIME
|
|
|
-#define DEFAULT_NTP_ENABLED true
|
|
|
-#define DEFAULT_NTP_SERVER "pool.ntp.org"
|
|
|
-#define DEFAULT_NTP_SYNC_INTERVAL 7200
|
|
|
-#define DEFAULT_TIMEZONE "CET-1CEST,M3.5.0/02,M10.5.0/03"
|
|
|
-#endif
|
|
|
-
|
|
|
-// confDevWiFi
|
|
|
-//#define DEVICE_NAME "WTherm-1" // now created from FIRMWARE_SHORTNAME + last 2 octets of MAC address
|
|
|
-//#define DEFAULT_HOST_NAME "" // now created from FIRMWARE_SHORTNAME + last 2 octets of MAC address
|
|
|
-#define DEFAULT_WIFI_APMODE_PASSWORD "nichtsicher" // min 8 chars!
|
|
|
-#define DEFAULT_WIFI_APMODE_TIMEOUT 5 //min
|
|
|
-#define DEFAULT_WIFI_RETRY_INTERVAL 5 //min
|
|
|
-#define DEFAULT_WIFI_CONNCHECK_INTERVAL 20 //s
|
|
|
-#define DEFAULT_WIFI_REBOOT_ONNOCONNECT 60 //min
|
|
|
-
|
|
|
-// confWeb
|
|
|
-#define HTTP_SET_TOKEN ""
|
|
|
-#define DEFAULT_HTTP_USER ""
|
|
|
-#define DEFAULT_HTTP_PASS ""
|
|
|
-#define DEFAULT_HTTP_USER_AUTH false
|
|
|
-#define DEFAULT_ENABLE_WEBCONSOLE true
|
|
|
-
|
|
|
-// confMqtt
|
|
|
-#define MQTT_ENABLE false
|
|
|
-#define MQTT_SERVER ""
|
|
|
-#define MQTT_PORT 1883
|
|
|
-#define MQTT_USER ""
|
|
|
-#define MQTT_PASS ""
|
|
|
-#define MQTT_TOPIC_IN "Test/WTherm"
|
|
|
-#define MQTT_TOPIC_OUT "Test/WTherm/stat"
|
|
|
-#define MQTT_OUT_RETAIN false
|
|
|
-#define MQTT_OUT_RETAIN_SENSORS false
|
|
|
-#define MQTT_OUT_PUBLISH_INTERVAL 0 // min, 0=only on change
|
|
|
-#define MQTT_OUT_PUBLISH_INTERVAL_SENSORS 5 // min, 0=only on change
|
|
|
-#define MQTT_WILLTOPIC "Test/WTherm/availability"
|
|
|
-#define MQTT_WILLQOS 2
|
|
|
-#define MQTT_WILLRETAIN false
|
|
|
-#define MQTT_WILLMSG "offline"
|
|
|
-#define MQTT_CONNMSG "online"
|
|
|
-#define MQTT_ENABLE_HEARTBEAT true
|
|
|
-
|
|
|
-// max interval for MQTT heartbeat message. only applicable if MQTT
|
|
|
-// IN-topic is defined. after this timeout MQTT reconnect is forced
|
|
|
-#define MQTT_HEARTBEAT_MAXAGE 180000
|
|
|
-
|
|
|
-// max interval for MQTT heartbeat message. only applicable if MQTT
|
|
|
-// IN-topic is defined. after this timeout the ESP will reboot
|
|
|
-#define MQTT_HEARTBEAT_MAXAGE_REBOOT 1800000
|
|
|
-
|
|
|
-// confBas
|
|
|
-#define DEFAULT_SETTEMP_MIN 16.0 // minimal temperature that can be set
|
|
|
-#define DEFAULT_SETTEMP_MAX 26.0 // maximal temperature that can be set
|
|
|
-#define AUTOSAVE_SETTEMP true
|
|
|
-#define AUTOSAVE_SETMODE false
|
|
|
-#define SAVE_TO_MQTT_RETAINED false
|
|
|
-#define DEFAULT_MEASURE_INTERVAL 15 // interval for temp/hum measurement
|
|
|
-
|
|
|
-// interval for display updates (if out-temp is active, display will toggle
|
|
|
-// in this interval)
|
|
|
-#define DEFAULT_DISPLAY_INTERVAL 5
|
|
|
-#define DEFAULT_DISPLAY_TIMEOUT 30 // display timeout after keypress (illumination)
|
|
|
-#define DEFAULT_PIR_ENABLES_DISPLAY false
|
|
|
-#define DEFAULT_PIR_ENABLES_DISPLAY_PRESET0_ONLY true
|
|
|
-#define DEFAULT_TOGGLING_I_O_TEMPHUM false
|
|
|
-
|
|
|
-// confAdv
|
|
|
-#define DEFAULT_HYSTERESIS 0.15 // hysteresis, normally 0.1 - 0.3
|
|
|
-#define DEFAULT_HEATING_MIN_OFFTIME 300 // minimal time the heating must keep turned off until it can start again, in s
|
|
|
-
|
|
|
-// correction value for temperature sensor reading
|
|
|
-#define TEMPSENSOR_CORRECTION_VALUE 0.0
|
|
|
-#define HUMSENSOR_CORRECTION_VALUE 0 // correction value for humidity sensor reading
|
|
|
-
|
|
|
-// decreases the set temp to overcome further temperature rise when the
|
|
|
-// heating is already switched off
|
|
|
-#define SETTEMP_DECREASE_VALUE 0.0
|
|
|
-#define OFF_MESSAGE "HEATING OFF"
|
|
|
-#define INSIDE_TEMP_LABEL "I"
|
|
|
-#define OUTSIDE_TEMP_LABEL "O"
|
|
|
-#define MODE_NAME_0 "off"
|
|
|
-#define MODE_NAME_1 "heat"
|
|
|
-#define PRESET_NAME_0 "Normal"
|
|
|
-#define PRESET_NAME_1 "Reduction 1"
|
|
|
-#define PRESET_NAME_2 "Reduction 2"
|
|
|
-
|
|
|
-// confAdd
|
|
|
-#define OUTTEMP_TOPIC_IN ""
|
|
|
-#define OUTHUM_TOPIC_IN ""
|
|
|
-#define MQTT_TOPIC_PIR "" // extra publish topic for PIR sensor
|
|
|
-#define MQTT_TOPIC_PIR_ON "ON"
|
|
|
-#define MQTT_TOPIC_PIR_OFF "OFF"
|
|
|
-
|
|
|
-// confLog
|
|
|
-#define DEFAULT_LOGLEVEL_SERIAL LOGLEVEL_DEBUG
|
|
|
-#define DEFAULT_LOGLEVEL_WEB LOGLEVEL_INFO
|
|
|
-#define DEFAULT_LOGLEVEL_MQTT LOGLEVEL_INFO
|
|
|
-#define DEFAULT_LOGLEVEL_SYSLOG LOGLEVEL_DEBUG // SYSLOG not implemented yet
|
|
|
-#define LOG_WEB_REQUESTS false
|
|
|
-
|
|
|
-// other (not configurable via commands/WebIF)
|
|
|
-#define DEFAULT_SETTEMP_HEATOFF 5.0 // heating target temperature in OFF mode (freezing guard, therefore > 0)
|
|
|
-#define SETTEMP_LOW_MIN 14.0 // minimal configurable temperature for reduction mode
|
|
|
-#define SETTEMP_LOW_MAX 21.5 // maximal configurable temperature for reduction mode
|
|
|
-
|
|
|
-// default initial values
|
|
|
-#define DEFAULT_SETTEMP 21.5
|
|
|
-#define DEFAULT_HEATINGMODE 1
|
|
|
-#define DEFAULT_PRESET 1
|
|
|
-#define DEFAULT_SETTEMP_LOW 20.0 // set temperature in night/low mode
|
|
|
-#define DEFAULT_SETTEMP_LOW2 17.0 // set temperature in night/low mode
|
|
|
-
|
|
|
-// COMPILE TIME SETTINGS
|
|
|
-// default values that can only be configured at compile time / hardware configuration
|
|
|
-#define BUTTON_DEBOUNCE_TIME 120
|
|
|
-#define BUTTON_HOLD_TIME 750
|
|
|
-
|
|
|
-// pin assignments and I2C addresses
|
|
|
-#define PIN_DHTSENSOR 13
|
|
|
-#define PIN_RELAIS 15 // 16
|
|
|
-#define PIN_BUTTON_PLUS 2
|
|
|
-#define PIN_BUTTON_MINUS 0
|
|
|
-#define PIN_BUTTON_MODE 14
|
|
|
-#define PIN_PIRSENSOR 12
|
|
|
-#define DHTTYPE DHT22 // DHT sensor type
|
|
|
-#define LCDADDR 0x27 // I2C address LCD
|
|
|
-#define LCDCOLS 16 // LCD cols
|
|
|
-#define LCDLINES 2 // LCD lines
|
|
|
-
|
|
|
-// default logic levels
|
|
|
-#define RELAISONSTATE HIGH
|
|
|
-#define BUTTONONSTATE LOW
|
|
|
-
|
|
|
-// SPIFFS settings
|
|
|
-#define SPIFFS_DBG
|
|
|
-#define SPIFFS_USE_MAGIC
|
|
|
-
|
|
|
-// LOG LEVELS
|
|
|
-#define LOGLEVEL_OFF 0
|
|
|
-#define LOGLEVEL_ERROR 1
|
|
|
-#define LOGLEVEL_WARN 2
|
|
|
-#define LOGLEVEL_INFO 3
|
|
|
-#define LOGLEVEL_DEBUG 4
|
|
|
-#define LOGLEVEL_VERBOSE 5
|
|
|
-
|
|
|
-// END PRE-COMPILE CONFIGURATION
|
|
|
|
|
|
PROGMEM const char PGMStr_FIRMWARE_NAME[] = FIRMWARE_NAME;
|
|
|
PROGMEM const char PGMStr_FIRMWARE_VERSION[] = FIRMWARE_VERSION;
|
|
@@ -241,12 +62,13 @@ PROGMEM const char PGMStr_setTempLow2[] = "setTempLow2";
|
|
|
PROGMEM const char PGMStr_switchHeating[] = "switch heating";
|
|
|
PROGMEM const char PGMStr_mqttRetainedSave[] = "MQTT retained save";
|
|
|
PROGMEM const char PGMStr_heating[] = "heating";
|
|
|
-PROGMEM const char PGMStr_thermostat[] = "TSTAT";
|
|
|
+PROGMEM const char PGMStr_thermostat[] = "THST";
|
|
|
PROGMEM const char PGMStr_WiFi[] = "WiFi";
|
|
|
//PROGMEM const char PGMStr_connectedTo[] = "connected to";
|
|
|
//PROGMEM const char PGMStr_withIP[] = "with IP";
|
|
|
PROGMEM const char PGMStr_MQTT[] = "MQTT";
|
|
|
-PROGMEM const char PGMStr_connectedReconnects[] = "connected, reconnects";
|
|
|
+PROGMEM const char PGMStr_connectedTo[] = "connected to";
|
|
|
+PROGMEM const char PGMStr_reconnects[] = "reconnects";
|
|
|
PROGMEM const char PGMStr_connectedFailed[] = "connect FAILED";
|
|
|
|
|
|
PROGMEM const char compile_date[] = __DATE__ " " __TIME__;
|
|
@@ -327,6 +149,7 @@ struct confDataDevWiFi
|
|
|
uint16_t WiFiRetryInterval;
|
|
|
uint16_t WiFiConnCheckInterval;
|
|
|
uint16_t WiFiRebootOnNoConnect;
|
|
|
+ bool wasChanged;
|
|
|
} confDevWiFi;
|
|
|
|
|
|
// confWeb
|
|
@@ -342,6 +165,7 @@ struct confDataWeb
|
|
|
char http_pass2[31];
|
|
|
bool wConsole;
|
|
|
bool wsConsole;
|
|
|
+ bool wasChanged;
|
|
|
} confWeb;
|
|
|
|
|
|
// confMqtt
|
|
@@ -366,6 +190,7 @@ struct confDataMqtt
|
|
|
bool mqtt_enable_heartbeat;
|
|
|
unsigned long mqtt_heartbeat_maxage_reconnect;
|
|
|
unsigned long mqtt_heartbeat_maxage_reboot;
|
|
|
+ bool wasChanged;
|
|
|
} confMqtt;
|
|
|
|
|
|
// confBas
|
|
@@ -383,6 +208,7 @@ struct confDataBas
|
|
|
bool PIR_enablesDisplay; // PIR sensor enables display illumination
|
|
|
bool PIR_enablesDisplay_preset0only;
|
|
|
bool togglingTempHumAIDisplay;
|
|
|
+ bool wasChanged;
|
|
|
} confBas;
|
|
|
|
|
|
// confAdv
|
|
@@ -405,6 +231,7 @@ struct confDataAdv
|
|
|
char psetName0[15];
|
|
|
char psetName1[15];
|
|
|
char psetName2[15];
|
|
|
+ bool wasChanged;
|
|
|
} confAdv;
|
|
|
|
|
|
// confAdd
|
|
@@ -415,6 +242,7 @@ struct confDataAdd
|
|
|
char mqtt_payload_pir_off[10];
|
|
|
char outTemp_topic_in[51];
|
|
|
char outHum_topic_in[51];
|
|
|
+ bool wasChanged;
|
|
|
} confAdd;
|
|
|
|
|
|
// confTime
|
|
@@ -425,6 +253,7 @@ struct confDataTime
|
|
|
char ntpServer1[31];
|
|
|
char ntpServer2[31];
|
|
|
unsigned long ntpSyncInterval;
|
|
|
+ bool wasChanged;
|
|
|
} confTime;
|
|
|
|
|
|
// confLog
|
|
@@ -435,6 +264,7 @@ struct confDataLog
|
|
|
uint8_t logLevelMqtt;
|
|
|
uint8_t logLevelSyslog;
|
|
|
bool logWebRequests;
|
|
|
+ bool wasChanged;
|
|
|
} confLog;
|
|
|
|
|
|
// Uptime
|
|
@@ -514,7 +344,9 @@ bool displayActive = false; // gets true when button is pressed. display light
|
|
|
// are only performed while display is active
|
|
|
bool PIRSensorOn = false;
|
|
|
unsigned long heatingOnTime, heatingOffTime;
|
|
|
-bool config_was_changed = false;
|
|
|
+//bool config_was_changed = false;
|
|
|
+bool configChangedRestartRequired = false;
|
|
|
+bool configChangedMqttConnResetRequired = false;
|
|
|
|
|
|
unsigned long lastUpdate_setTemp = 0; // set to millis() every time setTemp value is changed
|
|
|
unsigned long lastUpdate_setTempLow = 0; // set to millis() every time setTemp value is changed
|
|
@@ -534,6 +366,8 @@ uint16_t saveValuesTimeout = 5000;
|
|
|
unsigned long lastValueChange; // is set to millis() whenever setTemp value
|
|
|
// and/or heatingMode value is changed. used for
|
|
|
// autoSave function with hardcoded 5s timeout
|
|
|
+unsigned long lastConfigChange;
|
|
|
+bool lastConfigChangeNoteAlreadyDone = false;
|
|
|
bool setTempAlreadySaved = true; // only save if not yet done
|
|
|
bool setTempLowAlreadySaved = true; // only save if not yet done
|
|
|
bool setTempLow2AlreadySaved = true; // only save if not yet done
|
|
@@ -571,8 +405,8 @@ bool displayShowLine2OverlayMsg = false;
|
|
|
// if command "debugmode 1" is called...
|
|
|
bool sysInfoEverySecond = false;
|
|
|
|
|
|
-#ifdef ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
|
|
|
-char webLogBuffer[4096];
|
|
|
+#ifdef ENABLE_FEATURE_WEB_CONSOLE
|
|
|
+char webLogBuffer[WEB_CONSOLE_BUFFER_SIZE];
|
|
|
unsigned int webLogBuffer_index = 0;
|
|
|
#endif
|
|
|
|
|
@@ -581,9 +415,14 @@ unsigned int webLogBuffer_index = 0;
|
|
|
DHT dht(PIN_DHTSENSOR, DHTTYPE);
|
|
|
LiquidCrystal_I2C lcd(LCDADDR, LCDCOLS, LCDLINES);
|
|
|
|
|
|
-WiFiClient espClient;
|
|
|
+#ifdef USE_MQTT_TLS
|
|
|
+//WiFiClientSecure wifiClient;
|
|
|
+BearSSL::WiFiClientSecure wifiClient;
|
|
|
+#else
|
|
|
+WiFiClient wifiClient;
|
|
|
+#endif
|
|
|
|
|
|
-PubSubClient mqttclient(espClient);
|
|
|
+PubSubClient mqttclient(wifiClient);
|
|
|
|
|
|
ESP8266WebServer httpServer(80);
|
|
|
|
|
@@ -763,6 +602,15 @@ void setup()
|
|
|
|
|
|
// in non-blocking mode, program will continue past this point without waiting
|
|
|
persWM.begin();
|
|
|
+
|
|
|
+#ifdef USE_MQTT_TLS
|
|
|
+ //randomSeed(micros());
|
|
|
+ //wifiClient.setFingerprint(MQTT_TLS_fingerprint);
|
|
|
+ wifiClient.setInsecure();
|
|
|
+ wifiClient.allowSelfSignedCerts();
|
|
|
+ wifiClient.setCiphersLessSecure();
|
|
|
+#endif
|
|
|
+
|
|
|
delay(500);
|
|
|
|
|
|
httpServerInit();
|
|
@@ -777,7 +625,8 @@ void setup()
|
|
|
mqttPrepareSubscribeTopics();
|
|
|
mqttClientInit();
|
|
|
}
|
|
|
- sendLog(F("DEV: setup complete."), LOGLEVEL_INFO);
|
|
|
+
|
|
|
+ sendLog(F("DEV: setup complete."), LOGLEVEL_INFO);
|
|
|
} // void setup
|
|
|
|
|
|
void loop()
|
|
@@ -835,3 +684,21 @@ void loop()
|
|
|
#endif
|
|
|
|
|
|
} // void loop
|
|
|
+
|
|
|
+//#ifdef USE_MQTT_TLS
|
|
|
+//void verifytls() {
|
|
|
+// // Use WiFiClientSecure class to create TLS connection
|
|
|
+// Serial.print(F("MQTT: trying TLS connection to "));
|
|
|
+// Serial.println(confMqtt.mqtt_server);
|
|
|
+// if (!wifiClient.connect(confMqtt.mqtt_server, confMqtt.mqtt_port)) {
|
|
|
+// Serial.println("connection failed");
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (wifiClient.verify(MQTT_TLS_fingerprint, confMqtt.mqtt_server)) {
|
|
|
+// Serial.println("certificate matches");
|
|
|
+// } else {
|
|
|
+// Serial.println("certificate doesn't match");
|
|
|
+// }
|
|
|
+//}
|
|
|
+//#endif
|