From 37318944863b3f04ae065e75dee2935b5c6c0b5f Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Fri, 11 Nov 2022 11:15:05 +0100 Subject: [PATCH 1/2] Added ``FUNC_NETWORK_UP`` and ``FUNC_NETWORK_DOWN`` events --- API.md | 2 + CHANGELOG.md | 1 + tasmota/include/tasmota.h | 3 +- tasmota/tasmota_support/support.ino | 4 - tasmota/tasmota_support/support_tasmota.ino | 81 ++++++++++--------- .../tasmota_xdrv_driver/xdrv_03_energy.ino | 6 ++ tasmota/tasmota_xdrv_driver/xdrv_04_light.ino | 6 ++ tasmota/tasmota_xdrv_driver/xdrv_11_knx.ino | 11 +++ tasmota/tasmota_xdrv_driver/xdrv_20_hue.ino | 6 ++ tasmota/tasmota_xdrv_driver/xdrv_21_wemo.ino | 6 ++ .../xdrv_21_wemo_multi.ino | 6 ++ 11 files changed, 89 insertions(+), 43 deletions(-) diff --git a/API.md b/API.md index b631172ed..2dbc5c555 100644 --- a/API.md +++ b/API.md @@ -51,6 +51,8 @@ FUNC_SET_CHANNELS | | 2 | | | 1 | FUNC_SET_SCHEME | | | | | x | FUNC_HOTPLUG_SCAN | | | x | | | FUNC_DEVICE_GROUP_ITEM | | x | | | | +FUNC_NETWORK_UP | | 1 | 2 | 3 | 4 | Wifi or ETH network just went up (received even if webserver is not enabled) +FUNC_NETWORK_DOWN | | 1 | 2 | 3 | 4 | Wifi or ETH network just went down (received even if webserver is not enabled) The numbers represent the sequence of execution diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f5e4b364..0364f465a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. ### Added - Support for Plantower PMSx003T AQI models with temperature and humidity (#16971) - Support for Dingtian x595/x165 shift register based relay boards by Barbudor (#17032) +- Added ``FUNC_NETWORK_UP`` and ``FUNC_NETWORK_DOWN`` events ### Breaking Changed diff --git a/tasmota/include/tasmota.h b/tasmota/include/tasmota.h index faf2143ae..cc53ee79f 100644 --- a/tasmota/include/tasmota.h +++ b/tasmota/include/tasmota.h @@ -387,7 +387,8 @@ enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_PIN_STATE, FUNC_I2C_INIT, FUNC_ FUNC_RULES_PROCESS, FUNC_TELEPERIOD_RULES_PROCESS, FUNC_SERIAL, FUNC_FREE_MEM, FUNC_BUTTON_PRESSED, FUNC_BUTTON_MULTI_PRESSED, FUNC_WEB_ADD_BUTTON, FUNC_WEB_ADD_CONSOLE_BUTTON, FUNC_WEB_ADD_MANAGEMENT_BUTTON, FUNC_WEB_ADD_MAIN_BUTTON, FUNC_WEB_GET_ARG, FUNC_WEB_ADD_HANDLER, FUNC_SET_CHANNELS, FUNC_SET_SCHEME, FUNC_HOTPLUG_SCAN, FUNC_TIME_SYNCED, - FUNC_DEVICE_GROUP_ITEM }; + FUNC_DEVICE_GROUP_ITEM, + FUNC_NETWORK_UP, FUNC_NETWORK_DOWN }; enum AddressConfigSteps { ADDR_IDLE, ADDR_RECEIVE, ADDR_SEND }; diff --git a/tasmota/tasmota_support/support.ino b/tasmota/tasmota_support/support.ino index 2e1e77dd0..6446a44d8 100755 --- a/tasmota/tasmota_support/support.ino +++ b/tasmota/tasmota_support/support.ino @@ -21,10 +21,6 @@ extern "C" { extern struct rst_info resetInfo; } -#ifdef USE_KNX -bool knx_started = false; -#endif // USE_KNX - /*********************************************************************************************\ * Watchdog extension (https://github.com/esp8266/Arduino/issues/1532) \*********************************************************************************************/ diff --git a/tasmota/tasmota_support/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index 7f8008965..a0263fc7e 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -1569,76 +1569,81 @@ void Every250mSeconds(void) WifiDisable(); } break; - case 3: // Every x.75 second - if (!TasmotaGlobal.global_state.network_down) { + case 3: + { + // is there a network state change since last time, if so send events to modules + static bool network_was_down = true; // keep track of the previous state of network + bool network_state_changed = (network_was_down != (bool)TasmotaGlobal.global_state.network_down); // network state changed from last tick + network_was_down = TasmotaGlobal.global_state.network_down; + + if (!TasmotaGlobal.global_state.network_down) { #ifdef FIRMWARE_MINIMAL #ifdef CONFIG_IDF_TARGET_ESP32C3 - if (OtaFactoryRead()) { - OtaFactoryWrite(false); - TasmotaGlobal.ota_state_flag = 3; - } + if (OtaFactoryRead()) { + OtaFactoryWrite(false); + TasmotaGlobal.ota_state_flag = 3; + } #endif - if (1 == RtcSettings.ota_loader) { - RtcSettings.ota_loader = 0; - TasmotaGlobal.ota_state_flag = 3; - } + if (1 == RtcSettings.ota_loader) { + RtcSettings.ota_loader = 0; + TasmotaGlobal.ota_state_flag = 3; + } #endif // FIRMWARE_MINIMAL #ifdef USE_DISCOVERY - StartMdns(); + StartMdns(); #endif // USE_DISCOVERY #ifdef USE_WEBSERVER - if (Settings->webserver) { + if (Settings->webserver) { #ifdef ESP8266 - if (!WifiIsInManagerMode()) { StartWebserver(Settings->webserver, WiFi.localIP()); } + if (!WifiIsInManagerMode()) { StartWebserver(Settings->webserver, WiFi.localIP()); } #endif // ESP8266 #ifdef ESP32 #ifdef USE_ETHERNET - StartWebserver(Settings->webserver, (EthernetLocalIP()) ? EthernetLocalIP() : WiFi.localIP()); + StartWebserver(Settings->webserver, (EthernetLocalIP()) ? EthernetLocalIP() : WiFi.localIP()); #else - StartWebserver(Settings->webserver, WiFi.localIP()); + StartWebserver(Settings->webserver, WiFi.localIP()); #endif #endif // ESP32 #ifdef USE_DISCOVERY #ifdef WEBSERVER_ADVERTISE - MdnsAddServiceHttp(); + MdnsAddServiceHttp(); #endif // WEBSERVER_ADVERTISE #endif // USE_DISCOVERY - } else { - StopWebserver(); - } -#ifdef USE_EMULATION - if (Settings->flag2.emulation) { UdpConnect(); } -#endif // USE_EMULATION + + } else { + StopWebserver(); + } #endif // USE_WEBSERVER #ifdef USE_DEVICE_GROUPS - DeviceGroupsStart(); + DeviceGroupsStart(); #endif // USE_DEVICE_GROUPS -#ifdef USE_KNX - if (!knx_started && Settings->flag.knx_enabled) { // CMND_KNX_ENABLED - KNXStart(); - knx_started = true; - } -#endif // USE_KNX + // send FUNC_NETWORK_UP to all modules + if (network_state_changed) { + // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("WIF: Sending FUNC_NETWORK_UP")); + XdrvCall(FUNC_NETWORK_UP); + XsnsCall(FUNC_NETWORK_UP); + } - MqttCheck(); - } else { -#ifdef USE_EMULATION - UdpDisconnect(); -#endif // USE_EMULATION + MqttCheck(); + } else { #ifdef USE_DEVICE_GROUPS - DeviceGroupsStop(); + DeviceGroupsStop(); #endif // USE_DEVICE_GROUPS -#ifdef USE_KNX - knx_started = false; -#endif // USE_KNX + // send FUNC_NETWORK_UP to all modules + if (network_state_changed) { + // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("WIF: Sending FUNC_NETWORK_DOWN")); + XdrvCall(FUNC_NETWORK_DOWN); + XsnsCall(FUNC_NETWORK_DOWN); + } + } // Every x.75 second } break; } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino index 9807fd5f4..5795800d1 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino @@ -1415,6 +1415,12 @@ bool Xdrv03(uint32_t function) case FUNC_COMMAND: result = DecodeCommand(kEnergyCommands, EnergyCommand); break; + case FUNC_NETWORK_UP: + XnrgCall(FUNC_NETWORK_UP); + break; + case FUNC_NETWORK_DOWN: + XnrgCall(FUNC_NETWORK_DOWN); + break; } } return result; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino b/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino index a3d00b3f7..164e4d708 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino @@ -3428,6 +3428,12 @@ bool Xdrv04(uint32_t function) case FUNC_BUTTON_MULTI_PRESSED: result = XlgtCall(FUNC_BUTTON_MULTI_PRESSED); break; + case FUNC_NETWORK_UP: + XlgtCall(FUNC_NETWORK_UP); + break; + case FUNC_NETWORK_DOWN: + XlgtCall(FUNC_NETWORK_DOWN); + break; #ifdef USE_WEBSERVER case FUNC_WEB_ADD_MAIN_BUTTON: XlgtCall(FUNC_WEB_ADD_MAIN_BUTTON); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_11_knx.ino b/tasmota/tasmota_xdrv_driver/xdrv_11_knx.ino index b8573cb3f..04d1225d3 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_11_knx.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_11_knx.ino @@ -52,6 +52,8 @@ uint8_t Settings->knx_CB_param[MAX_KNX_CB] Type of Output (set relay, #include // KNX Library +bool knx_started = false; + address_t KNX_physs_addr; // Physical KNX address of this device address_t KNX_addr; // KNX Address converter variable @@ -1329,6 +1331,15 @@ bool Xdrv11(uint32_t function) case FUNC_PRE_INIT: KNX_INIT(); break; + case FUNC_NETWORK_UP: + if (!knx_started && Settings->flag.knx_enabled) { // CMND_KNX_ENABLED + KNXStart(); + knx_started = true; + } + break; + case FUNC_NETWORK_DOWN: + knx_started = false; + break; // case FUNC_SET_POWER: // break; } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_20_hue.ino b/tasmota/tasmota_xdrv_driver/xdrv_20_hue.ino index 30db910b5..74611fa78 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_20_hue.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_20_hue.ino @@ -1162,6 +1162,12 @@ bool Xdrv20(uint32_t function) case FUNC_WEB_ADD_HANDLER: WebServer_on(PSTR("/description.xml"), HandleUpnpSetupHue); break; + case FUNC_NETWORK_UP: + UdpConnect(); + break; + case FUNC_NETWORK_DOWN: + UdpDisconnect(); + break; } } return result; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_21_wemo.ino b/tasmota/tasmota_xdrv_driver/xdrv_21_wemo.ino index 1bcd0b9ab..0d433fbdf 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_21_wemo.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_21_wemo.ino @@ -354,6 +354,12 @@ bool Xdrv21(uint32_t function) WebServer_on(PSTR("/metainfoservice.xml"), HandleUpnpMetaService); WebServer_on(PSTR("/setup.xml"), HandleUpnpSetupWemo); break; + case FUNC_NETWORK_UP: + UdpConnect(); + break; + case FUNC_NETWORK_DOWN: + UdpDisconnect(); + break; } } return result; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_21_wemo_multi.ino b/tasmota/tasmota_xdrv_driver/xdrv_21_wemo_multi.ino index bf242d0f4..5ceb84ac9 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_21_wemo_multi.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_21_wemo_multi.ino @@ -453,6 +453,12 @@ bool Xdrv21(uint32_t function) numOfWemoSwitch++; } break; + case FUNC_NETWORK_UP: + UdpConnect(); + break; + case FUNC_NETWORK_DOWN: + UdpDisconnect(); + break; } } return result; From 0628d2f1a5205ca87b8e2f84aa42556a72919d10 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Fri, 11 Nov 2022 11:34:32 +0100 Subject: [PATCH 2/2] Use `XdrvXsnsCall` --- tasmota/tasmota_support/support_tasmota.ino | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tasmota/tasmota_support/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index a0263fc7e..f6fd5981f 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -1626,8 +1626,7 @@ void Every250mSeconds(void) // send FUNC_NETWORK_UP to all modules if (network_state_changed) { // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("WIF: Sending FUNC_NETWORK_UP")); - XdrvCall(FUNC_NETWORK_UP); - XsnsCall(FUNC_NETWORK_UP); + XdrvXsnsCall(FUNC_NETWORK_UP); } MqttCheck(); @@ -1640,8 +1639,7 @@ void Every250mSeconds(void) // send FUNC_NETWORK_UP to all modules if (network_state_changed) { // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("WIF: Sending FUNC_NETWORK_DOWN")); - XdrvCall(FUNC_NETWORK_DOWN); - XsnsCall(FUNC_NETWORK_DOWN); + XdrvXsnsCall(FUNC_NETWORK_DOWN); } } // Every x.75 second }