Controlling DeepSleep over MQTT

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Controlling DeepSleep over MQTT

#1 Post by SvnKswttr » 25 Feb 2019, 13:31

Hi all!
New to the forum but not new to IT related stuff and Arduino's.
I try to control the DeepSleep for ESP8266's (Wemos D1 mini) running ESPEasy on Version mega-20190225 over MQTT.

What I want to do:
deploy some battery powered ESPEasy-devices remotely but reachable over WiFi.
In case of some config-changes and/or firmware updates I do not want to walk to these, connect D0 to GND, do the stuff and re-connect D0 to RST.

Idea:
Why not use the currently working MQTT broker (mosquitto) to control the DeepSleep?

What I did so far:
I created a "Generic - MQTT Import"-Device subscribing to the below:
Device.PNG
Device.PNG (39.93 KiB) Viewed 3660 times
I have configured MQTT to publish stuff like temp, humidity, etc. MQTT is working fine.

I published 2 retained messages that "define" the values evaltuated in the rule:
DeepSleep will be enabled and last for 10 seconds

Code: Select all

mosquitto_pub -r "49/DeepSleep/Enabled" -m "1"
mosquitto_pub -r "49/DeepSleep/Duration" -m "10"
To check if the messages are really reatined by MQTT I subscribed via the terminal to the feed:

Code: Select all

mosquitto_sub -v -t "49/#"
49/DeepSleep/Enabled 1
49/DeepSleep/Duration 10
My Rule is (currently) as follows:

Code: Select all

on MQTT#Connected do
  Publish %sysname%/DeepSleep/Status,"Awake. Enabled? [MQTT-IN#DeepSleepEnabled]"
  Publish,%sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
  if MQTT-IN#DeepSleepEnabled="1.00"
    Publish,%sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
    Publish,%sysname%/GPIO/12,1 // LED just to have a visible indicator. Later the Data-Stuff will be placed here.
    publish,%sysname%/SensorData/PublishTime,%systime% //publish when data-stuff was read.
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    //deepsleep,[MQTT-IN#DeepSleepDuration] //go to deepsleep for X seconds (even if deepsleep value is not configured in the config menu)
  else
    Publish %sysname%/DeepSleep/Status,"staying awake"
    Publish,%sysname%/GPIO/12,0 //turning off the LED
endon
When I power up the tiny device the following is put out on the MQTT server:

49/status/LWT //Connected Connect the MQTT works fine.
49/DeepSleep/Status "Awake. Enabled? 0.00" //Here comes the bad stuff: Set to 1 but output is 0
49/DeepSleep/Duration 0.00 //Also the duration got lost.
49/DeepSleep/Duration 0.00
49/GPIO/12 1 //Another strange thing: Enabled was read in line #2 as 0.00 but here it went into the DeepSleep part.
49/SensorData/PublishTime 13:10:37
49/DeepSleep/Status "Sleeping for 0.00 seconds"

I assume that the rule runs "too fast" to get the MQTT stuff read.

I also tried the below code but that did not put out anything on the screen but went to sleep all the time even I set the "enabled" to 0

Code: Select all

On MQTT#Connected Do //when the broker is connected
  timerSet,1,5
endon

