123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- // PRE-COMPILE CONFIGURATION
- //#define FORCE_SPIFFS_FORMAT
- //#define DEBUG_VERBOSE
- #define DEBUG_SERIAL true
- #define DEBUG_MQTT true
- #define SPIFFS_DBG
- #define SPIFFS_USE_MAGIC
- #define FIRMWARE_NAME "WiFiThermostat"
- #define FIRMWARE_VERSION "0.4.0"
- #define FIRMWARE_URL "https://git.flokra.at/flo/WiFiThermostat"
- #define FIRMWARE_COPYRIGHT "FloKra"
- #define FIRMWARE_COPYRIGHT_URL "https://www.flokra.at/"
- // 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
- //confDev
- #define DEVICE_NAME "WiFiThermo-1"
- #define HOST_NAME ""
- #define WIFI_APMODE_PASSWORD "LassMiRein"
- //confWeb
- #define HTTP_SET_TOKEN "grzbrz"
- #define DEFAULT_HTTP_USER ""
- #define DEFAULT_HTTP_PASS ""
- //confMqtt
- #define MQTT_ENABLE true
- #define MQTT_SERVER "mqtt.lan"
- #define MQTT_PORT 1883
- #define MQTT_USER ""
- #define MQTT_PASS ""
- #define MQTT_TOPIC_IN "Test/Thermostat"
- #define MQTT_TOPIC_OUT "Test/Thermostat/stat"
- #define MQTT_OUT_RETAIN false
- #define MQTT_WILLTOPIC "Test/Thermostat/availability"
- #define MQTT_WILLQOS 2
- #define MQTT_WILLRETAIN false
- #define MQTT_WILLMSG "offline"
- #define MQTT_CONNMSG "online"
- #define MQTT_ENABLE_HEARTBEAT true
- #define MQTT_HEARTBEAT_MAXAGE 180000 // 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_REBOOT 1800000 // max interval for MQTT heartbeat message. only applicable if MQTT IN-topic is defined. after this timeout the ESP will reboot
- //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 true
- #define DEFAULT_MEASURE_INTERVAL 15 // interval for temp/hum measurement
- #define DEFAULT_DISPLAY_INTERVAL 5 // interval for display updates (if out-temp is active, display will toggle in this interval)
- #define DEFAULT_DISPLAY_TIMEOUT 30 // display timeout after keypress (illumination)
- #define DEFAULT_PIR_ENABLES_DISPLAY false
- #define DEFAULT_TOGGLING_I_O_TEMPHUM false
- //confAdv
- #define DEFAULT_HYSTERESIS 0.2 // hysteresis, normally 0.1 - 0.5
- #define DEFAULT_HEATING_MIN_OFFTIME 120 // minimal time the heating keeps turned off in s
- #define TEMPSENSOR_CORRECTION_VALUE 0.0 // correction value for temperature sensor reading
- #define HUMSENSOR_CORRECTION_VALUE 0 // correction value for humidity sensor reading
- #define SETTEMP_DECREASE_VALUE 0.0 // decreases the set temp to overcome further temperature rise when the heating is already switched off
- #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 "none"
- #define PRESET_NAME_1 "reduction1"
- #define PRESET_NAME_2 "reduction2"
- //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"
- // other (not configurable via WebIF)
- #define DEFAULT_SETTEMP_HEATOFF 5.0 // set temperature in OFF mode (freezing guard > 0)
- // 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 18.0 // set temperature in night/low mode
- // default values that can only be configured at compile time / hardware configuration
- #define BUTTON_DEBOUNCE_TIME 120
- #define BUTTON_HOLD_TIME 750
- #define SETTEMP_LOW_MIN 14.0 // minimal configurable temperature for reduction mode
- #define SETTEMP_LOW_MAX 21.5 // maximal configurable temperature for reduction mode
- // 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
- #define LCDLINES 2
- // default logic levels
- #define RELAISONSTATE HIGH
- #define BUTTONONSTATE LOW
- // END PRE-COMPILE CONFIGURATION
- //---------------------------------------------------------------------------------------------------------------------------------------------
- #include <Button.h>
- #include <ButtonEventCallback.h>
- #include <PushButton.h>
- #include <Bounce2.h>
- PushButton buttonPlus = PushButton(PIN_BUTTON_PLUS, ENABLE_INTERNAL_PULLUP);
- PushButton buttonMinus = PushButton(PIN_BUTTON_MINUS, ENABLE_INTERNAL_PULLUP);
- PushButton buttonMode = PushButton(PIN_BUTTON_MODE, ENABLE_INTERNAL_PULLUP);
- PushButton pirSensor = PushButton(PIN_PIRSENSOR, PRESSED_WHEN_HIGH);
- #include <PersWiFiManager.h>
- #include <ArduinoJson.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include "PubSubClient.h"
- #include <DNSServer.h>
- #include <FS.h>
- #include <Wire.h>
- #include "LiquidCrystal_I2C.h"
- #include <DHT.h>
- #include <string.h>
- boolean serialdebug = DEBUG_SERIAL;
- boolean mqttdebug = DEBUG_MQTT;
- boolean WifiInApMode = false;
- unsigned long WifiApModeStartedAt;
- // config variables - do not change here!
- // confDev
- char deviceName[31]; // device name - for web interface and AP-Mode SSID
- char hostName[31]; // announced hostname on WiFi connection
- char wifiAPModePassword[31];
- // confWeb
- char http_token[31];
- char http_user[31];
- char http_pass[31];
- boolean http_user_auth = false;
- char http_user1[31];
- char http_pass1[31];
- char http_user2[31];
- char http_pass2[31];
- // confMqtt
- boolean mqtt_enable = MQTT_ENABLE;
- char mqtt_server[41];
- int mqtt_port = MQTT_PORT;
- char mqtt_user[31];
- char mqtt_pass[31];
- char mqtt_topic_in[51]; // MQTT in topic for commands
- char mqtt_topic_out[51]; // MQTT out base topic, will be extended by various value names
- char mqtt_willTopic[51]; // MQTT Last Will topic
- int mqtt_willQos = MQTT_WILLQOS; // MQTT Last Will topic QOS
- boolean mqtt_willRetain = MQTT_WILLRETAIN; // MQTT Last Will retain
- char mqtt_willMsg[31]; // MQTT Last Will payload
- char mqtt_connMsg[31];
- boolean mqtt_outRetain = MQTT_OUT_RETAIN; // send MQTT out with retain flag
- boolean mqtt_enable_heartbeat = MQTT_ENABLE_HEARTBEAT;
- unsigned long mqtt_heartbeat_maxage_reconnect = MQTT_HEARTBEAT_MAXAGE;
- unsigned long mqtt_heartbeat_maxage_reboot = MQTT_HEARTBEAT_MAXAGE_REBOOT;
- // confBas
- float setTempMin = DEFAULT_SETTEMP_MIN; // minimal temperature that can be set
- float setTempMax = DEFAULT_SETTEMP_MAX; // maximal temperature that can be set
- boolean autoSaveSetTemp = AUTOSAVE_SETTEMP;
- boolean autoSaveHeatingMode = AUTOSAVE_SETMODE;
- int measureInterval = DEFAULT_MEASURE_INTERVAL; // interval for temp/hum measurement
- int displayInterval = DEFAULT_DISPLAY_INTERVAL; // interval for display updates (if out-temp is active, display will toggle in this interval)
- int displayInterval_saved = DEFAULT_DISPLAY_INTERVAL;
- int displayTimeout = DEFAULT_DISPLAY_TIMEOUT; // display timeout after keypress (illumination)
- boolean PIR_enablesDisplay = DEFAULT_PIR_ENABLES_DISPLAY; // PIR sensor enables display illumination
- boolean togglingTempHumAIDisplay = DEFAULT_TOGGLING_I_O_TEMPHUM;
- // confAdv
- float hysteresis = DEFAULT_HYSTERESIS; // hysteresis, normally 0.1 - 0.5
- int heatingMinOffTime = DEFAULT_HEATING_MIN_OFFTIME; // minimal time the heating keeps turned off in s
- float tempCorrVal = TEMPSENSOR_CORRECTION_VALUE; // correction value for temperature sensor reading
- int humCorrVal = HUMSENSOR_CORRECTION_VALUE; // correction value for humidity sensor reading
- float setTempDecreaseVal = SETTEMP_DECREASE_VALUE; // decreases the set temp to overcome further temperature rise when the heating is already switched off
- char offMessage[15];
- char itemplab[2];
- char otemplab[2];
- char modename0[15];
- char modename1[15];
- char psetname0[15];
- char psetname1[15];
- char psetname2[15];
- // confAdd
- char mqtt_topic_pir[51];
- char mqtt_payload_pir_on[10];
- char mqtt_payload_pir_off[10];
- char outTemp_topic_in[51];
- char outHum_topic_in[51];
- boolean mqtt_tempDisabled_credentialError = false;
- char mqtt_topic_in_cmd[61];
- char mqtt_topic_in_setTemp[61];
- char mqtt_topic_in_setMode[61];
- char mqtt_topic_in_setPreset[61];
- // set values
- float setTemp = DEFAULT_SETTEMP;
- float setTempLow = DEFAULT_SETTEMP_LOW; // set temperature in night/low mode
- float setTempLow2 = DEFAULT_SETTEMP_LOW2; // set temperature in night/low mode
- float currSetTemp = DEFAULT_SETTEMP;
- byte heatingMode = DEFAULT_HEATINGMODE; // 0 = off, 1 = heat
- byte preset = DEFAULT_PRESET; // 0 = normal/day, 1 = night/reduction 1, 2 = reduction 2 (long term leave)
- // saved set values (same as above, held in memory additionally to prevent saving unchanged values)
- float setTempSaved;
- float setTempLowSaved;
- float setTempLow2Saved;
- byte heatingModeSaved;
- byte presetSaved;
- // global pre set conf variables - not changeable via configuration
- float setTempLowMin = SETTEMP_LOW_MIN;
- float setTempLowMax = SETTEMP_LOW_MAX;
- int debounceTime = BUTTON_DEBOUNCE_TIME;
- int buttonHoldTime = BUTTON_HOLD_TIME;
- int maxMeasurementAge = MAX_MEASUREMENT_AGE;
- int maxMeasurementAgeOut = MAX_MEASUREMENT_AGE_OUT;
- // global variables for program flow
- float currTemp; // last reading from DHT sensor with smoothing
- float currTemp_raw; // last reading from DHT sensor (raw)
- int currHum; // last reading from DHT sensor with smoothing
- int currHum_raw; // last reading from DHT sensor (raw)
- boolean turnHeatingOn = false; // true if heating is active (relais switched on)
- unsigned long heatingLastOnMillis; // last time heating was switched on
- unsigned long heatingLastOffMillis; // last time heating was switched off
- float outTemp; // outside temp (via MQTT if enabled and in-topic configured)
- int outHum; // outside temp (via MQTT if enabled and in-topic configured)
- long outTempHumLastUpdate; // last reading from out temp/hum source
- char outTemp_newValue[6];
- boolean outTemp_parseNewValue;
- char outHum_newValue[4];
- boolean outHum_parseNewValue;
- char currentModeName[15];
- char currentPresetName[15];
- byte whichTempToDisplay; // 1=temp inside (from DHT sensor), 2= temp outside (via MQTT) - if out temp/hum available this value and the displayed value pair toggles with every displayInterval
- unsigned long lastMeasure = 0; // millis of last temp/hum measurement
- unsigned long lastDisplayUpdate = 0; // millis of last display update
- unsigned long lastDisplayToggle = 0; // millis of last display toggle
- unsigned long lastTempUpdate = 0; // last update time of DHT reading
- char msg[50]; // buffer MQTT in payload
- char topic[50]; // buffer MQTT in topic
- boolean displayActive = false; // gets true when button is pressed. display light gets switched on until timeout. button actions are only performed while display is active
- boolean PIRSensorOn = false;
- unsigned long heatingOnTime, heatingOffTime;
- 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
- unsigned long lastUpdate_setTempLow2 = 0; // set to millis() every time setTemp value is changed
- unsigned long lastUpdate_heatingMode = 0; // set to millis() every time heatingMode value is changed
- unsigned long lastUpdate_preset = 0; // set to millis() every time preset value is changed
- char cmdPayload[101]; // buffer for commands
- boolean cmdInQueue = false; // command is queued and will be processed next loop() run
- boolean saveConfigToFlash = false; // conf is saved in next loop() run
- boolean saveConfig2ToFlash = false; // conf2 is saved in next loop() run
- unsigned int 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
- boolean setTempAlreadySaved = true; // only save if not yet done
- boolean setTempLowAlreadySaved = true; // only save if not yet done
- boolean setTempLow2AlreadySaved = true; // only save if not yet done
- boolean heatingModeAlreadySaved = true; // only save if not yet done
- boolean presetAlreadySaved = true; // only save if not yet done
- unsigned long lastRun = 0;
- int count100ms = 0;
- int countSeconds = 0;
- int countMeasureInterval = 0;
- int countDisplayInterval = 0;
- int displayOverlayMsgTimeout = 2;
- unsigned long mqttLastReconnectAttempt = 0;
- int mqttReconnectAttempts = 0;
- int mqttReconnects = 0;
- boolean mqttConnected = false;
- unsigned long mqttLastHeartbeat;
- boolean mqttInTopicSubscribed = false;
- boolean pendingRestart = false;
- boolean doRestart = false;
- unsigned long pendingRestart_lastMillis = 0;
- boolean displayShowFullscreenMsg = false;
- //unsigned long displayShowFullscreenMsg_lastMillis = 0;
- boolean pendingPresetToggle = false;
- boolean updateDisplayImmediately = false;
- int pendingPreset;
- char pendingPresetName[15];
- //unsigned long pendingPreset_millis = 0;
- //int pendingPreset_timeout = 2000;
- boolean displayShowLine2OverlayMsg = false;
- // build Uptime String (unnecessary, i know ;) )
- unsigned int sysUptime_days = 0;
- unsigned int sysUptime_hours = 0;
- unsigned int sysUptime_mins = 0;
- char uptimeStr[15];
- DHT dht(PIN_DHTSENSOR, DHTTYPE);
- LiquidCrystal_I2C lcd(LCDADDR, LCDCOLS, LCDLINES);
- WiFiClient espClient;
- void mqttCallback(char* topic, byte* payload, unsigned int length);
- PubSubClient mqttclient(espClient);
- ESP8266WebServer httpServer(80);
- DNSServer dnsServer;
- PersWiFiManager persWM(httpServer, dnsServer);
- ESP8266HTTPUpdateServer httpUpdater;
- void setup() {
- Serial.begin(115200);
- delay(500);
- Serial.println();
- Serial.print(FIRMWARE_NAME);
- Serial.print(" v");
- Serial.print(FIRMWARE_VERSION);
- Serial.println(" starting...");
- pinMode(PIN_RELAIS, OUTPUT);
- digitalWrite(PIN_RELAIS, !RELAISONSTATE);
- pinMode(PIN_PIRSENSOR, INPUT);
- buttonPlus.configureButton(configurePushButton);
- //buttonPlus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
- buttonPlus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
- buttonPlus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
- buttonMinus.configureButton(configurePushButton);
- //buttonMinus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
- buttonMinus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
- buttonMinus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
- buttonMode.configureButton(configurePushButton);
- //buttonMode.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
- //buttonMode.onHold(1000, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
- buttonMode.onHoldRepeat(1000, 1000, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld
- buttonMode.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
- pirSensor.configureButton(configurePushButton);
- //pirSensor.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
- pirSensor.onHold(500, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
- pirSensor.onRelease(500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
-
- //set conf default values (bool, int and float variables are set at declaration)
-
- // confDev
- strlcpy(deviceName, DEVICE_NAME, sizeof(deviceName));
- strlcpy(hostName, HOST_NAME, sizeof(hostName));
- strlcpy(wifiAPModePassword, WIFI_APMODE_PASSWORD, sizeof(wifiAPModePassword));
-
- //confWeb
- strlcpy(http_token, HTTP_SET_TOKEN, sizeof(http_token));
- strlcpy(http_user, DEFAULT_HTTP_USER, sizeof(http_user));
- strlcpy(http_pass, DEFAULT_HTTP_PASS, sizeof(http_pass));
-
- //confMqtt
- strlcpy(mqtt_server, MQTT_SERVER, sizeof(mqtt_server));
- strlcpy(mqtt_user, MQTT_USER, sizeof(mqtt_user));
- strlcpy(mqtt_pass, MQTT_PASS, sizeof(mqtt_pass));
- strlcpy(mqtt_topic_in, MQTT_TOPIC_IN, sizeof(mqtt_topic_in));
- strlcpy(mqtt_topic_out, MQTT_TOPIC_OUT, sizeof(mqtt_topic_out));
- strlcpy(mqtt_willTopic, MQTT_WILLTOPIC, sizeof(mqtt_willTopic));
- strlcpy(mqtt_willMsg, MQTT_WILLMSG, sizeof(mqtt_willMsg));
- strlcpy(mqtt_connMsg, MQTT_CONNMSG, sizeof(mqtt_connMsg));
- //confBas
- // all values are int, float, boolean - set at var declaration
-
- //confAdv
- strlcpy(offMessage, OFF_MESSAGE, sizeof(offMessage));
- strlcpy(itemplab, INSIDE_TEMP_LABEL, sizeof(itemplab));
- strlcpy(otemplab, OUTSIDE_TEMP_LABEL, sizeof(otemplab));
- strlcpy(modename0, MODE_NAME_0, sizeof(modename0));
- strlcpy(modename1, MODE_NAME_1, sizeof(modename1));
- strlcpy(psetname0, PRESET_NAME_0, sizeof(psetname0));
- strlcpy(psetname1, PRESET_NAME_1, sizeof(psetname1));
- strlcpy(psetname2, PRESET_NAME_2, sizeof(psetname2));
-
- //confAdd
- strlcpy(mqtt_topic_pir, MQTT_TOPIC_PIR, sizeof(mqtt_topic_pir));
- strlcpy(mqtt_payload_pir_on, MQTT_TOPIC_PIR_ON, sizeof(mqtt_payload_pir_on));
- strlcpy(mqtt_payload_pir_off, MQTT_TOPIC_PIR_OFF, sizeof(mqtt_payload_pir_off));
- strlcpy(outTemp_topic_in, OUTTEMP_TOPIC_IN, sizeof(outTemp_topic_in));
- strlcpy(outHum_topic_in, OUTHUM_TOPIC_IN, sizeof(outHum_topic_in));
-
- if(serialdebug) Serial.println("default config values loaded..");
- initDisplay();
- Serial.println(F("Mounting SPIFFS..."));
- if (!SPIFFS.begin()) {
- Serial.println(F("Failed to mount SPIFFS"));
- return;
- }
- #ifdef FORCE_SPIFFS_FORMAT
- SPIFFS.format();
- Serial.print(F("Format SPIFFS complete."));
- #endif
- if (!SPIFFS.exists("/formatted")) {
- Serial.println(F("formatting SPIFFS, please wait 30 secs"));
- SPIFFS.format();
- Serial.println(F("SPIFFS formatted"));
- File f = SPIFFS.open("/formatted", "w");
- if (!f) {
- Serial.println(F("creating file '/formatted' failed"));
- } else {
- f.println(F("Format Complete"));
- }
- f.close();
- } else {
- if(serialdebug) Serial.println(F("SPIFFS is formatted. Moving along..."));
- }
- Serial.println(F("files in SPIFFS:"));
- Dir dir = SPIFFS.openDir("/");
- while (dir.next()) {
- Serial.print(dir.fileName());
- File f = dir.openFile("r");
- Serial.print(" ");
- Serial.println(f.size());
- f.close();
- }
- Serial.println(F("----"));
-
- loadConfigs();
- loadSavedValues();
-
- //if configuration returned empty strings - use the defaults where a values is necessary
- if (strlen(deviceName) < 4) strlcpy(deviceName, DEVICE_NAME, 31);
- if (modename0[0] == '\0') strlcpy(modename0, MODE_NAME_0, 15);
- if (modename1[0] == '\0') strlcpy(modename1, MODE_NAME_1, 15);
- if (psetname0[0] == '\0') strlcpy(psetname0, PRESET_NAME_0, 15);
- if (psetname1[0] == '\0') strlcpy(psetname1, PRESET_NAME_1, 15);
- if (psetname2[0] == '\0') strlcpy(psetname2, PRESET_NAME_2, 15);
- if (itemplab[0] == '\0') strlcpy(itemplab, INSIDE_TEMP_LABEL, 2);
- if (otemplab[0] == '\0') strlcpy(otemplab, OUTSIDE_TEMP_LABEL, 2);
- updateCurrentHeatingModeName();
- updateCurrentPresetName();
- // initialize DHT11/22 temp/hum sensor
- dht.begin();
- if (strlen(hostName) >= 4) { // if no hostname is set WiFi manager will create a unique one automatically based on MAC address
- WiFi.hostname(hostName);
- }
- //optional code handlers to run everytime wifi is connected...
- persWM.onConnect([]() {
- Serial.print("WiFi connected to '");
- Serial.print(WiFi.SSID());
- Serial.print("' with IP: ");
- Serial.println(WiFi.localIP());
- WifiInApMode = false;
- displayShowWifiConnected();
- });
- //...or AP mode is started
- persWM.onAp([]() {
- Serial.print("WiFi in AP mode with SSID '");
- Serial.print(persWM.getApSsid());
- if (strlen(wifiAPModePassword) >= 4) {
- Serial.print("' and password '");
- Serial.print(wifiAPModePassword);
- }
- Serial.println("'");
- WifiInApMode = true;
- WifiApModeStartedAt = millis();
- displayShowWifiConnectionError();
- });
- // sets network name and password for AP mode
- char wifiAPModeSSID[45];
- sprintf(wifiAPModeSSID, "%s-%s", FIRMWARE_NAME, deviceName);
- if (strlen(wifiAPModePassword) < 4) persWM.setApCredentials(wifiAPModeSSID);
- else persWM.setApCredentials(wifiAPModeSSID, wifiAPModePassword);
- //make connecting/disconnecting non-blocking
- persWM.setConnectNonBlock(true);
- //in non-blocking mode, program will continue past this point without waiting
- persWM.begin();
- delay(500);
- httpServerInit();
- if (mqtt_enable) {
- mqttPrepareSubscribeTopics();
- mqttClientInit();
- }
- Serial.println("setup complete.");
- } //void setup
- void loop() {
- checkMillis();
- persWM.handleWiFi(); //in non-blocking mode, handleWiFi must be called in the main loop
- yield();
- mqttHandleConnection();
- yield();
- outTempHum_updateOnNewValue();
- yield();
- dnsServer.processNextRequest();
- httpServer.handleClient();
- buttonPlus.update();
- buttonMinus.update();
- buttonMode.update();
- pirSensor.update();
- yield();
- evalCmd();
- yield();
-
- if (Serial.available()) {
- serialEvent();
- yield();
- }
- if (updateDisplayImmediately) {
- updateDisplayImmediately = false;
- updateDisplay();
- }
- } //void loop
|