diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 37c6b4e36..062f22a99 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,4 +1,5 @@ /* 6.1.1c + * Add rule triggers Wifi#Connected and Wifi#Disconnected (#3359) * Fix unsecure main webpage update * Add Turkish language file (#3332) * Fix command TimeDst/TimeStd invalid JSON (#3322) diff --git a/sonoff/settings.h b/sonoff/settings.h index e527d74b7..d94bcf0c3 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -155,7 +155,7 @@ typedef union { typedef union { uint8_t data; struct { - uint8_t pinmode : 3; // Enable INPUT + uint8_t pinmode : 3; // Enable INPUT uint8_t pullup : 1; // Enable internal weak pull-up resistor uint8_t b4 : 1; uint8_t b5 : 1; @@ -369,7 +369,7 @@ struct XDRVMAILBOX { char *data; } XdrvMailbox; -#define MAX_RULES_FLAG 5 // Number of bits used in RulesBitfield (tricky I know...) +#define MAX_RULES_FLAG 7 // Number of bits used in RulesBitfield (tricky I know...) typedef union { // Restricted by MISRA-C Rule 18.4 but so usefull... uint16_t data; // Allow bit manipulation struct { @@ -378,8 +378,8 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint16_t time_set : 1; uint16_t mqtt_connected : 1; uint16_t mqtt_disconnected : 1; - uint16_t spare05 : 1; - uint16_t spare06 : 1; + uint16_t wifi_connected : 1; + uint16_t wifi_disconnected : 1; uint16_t spare07 : 1; uint16_t spare08 : 1; uint16_t spare09 : 1; diff --git a/sonoff/support.ino b/sonoff/support.ino index 13a9d4265..5cc9b0611 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1096,10 +1096,22 @@ void WifiBegin(uint8_t flag) AddLog(LOG_LEVEL_INFO); } +void WifiState(uint8_t state) +{ + if (state == global_state.wifi_down) { + if (state) { + rules_flag.wifi_connected = 1; + } else { + rules_flag.wifi_disconnected = 1; + } + } + global_state.wifi_down = state ^1; +} + void WifiCheckIp() { if ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0)) { - global_state.wifi_down = 0; + WifiState(1); wifi_counter = WIFI_CHECK_SEC; wifi_retry = wifi_retry_init; AddLog_P((wifi_status != WL_CONNECTED) ? LOG_LEVEL_INFO : LOG_LEVEL_DEBUG_MORE, S_LOG_WIFI, PSTR(D_CONNECTED)); @@ -1111,7 +1123,7 @@ void WifiCheckIp() } wifi_status = WL_CONNECTED; } else { - global_state.wifi_down = 1; + WifiState(0); uint8_t wifi_config_tool = Settings.sta_config; wifi_status = WiFi.status(); switch (wifi_status) { @@ -1222,7 +1234,7 @@ void WifiCheck(uint8_t param) WifiCheckIp(); } if ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0) && !wifi_config_type) { - global_state.wifi_down = 0; + WifiState(1); #ifdef BE_MINIMAL if (1 == RtcSettings.ota_loader) { RtcSettings.ota_loader = 0; @@ -1258,7 +1270,7 @@ void WifiCheck(uint8_t param) } #endif // USE_KNX } else { - global_state.wifi_down = 1; + WifiState(0); #if defined(USE_WEBSERVER) && defined(USE_EMULATION) UdpDisconnect(); #endif // USE_EMULATION diff --git a/sonoff/xdrv_10_rules.ino b/sonoff/xdrv_10_rules.ino index 3296a9560..cc2ec65d6 100644 --- a/sonoff/xdrv_10_rules.ino +++ b/sonoff/xdrv_10_rules.ino @@ -432,6 +432,8 @@ void RulesEvery50ms() case 2: snprintf_P(json_event, sizeof(json_event), PSTR("{\"Time\":{\"Set\":%d}}"), GetMinutesPastMidnight()); break; case 3: strncpy_P(json_event, PSTR("{\"MQTT\":{\"Connected\":1}}"), sizeof(json_event)); break; case 4: strncpy_P(json_event, PSTR("{\"MQTT\":{\"Disconnected\":1}}"), sizeof(json_event)); break; + case 5: strncpy_P(json_event, PSTR("{\"WIFI\":{\"Connected\":1}}"), sizeof(json_event)); break; + case 6: strncpy_P(json_event, PSTR("{\"WIFI\":{\"Disconnected\":1}}"), sizeof(json_event)); break; } if (json_event[0]) { RulesProcessEvent(json_event);