On Rules#Timer=1 Do
  Publish %sysname%/DeepSleep/Status,"Awake. Enabled? [MQTT-IN#DeepSleepEnabled]"
  Publish,%sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
  if MQTT-IN#DeepSleepEnabled="1.00"
    Publish,%sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
    Publish,%sysname%/GPIO/12,1 // LED just to have a visible indicator
    publish,%sysname%/SensorData/PublishTime,%systime% //publish when stuff was read.
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    deepsleep,[MQTT-IN#DeepSleepDuration] //go to deepsleep for X seconds (even if deepsleep value is not configured in the config menu)
  else
    Publish %sysname%/DeepSleep/Status,"staying awake"
    Publish,%sysname%/GPIO/12,0 //turning off the LED
EndOn
49/status/LWT Connection Lost
49/status/LWT Connected
49/status/LWT Connection Lost
49/status/LWT Connected
49/status/LWT Connection Lost
49/status/LWT Connected
49/DeepSleep/Enabled 0 <= Here I set the value
49/status/LWT Connection Lost
49/status/LWT Connected
49/status/LWT Connection Lost
49/status/LWT Connected

COuld anyone point me into the right direction, please?

Many thanks
Sven

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#2 Post by SvnKswttr » 27 Feb 2019, 06:28

After digging around in the rules I am now completely off :/

running todays version: mega-20190227

ok, re-start:
Set an MQTT-Import-Device to read 3 retained messages:
  • "51/DeepSleep/SleepDuration"
  • "51/DeepSleep/AwakeDuration"
  • "51/DeepSleep/Enabled"
A "working" rule contains the following:

Code: Select all

on Time#Initialized do // when time was set by NTP
  publish,%sysname%/SensorData/H,[SensorData#H] //publish a reading
  publish,%sysname%/SensorData/T,[SensorData#T] //publish a reading
  publish,%sysname%/SensorData/P,[SensorData#P] //publish a reading
  publish,%sysname%/SensorData/PublishTime,%systime% //publish reading time
  Publish %sysname%/DeepSleep/Status,"Awake. DeepSleep enabled? [MQTT-IN#DeepSleepEnabled]" //just print the value
endon

on MQTT-IN#DeepSleepEnabled=1 do
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    //deepsleep,[MQTT-IN#DeepSleepDuration] //go to deepsleep for 600 seconds (even if deepsleep value is not configured in the config menu)
endon

on MQTT-IN#DeepSleepEnabled=0 do
  Publish %sysname%/DeepSleep/Status,"staying awake"
endon
The three retained MQTT values contain the followng:
51/DeepSleep/Enabled 1
51/DeepSleep/SleepDuration 45
51/DeepSleep/AwakeDuration 90
DS01.PNG
DS01.PNG (17.6 KiB) Viewed 3628 times
When rebooting the device the MQ shows the below output:
51/DeepSleep/status/LWT Connected => Fine as this is standard
51/SensorData/H 27.77 => expected humidity reading
51/SensorData/T 26.88 => expected temperature reading
51/SensorData/P 1026.53 => expected pressure reading
51/SensorData/PublishTime 06:18:17 => expected time of reading reading
51/DeepSleep/Status "Awake. DeepSleep enabled? 0" => UNEXPECTED status reading as it was set to "1"
51/DeepSleep/Status "Sleeping for 45 seconds" => Also UNEXPECTED as the previous reading for the same variable was "0"

So, mainly the logical expression for evaluating the MQ-Device (MQTT-IN#DeepSleepEnabled=1) is working correctly.

Next I tried to use the IF-ELSE funtion within the ON-DO-ENDON loop:

Code: Select all

on Time#Initialized do // when time was set by NTP
  publish,%sysname%/SensorData/H,[SensorData#H] //publish a reading
  publish,%sysname%/SensorData/T,[SensorData#T] //publish a reading
  publish,%sysname%/SensorData/P,[SensorData#P] //publish a reading
  publish,%sysname%/SensorData/PublishTime,%systime% //publish reading time
  Publish %sysname%/DeepSleep/Status,"Awake. DeepSleep enabled? [MQTT-IN#DeepSleepEnabled]"
  if [MQTT-IN#DeepSleepEnabled]=1
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    //deepsleep,[MQTT-IN#DeepSleepDuration] //go to deepsleep for [MQTT-IN#DeepSleepDuration] seconds
  else
    Publish %sysname%/DeepSleep/Status,"staying awake"
  endif
endon
The MQ-console proves me wrong:
51/DeepSleep/status/LWT Connection Lost
51/DeepSleep/status/LWT Connected
51/SensorData/H 28.22
51/SensorData/T 26.96
51/SensorData/P 1026.54
51/SensorData/PublishTime 06:26:55
51/DeepSleep/Status "Awake. DeepSleep enabled? 0"
51/DeepSleep/Status "staying awake"

any idea?

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#3 Post by grovkillen » 27 Feb 2019, 08:02

What topic are you subscribing to in the MQTT import? Please remember that that topic is the complete topic path which only that plugin is subscribing to. So if you want it to listen to the whole path (which you're trying to) you need to add that full path to the settings of that plugin. Or simple publish to the exact topic you already have there.

And you should use the MQTTimport#Connected event since the MQTT import manages it's own connection.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#4 Post by SvnKswttr » 27 Feb 2019, 09:13

Thanks for your answer,

The reason for using "Time#Initialized" is simply the fact that "MQTTimport#Connected" is triggered before the NTP-Client got the current time and I like to see the latest Data-Upload time instead og 00:00:03 :)

My subscription is as shown below:
sub.PNG
sub.PNG (52.64 KiB) Viewed 3615 times
My Workaround as of now is:

Code: Select all

on Time#Initialized do // when time was set by NTP
  publish,%sysname%/SensorData/H,[SensorData#H] //publish a reading
  publish,%sysname%/SensorData/T,[SensorData#T] //publish a reading
  publish,%sysname%/SensorData/P,[SensorData#P] //publish a reading
  publish,%sysname%/SensorData/PublishTime,%systime% //publish reading time
  publish,%sysname%/DeepSleep/Status,"Staying awake for [MQTT-IN#AwakeDuration] seconds"
  timerSet,1,60 // wait a minute just in case...
  //timerSet,1,[MQTT-IN#AwakeDuration]
endon

on Rules#Timer=1 do
  Publish,%sysname%,DeepSleep/Status,"Need to sleep for [MQTT-IN#DeepSleepDuration] sec"
  deepsleep [MQTT-IN#DeepSleepDuration]
endon

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#5 Post by grovkillen » 27 Feb 2019, 10:47

Yes but I would think that upon boot the values of that dummy is all zeroes. And since you're using the event which takes place before the MQTT import is connected you'll end up in the state you're experiencing.

Please try to use another event which will trigger upon receiving of the MQTT message for the deepsleep.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#6 Post by SvnKswttr » 27 Feb 2019, 10:54

Just had some time to have a deeper look into the serial output with debug-level:
I also switched from Time#Initialized to MQTTimport#Connected as suggested.

As you can see in the debug-log the MQTTimport#Connected is triggered (4633) before the import was done.
That's the reason why my loops do not work.

Code: Select all

INIT : Booting version: mega-20190227 (ESP82xx Core 2_4_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3 PUYA support)
78 : INIT : Warm boot #19 - Restart Reason: External System
80 : FS   : Mounting...
105 : FS   : Mount successful, used 76555 bytes of 957314
466 : CRC  : program checksum       ...OK
501 : CRC  : SecuritySettings CRC   ...OK
586 : INIT : Free RAM:25568
588 : INIT : I2C
588 : INIT : SPI not enabled
625 : INFO : Plugins: 46 [Normal] (ESP82xx Core 2_4_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3 PUYA support)
626 : EVENT: System#Wake
658 : WIFI : Set WiFi to STA
691 : WIFI : Connecting WIFISID attempt #0
692 : IP   : Static IP : 192.168.252.40 GW: 192.168.252.111 SN: 255.255.255.0 DNS: 192.168.252.111
694 : WIFI : Not configured in Station Mode!!: WIFISID
720 : EVENT: System#Boot
780 : DHT  : Temperature: 24.60
780 : DHT  : Humidity: 46.20
783 : EVENT: SensorData#T=24.60
811 : EVENT: SensorData#H=46.20
1648 : IMPT : MQTT 037 Intentional reconnect
2081 : WD   : Uptime 0 ConnectFailures 0 FreeMem 22480 WiFiStatus 0
2646 : IMPT : MQTT 037 Intentional reconnect
3013 : LoopStats: shortestLoop: 2995 longestLoop: 481276 avgLoopDuration: 6130.27 loopCounterMax: 10016 loopCounterLast: 368 countFindPluginId: 1
3014 : Scheduler stats: (called/tasks/max_length/idle%) 367/82/7/31.00
3650 : IMPT : MQTT 037 Intentional reconnect
4109 : WIFI : Connected! AP: WIFISID (xx:xx:xx:xx:xx:xx) Ch: 11 Duration: 2902 ms
4110 : EVENT: WiFi#ChangedAccesspoint
4141 : EVENT: WiFi#ChangedAccesspoint Processing time:31 milliSeconds
4143 : IP   : Static IP : 192.168.252.40 GW: 192.168.252.111 SN: 255.255.255.0 DNS: 192.168.252.111
4145 : WIFI : Static IP: 192.168.252.40 (Buero-40) GW: 192.168.252.111 SN: 255.255.255.0   duration: 549 ms
4159 : EVENT: WiFi#Connected
4187 : EVENT: WiFi#Connected Processing time:28 milliSeconds
4193 : Webserver: start
4259 : MQTT : Intentional reconnect
4311 : MQTT : Connected to broker with client ID: ESPClient_3C:71:BF:28:E0:1A
4315 : Subscribed to: Buero/#
4318 : EVENT: MQTT#Connected
4342 : EVENT: MQTT#Connected Processing time:24 milliSeconds
4547 : NTP  : NTP replied: delay 20 mSec Accuracy increased by 0.998 seconds
4549 : Time adjusted by -1551258400474.23 msec. Wander: -430905111.24 msec/second
4551 : Current Time Zone:  DST time start: 2019-03-31 02:00:00 offset: 120 minSTD time start: 2019-10-27 03:00:00 offset: 60 min
4554 : EVENT: Time#Initialized
4571 : EVENT: Time#Initialized Processing time:17 milliSeconds
4574 : EVENT: Clock#Time=Wed,10:06
4589 : EVENT: Clock#Time=Wed,10:06 Processing time:15 milliSeconds
4631 : IMPT : Connected to MQTT broker with Client ID=Buero-Import
[b]4633 : EVENT: MQTTimport#Connected[/b]
4649 : ACT  : publish,Buero/SensorData/H,46.2
4660 : Command: publish
4661 : publish,Buero/SensorData/H,46.2
4672 : ACT  : publish,Buero/SensorData/T,24.6
4682 : Command: publish
4683 : publish,Buero/SensorData/T,24.6
4695 : ACT  : publish,Buero/SensorData/P,
4704 : Command: publish
4705 : publish,Buero/SensorData/P,
4712 : ACT  : publish,Buero/SensorData/PublishTime,10:06:44
4723 : Command: publish
4723 : publish,Buero/SensorData/PublishTime,10:06:44
4744 : ACT  : publish,Buero/DeepSleep/Status,"Staying awake for 0 seconds"
4752 : Command: publish
4753 : publish,Buero/DeepSleep/Status,"Staying awake for 0 seconds"
4758 : ACT  : timerSet,1,60
4766 : Command: timerset
4767 : timerSet,1,60
4771 : ACT  :
4786 : EVENT: MQTTimport#Connected Processing time:153 milliSeconds
4802 : IMPT : [MQTT-IN#DeepSleepDuration] subscribed to Buero/DeepSleep/SleepDuration
4808 : IMPT : [MQTT-IN#DeepSleepEnabled] subscribed to Buero/DeepSleep/Enabled
4812 : IMPT : [MQTT-IN#AwakeDuration] subscribed to Buero/DeepSleep/AwakeDuration
4930 : IMPT : [MQTT-IN#DeepSleepDuration] : 10.00
4933 : EVENT: MQTT-IN#DeepSleepDuration=10.00
4949 : EVENT: MQTT-IN#DeepSleepDuration=10.00 Processing time:16 milliSeconds
5335 : IMPT : [MQTT-IN#AwakeDuration] : 30.00
5337 : EVENT: MQTT-IN#AwakeDuration=30.00
5353 : EVENT: MQTT-IN#AwakeDuration=30.00 Processing time:16 milliSeconds
20526 : EVENT: Clock#Time=Wed,10:07
20543 : EVENT: Clock#Time=Wed,10:07 Processing time:17 milliSeconds
I will need to have two 1sec breaks and start the other tasks then.
This is the output on MQTT with a 10sec DeepSleep with the rule(s) at the botom.
This means it will tak about 18 seconds between each data publishing. Minus 10 seconds of sleep: 8 sec of data processing.
Buero/status/LWT Connected
Buero/SensorData/H 46.0
Buero/SensorData/T 25.3
Buero/SensorData/P (null)
Buero/SensorData/PublishTime 10:43:22
Buero/status/LWT Connection Lost
Buero/status/LWT Connected
Buero/SensorData/H 46.1
Buero/SensorData/T 25.3
Buero/SensorData/P (null)
Buero/SensorData/PublishTime 10:43:40
Buero/DeepSleep/Enabled 0
Buero/status/LWT Connection Lost
Buero/status/LWT Connected
Buero/SensorData/H 45.9
Buero/SensorData/T 25.3
Buero/SensorData/P (null)
Buero/SensorData/PublishTime 10:43:59
Buero/DeepSleep/Status "Staying awake."
I needed the 2nd break (Timer 2) as the data reads have not been pushed to MQ without.
The rule(s) as of 20190227, 10:49 CET:

Code: Select all

on Time#Initialized do // when time was set by NTP
  timerSet,1,1 // wait a sec, please
endon

on Rules#Timer=1 do
  publish,%sysname%/SensorData/H,[SensorData#H] //publish a reading
  publish,%sysname%/SensorData/T,[SensorData#T] //publish a reading
  publish,%sysname%/SensorData/P,[SensorData#P] //publish a reading
  publish,%sysname%/SensorData/PublishTime,%systime% //publish reading time
  if [MQTT-IN#DeepSleepEnabled]=0
    publish,%sysname%/DeepSleep/Status,"Staying awake."
  else
    timerSet,2,1 // wait a sec, please
  endif
endon

on Rules#Timer=2 do
  Publish,%sysname%,DeepSleep/Status,"Need to sleep for [MQTT-IN#DeepSleepDuration] sec"
  deepsleep [MQTT-IN#DeepSleepDuration]
endon
Everything in Timer 1 (between 6679 and 6870) was not sent to MQTT without the 2nd break :(
4745 : EVENT: Time#Initialized Processing time:38 milliSeconds
4748 : EVENT: Clock#Time=Wed,10:37
4764 : EVENT: Clock#Time=Wed,10:37 Processing time:16 milliSeconds
4802 : IMPT : Connected to MQTT broker with Client ID=Buero-Import
4803 : EVENT: MQTTimport#Connected
4822 : EVENT: MQTTimport#Connected Processing time:19 milliSeconds
4837 : IMPT : [MQTT-IN#DeepSleepDuration] subscribed to Buero/DeepSleep/SleepDuration
4842 : IMPT : [MQTT-IN#DeepSleepEnabled] subscribed to Buero/DeepSleep/Enabled
4847 : IMPT : [MQTT-IN#AwakeDuration] subscribed to Buero/DeepSleep/AwakeDuration
4871 : IMPT : [MQTT-IN#DeepSleepDuration] : 10.00
4874 : EVENT: MQTT-IN#DeepSleepDuration=10.00
4891 : EVENT: MQTT-IN#DeepSleepDuration=10.00 Processing time:17 milliSeconds
5073 : IMPT : [MQTT-IN#DeepSleepEnabled] : 1.00
5075 : EVENT: MQTT-IN#DeepSleepEnabled=1.00
5092 : EVENT: MQTT-IN#DeepSleepEnabled=1.00 Processing time:17 milliSeconds
5275 : IMPT : [MQTT-IN#AwakeDuration] : 30.00
5277 : EVENT: MQTT-IN#AwakeDuration=30.00
5294 : EVENT: MQTT-IN#AwakeDuration=30.00 Processing time:17 milliSeconds
6679 : EVENT: Rules#Timer=1
6706 : ACT : publish,Buero/SensorData/H,46.1
6715 : Command: publish
6715 : publish,Buero/SensorData/H,46.1
6734 : ACT : publish,Buero/SensorData/T,25.2
6743 : Command: publish
6744 : publish,Buero/SensorData/T,25.2
6762 : ACT : publish,Buero/SensorData/P,
6771 : Command: publish
6772 : publish,Buero/SensorData/P,
6780 : ACT : publish,Buero/SensorData/PublishTime,10:37:29
6788 : Command: publish
6789 : publish,Buero/SensorData/PublishTime,10:37:29
6807 : Lev.1: [if 1=0]=false
6816 : Lev.1: [else]=true
6834 : ACT : Publish,Buero,DeepSleep/Status,"Need to sleep for 10 sec"
6843 : Command: publish
6844 : Publish,Buero,DeepSleep/Status,"Need to sleep for 10 sec"
6862 : ACT : deepsleep 10
6870 : Command: deepsleep
6870 : deepsleep 10
6872 : EVENT: System#Sleep
6897 : EVENT: System#Sleep Processing time:25 milliSeconds
Any idea for speeding up or tune the stuff?
Sven

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#7 Post by grovkillen » 27 Feb 2019, 10:58

What happens if you place those publish inside the if statement?
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#8 Post by SvnKswttr » 27 Feb 2019, 11:05

I have had this before: Nothing gets published on MQ :(

Regarding the other event that comes later: That will save another second:
MQTT-IN#AwakeDuration is the last one that is visible in the logs.
So rue as follows:

Code: Select all

on MQTT-IN#AwakeDuration do
  publish,%sysname%/SensorData/H,[SensorData#H] //publish a reading
  publish,%sysname%/SensorData/T,[SensorData#T] //publish a reading
  publish,%sysname%/SensorData/P,[SensorData#P] //publish a reading
  publish,%sysname%/SensorData/PublishTime,%systime% //publish reading time
  if [MQTT-IN#DeepSleepEnabled]=0
    publish,%sysname%/DeepSleep/Status,"Staying awake."
  else
    timerSet,2,1 // wait a sec, please
  endif
endon

on Rules#Timer=2 do
  Publish,%sysname%,DeepSleep/Status,"Need to sleep for [MQTT-IN#DeepSleepDuration] sec"
  deepsleep [MQTT-IN#DeepSleepDuration]
endon

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#9 Post by grovkillen » 27 Feb 2019, 11:58

Try to make the rule not use 0 to see if it should deepsleep or not since that is the boot value.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#10 Post by SvnKswttr » 27 Feb 2019, 12:53

that makes no sense as already the "main"-stuff in the "on"-part gets not published :(

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#11 Post by grovkillen » 27 Feb 2019, 13:09

So what you say is that even if the serial log shows it being published you don't see it in the MQTT stream of you broker?
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#12 Post by SvnKswttr » 27 Feb 2019, 13:41

Yes, that's right. Broker runs in the same network with 8 nodes connecting every 2 to 5 minutes. Another 4 nodes are connecting for testing only.
So the mosquitto is idling around running on a Cubiboard 2 with debian stretch as OS from a SATA connected SSD.

Even when I set the minimum interval to 1msec nothing comes through.
htop.PNG
htop.PNG (7.07 KiB) Viewed 3575 times
Also in Timer 2 the publish message comes not through on all nodes:

Code: Select all

on Rules#Timer=2 do
  Publish,%sysname%,DeepSleep/Status,"Need to sleep for [MQTT-IN#DeepSleepDuration] sec"
  deepsleep [MQTT-IN#DeepSleepDuration]

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#13 Post by grovkillen » 27 Feb 2019, 13:52

Something to try: shorten the names of the values and remove any special characters (minus for example).
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

SvnKswttr
Normal user
Posts: 14
Joined: 25 Feb 2019, 12:52
Location: Germany

Re: Controlling DeepSleep over MQTT

#14 Post by SvnKswttr » 27 Feb 2019, 13:57

Valuenames are single character only so shorten ist not possible,
The shortest topic is "Bad/SensorData/H" with an integer payload.
and even this is not shown.
One the 1sec delay is inserted all is fine

chemmex
Normal user
Posts: 17
Joined: 15 Feb 2019, 16:18

Re: Controlling DeepSleep over MQTT

#15 Post by chemmex » 27 Feb 2019, 14:11

Also in Timer 2 the publish message comes not through on all nodes:
Confirming: no publication before deepsleep command, trying same or similar ruleset

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#16 Post by grovkillen » 27 Feb 2019, 14:33

Your value names for the MQTT import is not single characters.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
grovkillen
Core team member
Posts: 3607
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Controlling DeepSleep over MQTT

#17 Post by grovkillen » 27 Feb 2019, 14:34

chemmex wrote:
27 Feb 2019, 14:11
Also in Timer 2 the publish message comes not through on all nodes:
Confirming: no publication before deepsleep command, trying same or similar ruleset
All right, so please add it to a GitHub issue (please search and see if someone already have it opened).
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

patrik
Normal user
Posts: 2
Joined: 30 Jun 2020, 02:10
Location: Sweden

Re: Controlling DeepSleep over MQTT

#18 Post by patrik » 30 Jun 2020, 02:13

Sorry to revive an old thread.
Where you able to get this working, SvnKswttr?

patrik
Normal user
Posts: 2
Joined: 30 Jun 2020, 02:10
Location: Sweden

Re: Controlling DeepSleep over MQTT

#19 Post by patrik » 05 Jul 2020, 16:13

So after a bit of messing around with SvnKswttrs examples i've gotten it working.
Haven't seen anyone else posting anything, anywhere about something like this so i'll post my example of it.

Using SvnKswttrs rules and devices example i've changed a few things since i am using a Domoticz switch selector to Sleep, Wake up, and giving notification when awake.
LolinWakeButton.png
LolinWakeButton.png (13.29 KiB) Viewed 142 times
Here is my example of SvnKswttrs Rule#1 talking to Domoticz deviceid 195

Code: Select all

on MQTT#Connected do
  Publish %sysname%/DeepSleep/Status,"Awake. Enabled? [MQTT-IN#DeepSleepEnabled]"
  Publish %sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
  if [MQTT-IN#DeepSleepEnabled]=1
    Publish %sysname%/DeepSleep/Duration,[MQTT-IN#DeepSleepDuration]
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    Publish domoticz/in, '{"command": "switchlight", "idx": 195, "switchcmd": "Set Level", "level": 0 }'
    deepsleep,[MQTT-IN#DeepSleepDuration] //go to deepsleep for X seconds (even if deepsleep value is not configured in the config menu)
  else
    Publish %sysname%/DeepSleep/Status,"Staying awake"
    Publish domoticz/in, '{"command": "switchlight", "idx": 195, "switchcmd": "Set Level", "level": 20 }'
  endif
endon
Also added a second rule (not sure if i can just put this in the first instead). I found it was needed for the Esp to go to sleep at startup (i think, writing this up makes me wonder why i made it):

Code: Select all

on MQTT-IN#DeepSleepEnabled=1 do
    Publish %sysname%/DeepSleep/Status,"Sleeping for [MQTT-IN#DeepSleepDuration] seconds"
    deepsleep,[MQTT-IN#DeepSleepDuration]
endon
Kept the task pretty much as SvnKswttr had it:
ESPEasyTask.png
ESPEasyTask.png (27.79 KiB) Viewed 142 times
On the Domoticz side i have made two scripts being executed on Sleep and Wake up button.
These are just simple bash scripts using mosquitto_pub.
DomoticzButton.png
DomoticzButton.png (43.73 KiB) Viewed 142 times
esp_awake:

Code: Select all

#!/bin/sh
/usr/bin/mosquitto_pub -r -t "$1/DeepSleep/Enabled" -m "0"
esp_sleep:

Code: Select all

#!/bin/sh
/usr/bin/mosquitto_pub -r -t "$1/DeepSleep/Duration" -m "$2"
/usr/bin/mosquitto_pub -r -t "$1/DeepSleep/Enabled" -m "1"
Using $1 for the name of the device and $2 for sleep duration give the ability to use the same script for several devices.

chemmex
Normal user
Posts: 17
Joined: 15 Feb 2019, 16:18

Re: Controlling DeepSleep over MQTT

#20 Post by chemmex » 08 Jul 2020, 19:19

Apart from MQTTImport, normal MQTT subscription works well, too. I am using OpenHAB controller, unit subscribes to %sysname%/# topic. Commands are sent to %sysname%/cmd topic, payload contains event command with arguments.

With Node-Red the control function looks like

Code: Select all

msg.topic = msg.deviceId + "/cmd" ;
msg.payload = "event,setctl=2,300";
return msg;
And with this in rules the unit goes to sleep for 300 seconds:

Code: Select all

on setctl do
  if %eventvalue1%=0
     publish %sysname%/Status,"Maintenance mode"
     gpio,12,1
  endif
  if %eventvalue1%=1
     publish %sysname%/Status,"Continuous mode"
     timerset,1,2
  endif
  if %eventvalue1%=2
     publish %sysname%/Status,"Going to sleep for %eventvalue2% seconds"
     deepsleep,%eventvalue2%
  endif
endon

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests