From 6be5daa77cf01decc401094607239b691ad239be Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 7 Nov 2019 14:49:29 +0100 Subject: [PATCH] Fix Tele message when DeepSleepTime is 0 Fix Tele message when DeepSleepTime is 0 (#6842) --- tasmota/support_command.ino | 3 +-- tasmota/tasmota.ino | 9 +++----- tasmota/xdrv_29_deepsleep.ino | 43 +++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 5a4c955d8..14fb7a575 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -1336,8 +1336,7 @@ void CmndTeleperiod(void) if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) { Settings.tele_period = (1 == XdrvMailbox.payload) ? TELE_PERIOD : XdrvMailbox.payload; if ((Settings.tele_period > 0) && (Settings.tele_period < 10)) Settings.tele_period = 10; // Do not allow periods < 10 seconds - tele_period = Settings.tele_period; - prep_called = false; + tele_period = Settings.tele_period; } ResponseCmndNumber(Settings.tele_period); } diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 98f19764c..b295cf664 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -157,7 +157,6 @@ bool spi_flg = false; // SPI configured bool soft_spi_flg = false; // Software SPI configured bool ntp_force_sync = false; // Force NTP sync bool ntp_synced_message = false; // NTP synced message flag -bool prep_called = false; // Deep sleep flag to detect a proper start of initialize sensors myio my_module; // Active copy of Module GPIOs (17 x 8 bits) gpio_flag my_module_flag; // Active copy of Template GPIO flags StateBitfield global_state; // Global states (currently Wifi and Mqtt) (8 bits) @@ -841,13 +840,12 @@ void PerformEverySecond(void) if (Settings.tele_period) { tele_period++; // increase time for prepare and document state to ensure TELEPERIOD deliver results - if (tele_period == Settings.tele_period -3 && !prep_called) { - // sensores must be called later if driver switch on e.g. power on deepsleep + if (tele_period == Settings.tele_period -3) { + // sensors must be called later if driver switch on e.g. power on deepsleep XdrvCall(FUNC_PREP_BEFORE_TELEPERIOD); XsnsCall(FUNC_PREP_BEFORE_TELEPERIOD); - prep_called = true; } - if (tele_period >= Settings.tele_period && prep_called) { + if (tele_period >= Settings.tele_period) { tele_period = 0; MqttPublishTeleState(); @@ -859,7 +857,6 @@ void PerformEverySecond(void) RulesTeleperiod(); // Allow rule based HA messages #endif // USE_RULES } - prep_called = true; XdrvCall(FUNC_AFTER_TELEPERIOD); } } diff --git a/tasmota/xdrv_29_deepsleep.ino b/tasmota/xdrv_29_deepsleep.ino index a3e091227..455df9f40 100644 --- a/tasmota/xdrv_29_deepsleep.ino +++ b/tasmota/xdrv_29_deepsleep.ino @@ -20,6 +20,12 @@ #ifdef USE_DEEPSLEEP /*********************************************************************************************\ * DeepSleep Support + * + * - For wakeup from DeepSleep needs GPIO16 to be connected to RST + * - GPIO_DEEPSLEEP may be used to stop DeepSleep when connected to Gnd + * - GPIO16 may be configured as GPIO_DEEPSLEEP + * + * See wiki https://github.com/arendst/Tasmota/wiki/DeepSleep \*********************************************************************************************/ #define XDRV_29 29 @@ -36,16 +42,24 @@ const char kDeepsleepCommands[] PROGMEM = D_PRFX_DEEPSLEEP "|" void (* const DeepsleepCommand[])(void) PROGMEM = { &CmndDeepsleepTime }; -const char JSON_DEEPSLEEP[] PROGMEM = "\"" D_PRFX_DEEPSLEEP "%d\":{\"Time\":%d}"; +bool DeepSleepEnabled(void) +{ + if (0 == Settings.deepsleep) { + return false; + } + + if (pin[GPIO_DEEPSLEEP] < 99) { + pinMode(pin[GPIO_DEEPSLEEP], INPUT_PULLUP); + return (digitalRead(pin[GPIO_DEEPSLEEP])); // Disable DeepSleep if user holds pin GPIO_DEEPSLEEP low + } + + return true; +} void DeepSleepInit(void) { - if (pin[GPIO_DEEPSLEEP] < 99) { - if (!digitalRead(pin[GPIO_DEEPSLEEP])) { - RtcSettings.ultradeepsleep = 0; - } - } - if ((RtcSettings.ultradeepsleep > MAX_DEEPSLEEP_CYCLE) && (RtcSettings.ultradeepsleep < 1700000000)) { + // Go back to sleep after 60 minutes if requested deepsleep has not been reached + if (DeepSleepEnabled() && (RtcSettings.ultradeepsleep > MAX_DEEPSLEEP_CYCLE) && (RtcSettings.ultradeepsleep < 1700000000)) { RtcSettings.ultradeepsleep = RtcSettings.ultradeepsleep - MAX_DEEPSLEEP_CYCLE; RtcReboot.fast_reboot_count = 0; RtcRebootSave(); @@ -53,18 +67,16 @@ void DeepSleepInit(void) RtcSettingsSave(); ESP.deepSleep(100 * RtcSettings.deepsleep_slip * (MAX_DEEPSLEEP_CYCLE < RtcSettings.ultradeepsleep ? MAX_DEEPSLEEP_CYCLE : RtcSettings.ultradeepsleep), WAKE_RF_DEFAULT); yield(); + // Sleeping } + // Stay awake RtcSettings.ultradeepsleep = 0; } -void CheckForDeepsleep(void) +void DeepSleepCheck(void) { - uint8_t disable_deepsleep_switch = 0; - if (pin[GPIO_DEEPSLEEP] < 99) { - disable_deepsleep_switch = !digitalRead(pin[GPIO_DEEPSLEEP]); - } // new function AFTER_TELEPERIOD can take some time therefore <2 - if ((Settings.deepsleep > 10) && (Settings.deepsleep < 4294967295) && !disable_deepsleep_switch && (tele_period < 2) && prep_called) { + if (DeepSleepEnabled() && (Settings.deepsleep > 10) && (Settings.deepsleep < 4294967295)) { SettingsSaveAll(); // deepsleep_slip is ideally 10.000 == 100% // typically the device has up to 4% slip. Anything else is a wrong setting in the deepsleep_slip @@ -115,8 +127,8 @@ void CheckForDeepsleep(void) RtcSettingsSave(); ESP.deepSleep(100 * RtcSettings.deepsleep_slip * sleeptime); yield(); + // Sleeping } - prep_called = false; } /*********************************************************************************************\ @@ -129,6 +141,7 @@ void CmndDeepsleepTime(void) if ((XdrvMailbox.payload == 0) || ((XdrvMailbox.payload > 10) && (XdrvMailbox.payload < (24 * 60 * 60)))) { // Allow max 24 hours sleep Settings.deepsleep = XdrvMailbox.payload; RtcSettings.nextwakeup = 0; + tele_period = Settings.tele_period -3; // Initiate start DeepSleep on next finish of forced TelePeriod } Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.deepsleep); } @@ -143,7 +156,7 @@ bool Xdrv29(uint8_t function) switch (function) { case FUNC_AFTER_TELEPERIOD: - CheckForDeepsleep(); + DeepSleepCheck(); break; case FUNC_COMMAND: result = DecodeCommand(kDeepsleepCommands, DeepsleepCommand);