so I have an application in which there are 2 specific times (eg 9AM, 6PM) where a variable curPos is updated. Everything is converted in ms since 00:00 (9AM = 32400000, 6PM = 64800000). Now, the actions (curPos updates) should be triggered every day at these times. To retrieve the current real time (always in ms) I'm sending a http request to a website. This is done only once in the setup().
The idea is that I have to access somehow (http request) the current real time in order to determine when are 9AM and 6PM in my ESP8266, since the start of the sketch. The current real time is needed only once at the beginning (in case there's an issue and the sketch restarts). Then I can calculate the difference between the current real time and the timers.
The next step would be to translate the values into a 24hour cycle starting at the startup/reset time, so that it can repeat itself without need to request again the real time.
I'm lost in the logic on how to deal with this and with millis() to check when it's time to trigger one of the actions.
This is what I'm doing now with the HTPP GET payload in the setup() (I can successfully retrieve the 3 values in ms from the GET response)
String payload = http.getString();
int commaIndex = payload.indexOf(',');
int secondCommaIndex = payload.indexOf(',', commaIndex + 1); //Search for the next comma just after the first
mainTime = payload.substring(0, commaIndex).toInt(); //current real time in ms
timers[0] = payload.substring(commaIndex + 1, secondCommaIndex).toInt(); //9AM in ms
timers[1] = payload.substring(secondCommaIndex + 1).toInt()); //6PM in ms
timers[0] = abs(mainTime - timers[0]); //the time difference before reaching the first timer (in ms)
timers[1] = abs(mainTime - timers[1]); //the time difference before reaching the second timer (in ms)
// my attempt to turn the time differences into a 24hrs "local" (in the sketch) time
//dayMs is 24hrs in ms (86400000)
if(timers[0] < 0)
timers[0] = dayMs - timers[0];
if(timers[1] < 0)
timers[1] = dayMs - timers[1];
and in the loop I'd need a comparison (I think) but I'm just really lost on this
//I think millis need to be reset after 24hrs since the start of the sketch?
if(millis() == timers[0]){
curPos = 1; //first timer expired then trigger
}else if(millis() == timers[1]){
curPos = 7; //second timer expired then trigger
}
thanks!
tick
variable ... increment it inside the loop() every second or every minute (even every hour ... whatever works for you) .... then simply monitor the value oftick
– jsotola Aug 10 '18 at 16:39