Enter deep sleep right after sending all data

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Flagada
New user
Posts: 6
Joined: 05 Jan 2022, 12:28

Enter deep sleep right after sending all data

#1 Post by Flagada » 05 Jan 2022, 12:53

Hi there ! First of all thank you very much for the development of such a great and useful firmware ! 8-)

I have a remark/question about the deep sleep timer (I'm using the last ESP Mega firmware mega-20211224) and more specially about the sleep awake time.
If I understand well, this timer is first used for the wifi timeout connection and after connection it is used again to go into deep sleep whether all the data have been sent or not.

Could it be possible to keep a timer value for the wifi timeout and to add an option to enter deep sleep right after all data that have to be sent have effectively been sent?
I found a bit tricky that the user have to found the best suitable time in order to be sure that all data have been sent while not keeping the system awake for a too long and useless time.

What do you think about that?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Enter deep sleep right after sending all data

#2 Post by TD-er » 05 Jan 2022, 13:11

You already can, but you may need to use the rules for it.
You can act on some events (e.g. from the sensors) and then call deepsleep, or set a timer which will then trigger a new event on which you can act to call deepsleep.

When executing the deepsleep command, all controllers are also instructed to flush all data in their queue.
N.B. this does not wait forever, so in theory you may loose not yet sent data if the receiving end of that controller does not accept data quickly enough.

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Enter deep sleep right after sending all data

#3 Post by chromo23 » 05 Jan 2022, 14:03

I have the same issues with my battery powered devices and also opened a thread related to this one a while ago.
viewtopic.php?f=6&t=8497

I never made an official feature request on github...

Flagada
New user
Posts: 6
Joined: 05 Jan 2022, 12:28

Re: Enter deep sleep right after sending all data

#4 Post by Flagada » 05 Jan 2022, 18:25

Thank you for you fast responses!

I think it would be a great feature to add!

I will try what you suggest, can you tell me if I am wrong (I've never used rules before ^^)?

For information the aim of my system is to inform me of a door state and of the battery voltage, the esp is always in deep sleep and is waken only when the switch state changes (using a side attiny).

So to implement what you suggest correctly :
- 1st of all I have to delete the domoticz controller has the sending will be done by the rules.
- Then I have to check the "SendToHTTP wait for ack" to be sure that the sendings will be done before sleeping.
- If I have 2 readings to send (pin value and vcc value) I have to set several variables and when all of these variables will be set to 1 then we can enter deep sleep.

Here is what I have in mind inspired by your other topic :

Code: Select all

On System#Boot do
 TaskRun,1 // launch reading of the first "sensor"
 TaskRun,2 // launch reading of the 2nd "sensor"
 TimerSet_ms,1,10 // creating timer
 TimerSet,2,30 // Set Timer 2 for the next event in 30 seconds to force shut down in case of problem
Endon

On System#Wake do // is it mandatory to have this if we have System#Boot ?
 TaskRun,1
 TaskRun,2
Endon

On WiFi#Connected do
 Let,1,1
Endon

On EtatPorteGarage#State do
 Let,2,1
Endon

On Vcc#vcc do
 Let,3,1
Endon

On Rules#Timer=1 do
 If [var#1]=1 and [var#2]=1 and [var#3]=1 // we are connected and all values are available
  SendToHTTP whatever you want // send [EtatPorteGarage#State] to domoticz
  SendToHTTP whatever you want // send [Vcc#vcc] to domoticz
  deepsleep // bye bye
 Endif
 TimerSet_ms,1,10 // still waiting
Endon

On Rules#Timer=2 do deepsleep endon // force sleep in case of problem

This seems a bit overkill!

I have several questions :
- Is it mandatory to call TaskRun in order to launch the sensor reading ?
- Isn't it redondant to have System#Wake and System#Boot ?
- Can we still use the controller for sending via the rules ?
- If a controller is configured and set-up in the tasks, will the TaskRun send the value automatically?

Sorry for all those rule newbie questions! :-)

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Enter deep sleep right after sending all data

#5 Post by chromo23 » 05 Jan 2022, 19:04

Is it mandatory to call TaskRun in order to launch the sensor reading ?
For you application probably not i tried to get the readings from the sensor as fast as possible...i actually don’t know if it helps and wifi takes anyway much longer to connect.
Isn't it redondant to have System#Wake and System#Boot ?
I think so.. i was to lazy to read about it. i initially thought that System#Boot would not work because the esp only wakes up from sleep
- Can we still use the controller for sending via the rules ?
- If a controller is configured and set-up in the tasks, will the TaskRun send the value automatically?
Since i do not use controller i can not really help you...

But you should look here also:
https://www.letscontrolit.com/wiki/index.php/SleepMode
Especially the part with the switch.

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Enter deep sleep right after sending all data

#6 Post by chromo23 » 05 Jan 2022, 19:10

calling deepsleep directly after sendtohttp is also problematic since there is no acknowledgement for sending something an therefore the esp will probably go to sleep before a successful sending....

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Enter deep sleep right after sending all data

#7 Post by TD-er » 05 Jan 2022, 19:17

You should not need to disable the Domoticz controller.
If a task has a value available (thus when PLUGIN_READ is called and the sensor has a new value) then the data is sent to the controller(s) and (!!!) it sends the new values as an event to be processed in the rules.
So you can still use a controller and don't need to do it all yourself in the rules.
Or else it should not be called "ESPEasy" ;)

This means that you can also consider the value events as a trigger that there have been new values.

What you can do in your rules, is call a taskrun (which effectively calls the plugin call PLUGIN_READ) and keep track of whether you are connected to WiFi (there is an event for that)

So in pseudo rules code:

Code: Select all

At boot, start some timer to call deepsleep, just in case the connection to WiFi fails

At WiFi#connected event, set a variable to know there is a connection possible to flush data sent to the controllers
Maybe also call for a number of taskrun calls to make sure the tasks will yield a value as soon as possible

At all the taskname#taskvaluename events you care for, (or just the one that takes the longest) check if all events you need are checked and there is WiFi connection, then go to sleep


Flagada
New user
Posts: 6
Joined: 05 Jan 2022, 12:28

Re: Enter deep sleep right after sending all data

#8 Post by Flagada » 05 Jan 2022, 21:17

ok all you said make sense :)
here what it could look :

