From ae6fcd4d605011475dd8b68e900b23274d982b93 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 24 Aug 2018 16:38:55 +0200 Subject: [PATCH] Change sleep command Change command sleep from restart after change to not restart after change (#3554) --- sonoff/_changelog.ino | 1 + sonoff/sonoff.ino | 2 +- sonoff/support.ino | 26 +++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 15f4607d1..df9f78284 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -4,6 +4,7 @@ * Add GPIO options ButtonXn, SwitchXn and CounterXn to select INPUT mode instead of INPUT_PULLUP (#2525) * Fix OtaMagic when file path contains a dash (-) (#3563) * Fix EnergyReset3 (#2723) + * Change command sleep from restart after change to not restart after change (#3554) * * 6.1.1.8 * Fix MQTT reconnection detection when using TasmotaMqtt library (#3558) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index aec8a07cb..7fd244f6d 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -562,9 +562,9 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) } else if (CMND_SLEEP == command_code) { if ((payload >= 0) && (payload < 251)) { - if ((!Settings.sleep && payload) || (Settings.sleep && !payload)) restart_flag = 2; Settings.sleep = payload; sleep = payload; + WiFiSetSleepMode(); } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_UNIT_NVALUE_UNIT, command, sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : "", Settings.sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : ""); } diff --git a/sonoff/support.ino b/sonoff/support.ino index cf62cdba0..911e75a9c 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1079,6 +1079,28 @@ void WifiConfig(uint8_t type) } } +void WiFiSetSleepMode() +{ +/* Excerpt from the esp8266 non os sdk api reference (v2.2.1): + * Sets sleep type for power saving. Set WIFI_NONE_SLEEP to disable power saving. + * - Default mode: WIFI_MODEM_SLEEP. + * - In order to lower the power comsumption, ESP8266 changes the TCP timer + * tick from 250ms to 3s in WIFI_LIGHT_SLEEP mode, which leads to increased timeout for + * TCP timer. Therefore, the WIFI_MODEM_SLEEP or deep-sleep mode should be used + * where there is a requirement for the accurancy of the TCP timer. + */ + +#ifndef ARDUINO_ESP8266_RELEASE_2_4_1 // See https://github.com/arendst/Sonoff-Tasmota/issues/2559 - Sleep bug in SDK + + if (sleep) { + WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times + } else { + WiFi.setSleepMode(WIFI_MODEM_SLEEP); // Diable sleep (Esp8288/Arduino core and sdk default) + } + +#endif +} + void WifiBegin(uint8_t flag) { const char kWifiPhyMode[] = " BGN"; @@ -1095,9 +1117,7 @@ void WifiBegin(uint8_t flag) WiFi.disconnect(true); // Delete SDK wifi config delay(200); WiFi.mode(WIFI_STA); // Disable AP mode -#ifndef ARDUINO_ESP8266_RELEASE_2_4_1 // See https://github.com/arendst/Sonoff-Tasmota/issues/2559 - Sleep bug - if (Settings.sleep) { WiFi.setSleepMode(WIFI_LIGHT_SLEEP); } // Allow light sleep during idle times -#endif + WiFiSetSleepMode(); // if (WiFi.getPhyMode() != WIFI_PHY_MODE_11N) { WiFi.setPhyMode(WIFI_PHY_MODE_11N); } if (!WiFi.getAutoConnect()) { WiFi.setAutoConnect(true); } // WiFi.setAutoReconnect(true);