I have working code with the photon sleeping and waking based on a second count; however I want to put the photon to sleep and wake it up at a certain time, not just based on a timer. For example: checking a variable twice a day at say 6am and 6pm. How can I do this?
Calculate the time until the next wakeup, and have it sleep for that long
I believe i have it set up like that now. System.sleep(SLEEP_MODE_DEEP,1800) where 1800 is in seconds. I’m just afraid that with the actions taking place in the code, some actions will happen if outside variables are met and sometimes not when those variables are not met and the time will slowly drift or i will have to power everything up for the first time at the time that I want it to wake up every day. Or am I wrong in thinking this and the timer will be accurate enough?
If you use constant 1800 you’ll definetly introduce some “drift”, but that’s not what @Moors7 meant when he said[quote=“Moors7, post:2, topic:33171”]
Calculate the time until the next wakeup
Time.local() will give you the current time and date. So when you take that immediately before your sleep call and then calculate the number of seconds till your desired wake time, then the error introduced by the unpredictable time needed to wake, reconnect, do the jobs and go back to sleep won’t accumulate.
BTW, with each cloud reconnect the RTC will be synced too, so no big risk comming from that direction either.
So before my sleep call I can just input Time.local() and use the same System.sleep that I have? And do I need to set the timezone with Time.zone(-4)…eastern time?
You need to calculate the amount of seconds you need to sleep, before you put it to sleep.
Let’s say it’s 13:00 and you want it to sleep until 18:00, for how many seconds does it need to sleep?
18,000 seconds. Can you show me a code example of this please?
Sorry, but that should be basic math.
int secNow = Time.local() % 86400;
// for 18:23:25
int secThen = 18*3600 + 23*60 + 25;
int secDifference = secThen - secNow;
// a clumsy way to deal with calculating past midnight
while (secDifference < 0) secDifference += 86400;
// there would be a better math approach, but might reduce clarity for beginners
The only thing that might be new in that is
% 86400 which is the modulus operator which just returns the remaining rest of a division by 86400 (number of seconds in 24 hours). This strips the date info off the date time info given by
And yes, in order to have
Time.local() return the correct time for your location you need to call
Time.zone() at least once before requesting the time.
thanks. Again, I’m sorry for all the questions about code. As a mechanical engineering major I have experience with matlab code, but with an extremely vague instrumentation class featuring c++ code, its kind of tough for me. I can make things work and struggle with the code and electrical pixies so that’s why I’m trying these projects on my own to extend my knowledge.
I too am trying something similar. Just a handshake once per day… maybe publish something, trigger a counter, and then on the 7th time (one week) it does something more. I am having difficulty understanding the example you showed. yes… a novice here as well.
Can you explain how what exactly isn’t clear and how much (which parts) you did understand?
In your example, I don’t see the deep sleep command and how it would be used in this scenario .
??? would a formula also be here in insert the time needed to sleep the correct time in his case?
My case would be I want to sleep for 24hours.
I guess what I am trying to see is and example of code that would wake a photon up once per day (at a certain time) and go back to sleep. Using counters in the program, (my case) it would do different things on different days. Say, just a handshake on some days, but actual running a pump for a min or so on other days, but still wakes up on the same time each day. Would I have to make formulas for each code that is going to run that day?
You just need to view this from the most basic mathematical side of things.
When you retrieve
Time.local() you get a big number that represents the count of seconds since 01/01/1970 00:00:00.
One day has 86400 seconds. So when you divide that big number by 86400 you will get the number of days since then as the integer value and the remainder will give you the amount of seconds the current day is old.
So if you want to represent any arbitrary time (e.g. 11:25:30 am) you can calculate that time as
int secondOfDay = 11*60*60 + 25*60 + 30;
And if you want to sleep from now till then, you’d write
int currentSecond = Time.local() % 86400;
int tomorrow = 86400 + secondOfDay;
int secondsToSleep = tomorrow - currentSecond;
This will take care of waking every day at the same time.
What you want to do when you are awake is another business.
You could do something like this (or anything else )
// do things on Sunday
// do things on Monday
// do nothing the rest of the days
Wow. That helps out tremendously. Coming from just doing simple arduino stuff, this is an extremely powerful little chip. I didn’t know you could replace the last part of the sleep timer with a variable. Great part about the forum is you learn something each time you get on it. Sadly, it make the rabbit hole deeper each time. hahaha
Might take me a few days on figuring out how to do the Sunday, Monday thing. lol