Code: Select all

on System#Boot do
  TimerSet,1,30 // Set Timer to force shut down in case of problem
Endon
On Rules#Timer=1 do
  deepsleep // force sleep in case of problem
endon

on WiFi#connected do
  TaskRun,1
  TaskRun,2
  Let,1,1
endon
on vcc#vcc do
  Let,2,1
  If [var#1]=1 and [var#3]=1 // we are connected and all values have been read
    deepsleep // bye bye
  Endif
endon
on test#State do
  Let,3,1
  If [var#1]=1 and [var#2]=1 // we are connected and all values have been read
    deepsleep // bye bye
  Endif
endon

The only thing that annoy me before testing is : how will I be able to connect again? I suppose the normal trick of 30s in case of power down/power up will not work in this case, I have to include a secure case by using a specific swith or jumper.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Enter deep sleep right after sending all data

#9 Post by TD-er » 05 Jan 2022, 21:33

you can give the nr of seconds to sleep See: https://espeasy.readthedocs.io/en/lates ... l-commands
and you can check the state of a pin in the rules, preventing sleep

N.B. you can also disable timer1 to prevent entering deep sleep

Flagada
New user
Posts: 6
Joined: 05 Jan 2022, 12:28

Re: Enter deep sleep right after sending all data

#10 Post by Flagada » 20 Jan 2022, 17:05

Following your proposal here is the final code I use (it may help someone someday) :)

On startup I start a 30s timer to force deepsleep in case of problem.
I added a SleepSwitch that block sleep if pressed.
I have 2 tasks DoorLock and vcc which are launched after wifi connection.
When the 2 tasks have ended I restart the sleep timer for 0.1s.

Code: Select all

on System#Boot do
  TimerSet,1,30
Endon
On Rules#Timer=1 do
  If [SleepSwitch#State]=0
    deepsleep
  EndIf
endon

on WiFi#connected do
  Let,1,1
  TaskRun,1
  TaskRun,2
endon
on DoorLock#State do
  Let,2,1
  If [var#1]=1 and [var#3]=1
    timerSet_ms,1,100
  Endif
endon
on vcc#vcc do
  Let,3,1
  If [var#1]=1 and [var#2]=1
    timerSet_ms,1,100
  Endif
endon
Thanks to this the awaken time is about only 5-6s :)

For information I updated to the last firmware recently (mega-20211224) and since I have some random connection problems at startup that where not present before (unfortunately I am not sure of the previous version I used, maybe the mega-20210503). I checked the "Send With Max TX Power" and "Force Wifi B/G" and it seems to improve the situation... The RSSI is about -73 dBm which is not so bad.

Thanks!

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Enter deep sleep right after sending all data

#11 Post by chromo23 » 20 Jan 2022, 17:52

5-6 seconds are actually quite a lot.
I have a battery powered wemos. Collecting only data from a bme280 and analog value of the battery. it takes 3 seconds to send data successfully to thingspeak.
I was once down to 2 seconds with an older build (but i cannot rember wich one. :cry: ). it also depends on the wifi router. (tested different models and had fluctuations from 2-4 seconds. inthink my broadcom routers where always a bit faster. but i doubt the method of investigation)
I haven’t read the whole thread but i am sure somebody already suggested an static ip.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Enter deep sleep right after sending all data

#12 Post by TD-er » 20 Jan 2022, 19:15

Static IP and fixed channel for the access point.
Also on tools->Advanced enable: "Use Last Connected AP from RTC"

Flagada
New user
Posts: 6
Joined: 05 Jan 2022, 12:28

Re: Enter deep sleep right after sending all data

#13 Post by Flagada » 04 Feb 2022, 16:28

Hi and thank you for your reply,

I well have a static IP and channel. In fact the 5-6s are not really a problem for me as my system is used only a few time each days.

What annoys me more since my last firmware update is the fact that sometime the information is not sent before going to sleep, the different options I used like "Use Last Connected AP from RTC", "Send With Max TX Power" and "Force Wifi B/G" seems to have improved the situation but it's not always good (I would say it crashes maybe 1 time on 10). With the previous firmware I used I never had this problem.

It looks like the wifi connection failed and the system goes then into deepsleep.
I don't know what could be the best solution.
For example is there a way in rules to force a wifi restarting if after 10s if it is not connected? Or do a full restart a few times if not connected before to enter a full long deep sleep (this would require the saving of a variable between 2 restarts)?
I am thinking of using a ESP12 card with an external antenna to improve range but as I said the RSSI is not so bad and it was working before :?

Thanks for your advices :)

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests