Fix Tele message when DeepSleepTime is 0

Fix Tele message when DeepSleepTime is 0 (#6842)
This commit is contained in:
Theo Arends 2019-11-07 14:49:29 +01:00
parent 4b884679e6
commit 6be5daa77c
3 changed files with 32 additions and 23 deletions

View File

@ -1336,8 +1336,7 @@ void CmndTeleperiod(void)
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
Settings.tele_period = (1 == XdrvMailbox.payload) ? TELE_PERIOD : XdrvMailbox.payload; 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 if ((Settings.tele_period > 0) && (Settings.tele_period < 10)) Settings.tele_period = 10; // Do not allow periods < 10 seconds
tele_period = Settings.tele_period; tele_period = Settings.tele_period;
prep_called = false;
} }
ResponseCmndNumber(Settings.tele_period); ResponseCmndNumber(Settings.tele_period);
} }

View File

@ -157,7 +157,6 @@ bool spi_flg = false; // SPI configured
bool soft_spi_flg = false; // Software SPI configured bool soft_spi_flg = false; // Software SPI configured
bool ntp_force_sync = false; // Force NTP sync bool ntp_force_sync = false; // Force NTP sync
bool ntp_synced_message = false; // NTP synced message flag 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) myio my_module; // Active copy of Module GPIOs (17 x 8 bits)
gpio_flag my_module_flag; // Active copy of Template GPIO flags gpio_flag my_module_flag; // Active copy of Template GPIO flags
StateBitfield global_state; // Global states (currently Wifi and Mqtt) (8 bits) StateBitfield global_state; // Global states (currently Wifi and Mqtt) (8 bits)
@ -841,13 +840,12 @@ void PerformEverySecond(void)
if (Settings.tele_period) { if (Settings.tele_period) {
tele_period++; tele_period++;
// increase time for prepare and document state to ensure TELEPERIOD deliver results // increase time for prepare and document state to ensure TELEPERIOD deliver results
if (tele_period == Settings.tele_period -3 && !prep_called) { if (tele_period == Settings.tele_period -3) {
// sensores must be called later if driver switch on e.g. power on deepsleep // sensors must be called later if driver switch on e.g. power on deepsleep
XdrvCall(FUNC_PREP_BEFORE_TELEPERIOD); XdrvCall(FUNC_PREP_BEFORE_TELEPERIOD);
XsnsCall(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; tele_period = 0;
MqttPublishTeleState(); MqttPublishTeleState();
@ -859,7 +857,6 @@ void PerformEverySecond(void)
RulesTeleperiod(); // Allow rule based HA messages RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES #endif // USE_RULES
} }
prep_called = true;
XdrvCall(FUNC_AFTER_TELEPERIOD); XdrvCall(FUNC_AFTER_TELEPERIOD);
} }
} }

View File

@ -20,6 +20,12 @@
#ifdef USE_DEEPSLEEP #ifdef USE_DEEPSLEEP
/*********************************************************************************************\ /*********************************************************************************************\
* DeepSleep Support * 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 #define XDRV_29 29
@ -36,16 +42,24 @@ const char kDeepsleepCommands[] PROGMEM = D_PRFX_DEEPSLEEP "|"
void (* const DeepsleepCommand[])(void) PROGMEM = { void (* const DeepsleepCommand[])(void) PROGMEM = {
&CmndDeepsleepTime }; &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) void DeepSleepInit(void)
{ {
if (pin[GPIO_DEEPSLEEP] < 99) { // Go back to sleep after 60 minutes if requested deepsleep has not been reached
if (!digitalRead(pin[GPIO_DEEPSLEEP])) { if (DeepSleepEnabled() && (RtcSettings.ultradeepsleep > MAX_DEEPSLEEP_CYCLE) && (RtcSettings.ultradeepsleep < 1700000000)) {
RtcSettings.ultradeepsleep = 0;
}
}
if ((RtcSettings.ultradeepsleep > MAX_DEEPSLEEP_CYCLE) && (RtcSettings.ultradeepsleep < 1700000000)) {
RtcSettings.ultradeepsleep = RtcSettings.ultradeepsleep - MAX_DEEPSLEEP_CYCLE; RtcSettings.ultradeepsleep = RtcSettings.ultradeepsleep - MAX_DEEPSLEEP_CYCLE;
RtcReboot.fast_reboot_count = 0; RtcReboot.fast_reboot_count = 0;
RtcRebootSave(); RtcRebootSave();
@ -53,18 +67,16 @@ void DeepSleepInit(void)
RtcSettingsSave(); RtcSettingsSave();
ESP.deepSleep(100 * RtcSettings.deepsleep_slip * (MAX_DEEPSLEEP_CYCLE < RtcSettings.ultradeepsleep ? MAX_DEEPSLEEP_CYCLE : RtcSettings.ultradeepsleep), WAKE_RF_DEFAULT); ESP.deepSleep(100 * RtcSettings.deepsleep_slip * (MAX_DEEPSLEEP_CYCLE < RtcSettings.ultradeepsleep ? MAX_DEEPSLEEP_CYCLE : RtcSettings.ultradeepsleep), WAKE_RF_DEFAULT);
yield(); yield();
// Sleeping
} }
// Stay awake
RtcSettings.ultradeepsleep = 0; 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 // 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(); SettingsSaveAll();
// deepsleep_slip is ideally 10.000 == 100% // 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 // 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(); RtcSettingsSave();
ESP.deepSleep(100 * RtcSettings.deepsleep_slip * sleeptime); ESP.deepSleep(100 * RtcSettings.deepsleep_slip * sleeptime);
yield(); 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 if ((XdrvMailbox.payload == 0) || ((XdrvMailbox.payload > 10) && (XdrvMailbox.payload < (24 * 60 * 60)))) { // Allow max 24 hours sleep
Settings.deepsleep = XdrvMailbox.payload; Settings.deepsleep = XdrvMailbox.payload;
RtcSettings.nextwakeup = 0; 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); Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.deepsleep);
} }
@ -143,7 +156,7 @@ bool Xdrv29(uint8_t function)
switch (function) { switch (function) {
case FUNC_AFTER_TELEPERIOD: case FUNC_AFTER_TELEPERIOD:
CheckForDeepsleep(); DeepSleepCheck();
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = DecodeCommand(kDeepsleepCommands, DeepsleepCommand); result = DecodeCommand(kDeepsleepCommands, DeepsleepCommand);