Skip to content

Commit

Permalink
Auto restart on WiFi drop
Browse files Browse the repository at this point in the history
  • Loading branch information
dzungpv committed Jun 5, 2023
1 parent b28b0b4 commit f23cae2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/mitsubishi2mqtt/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

const PROGMEM char* m2mqtt_version = "0.6.2.2";
const PROGMEM char* m2mqtt_version = "0.6.3";

//Define global variables for files
#ifdef ESP32
Expand All @@ -38,6 +38,9 @@ const PROGMEM uint8_t redLedPin = 0;

// Define global variables for network
const PROGMEM char* hostnamePrefix = "HVAC_";
const PROGMEM uint32_t WIFI_RETRY_INTERVAL_MS = 300000; //300 seconds
unsigned long wifi_timeout;
bool wifi_config_exists;
String hostname = "";
String ap_ssid;
String ap_pwd;
Expand Down
24 changes: 16 additions & 8 deletions src/mitsubishi2mqtt/mitsubishi2mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void setup() {
WiFi.hostname(hostname.c_str());
#endif
setDefaults();
loadWifi();
wifi_config_exists = loadWifi();
loadAdvance();
if (initWifi()) {
if (SPIFFS.exists(console_file)) {
Expand Down Expand Up @@ -438,6 +438,7 @@ boolean initWifi() {

Serial.println(F("\n\r \n\rStarting in AP mode"));
WiFi.mode(WIFI_AP);
wifi_timeout = millis() + WIFI_RETRY_INTERVAL_MS;
WiFi.persistent(false); //fix crash esp32 https://github.com/espressif/arduino-esp32/issues/2025
if (!connectWifiSuccess) {
// Set AP password when falling back to AP on fail
Expand Down Expand Up @@ -1291,8 +1292,8 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
else if (modeUpper == "FAN_ONLY") {
modeUpper = "FAN";
hpSendDummy("action", "fan_only", "mode", "fan_only");
}
}

if (modeUpper == "OFF") {
hp.setPowerSetting("OFF");
hpSendDummy("action", "off", "mode", "off");
Expand All @@ -1311,8 +1312,8 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
hp.setModeSetting(currentSettings.mode);
//
float temperature = strtof(message, NULL);
if(!(temperature>=min_temp&&temperature<=max_temp)){
temperature = 23;
if (!(temperature >= min_temp && temperature <= max_temp)) {
temperature = 23;
}
const size_t bufferSize = JSON_OBJECT_SIZE(2);
StaticJsonDocument<bufferSize> root;
Expand Down Expand Up @@ -1393,7 +1394,7 @@ void haConfig() {
haConfig["temp_step"] = temp_step;
haConfig["pow_cmd_t"] = ha_power_set_topic;
haConfig["avty_t"] = ha_availability_topic;

JsonArray haConfigFan_modes = haConfig.createNestedArray("fan_modes");
haConfigFan_modes.add("AUTO");
if (supportQuietMode) {
Expand Down Expand Up @@ -1489,8 +1490,8 @@ bool connectWifi() {
#endif
WiFi.begin(ap_ssid.c_str(), ap_pwd.c_str());
Serial.println("Connecting to " + ap_ssid);
unsigned long wifiStartTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - wifiStartTime < 10000) {
wifi_timeout = millis() + 30000;
while (WiFi.status() != WL_CONNECTED && millis() < wifi_timeout) {
Serial.write('.');
//Serial.print(WiFi.status());
// wait 500ms, flashing the blue LED to indicate WiFi connecting...
Expand Down Expand Up @@ -1601,6 +1602,13 @@ void checkLogin() {
void loop() {
server.handleClient();
ArduinoOTA.handle();

//reset board to attempt to connect to wifi again if in ap mode or wifi dropped out and time limit passed
if (WiFi.getMode() == WIFI_STA and WiFi.status() == WL_CONNECTED) {
wifi_timeout = millis() + WIFI_RETRY_INTERVAL_MS;
} else if (wifi_config_exists and millis() > wifi_timeout) {
ESP.restart();
}
if (!captive and mqtt_config) {
// Sync HVAC UNIT
hp.sync();
Expand Down

0 comments on commit f23cae2

Please sign in to comment.