Setting RTC From Internet?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Setting RTC From Internet?

Post by weldeng » Fri Nov 18, 2016 1:24 pm

Roberthh wrote:That's what I am using to set the time after boot:

Code: Select all

from ntptime import settime
settime()

I am running a timer program on the ESP8266. Because the rtc is losing about 20 minutes/day I am calling setttime() every hour. I can't make it a day before I get the error shown below, which then stops the program from continuing to run. Is there a way to ignore an error like this so the program can continue to run

OSError: [Errno 110] ETIMEDOUT

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Setting RTC From Internet?

Post by Roberthh » Fri Nov 18, 2016 1:36 pm

Did you try to put the call to setttime() into a try/except clause, like

Code: Select all

try:
    settime()
except:
    pass
If you want to be more specific about the exception, you could try to catch TimeoutError only.
Last edited by Roberthh on Fri Nov 18, 2016 3:21 pm, edited 1 time in total.

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Setting RTC From Internet?

Post by weldeng » Fri Nov 18, 2016 2:52 pm

Roberthh wrote:Did you tr to put the call to setttime() into e try/except clause, like

Code: Select all

try:
    settime()
except:
    pass
If you want to be more specific about the exception, you could try to catch TimeoutError only.
I did not. I looked in the micropython documentation for release 1.8.5 and did not see that try and except were supported. Will try it tonight. Thanks.

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Setting RTC From Internet?

Post by weldeng » Sun Nov 20, 2016 2:35 pm

weldeng wrote:
Roberthh wrote:Did you tr to put the call to setttime() into e try/except clause, like

Code: Select all

try:
    settime()
except:
    pass
If you want to be more specific about the exception, you could try to catch TimeoutError only.
I did not. I looked in the micropython documentation for release 1.8.5 and did not see that try and except were supported. Will try it tonight. Thanks.

Your suggestion worked, thanks. Had four setttime() TimeoutErrors in a 12 hour period.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Setting RTC From Internet?

Post by pythoncoder » Mon Nov 21, 2016 7:43 am

@weldeng I've had problems with timeouts too. Rather than skipping the reading for a whole hour and risking another timeout I'd consider this approach: try a reading. If a timeout occurs repeat after (say) two minutes until you get a valid reading. Then wait an hour.

Another approach is to use a DS3231. These are amazingly cheap and precise (a minute or so per year) battery backed RTC's. They are so precise that I've used them for calibrating the RTC on the Pyboard.
Peter Hinch
Index to my micropython libraries.

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: Setting RTC From Internet?

Post by Lornioiz » Wed Nov 23, 2016 8:48 pm

weldeng wrote: I am running a timer program on the ESP8266. Because the rtc is losing about 20 minutes/day I am calling setttime() every hour. I can't make it a day before I get the error shown below, which then stops the program from continuing to run. Is there a way to ignore an error like this so the program can continue to run

OSError: [Errno 110] ETIMEDOUT

Can I ask you how do you do the hourly polling? Do you have a variable holding the elapsed time or do you use a less resource intensive method (if it exist at all).
In Python (i.e. with hardware unlimited resources) I would use that approach, but I'm curious to know if there is a better solution.

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: Setting RTC From Internet?

Post by Lornioiz » Wed Nov 23, 2016 9:18 pm

pythoncoder wrote: Another approach is to use a DS3231. These are amazingly cheap and precise (a minute or so per year) battery backed RTC's. They are so precise that I've used them for calibrating the RTC on the Pyboard.
On a loosely related note: I almost finished a driver for the DS3231, because I need a standalone hardware clock for a project. It works pretty well as a clock and I'm trying to use the internal alarm in order to trigger some functions in the main program (by the hardware interrupt).
My question is: it is really worth to use the hardware interrupt of the DS3231 alarm (resource wise)? Does a check of the elapsed time every cycle put much overhead compared to the use of interrupts?
I posted earlier today a question about how to use interrupts because I know next to nothing about them. I understand interrupts are needed for another purpose (stop the execution of the main code, execute a function and resume the main code were it was left) but do they have a performance/resource advantage as well?

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Setting RTC From Internet?

Post by weldeng » Wed Nov 23, 2016 11:01 pm

Lornioiz wrote:
weldeng wrote: I am running a timer program on the ESP8266. Because the rtc is losing about 20 minutes/day I am calling setttime() every hour. I can't make it a day before I get the error shown below, which then stops the program from continuing to run. Is there a way to ignore an error like this so the program can continue to run

OSError: [Errno 110] ETIMEDOUT

Can I ask you how do you do the hourly polling? Do you have a variable holding the elapsed time or do you use a less resource intensive method (if it exist at all).
In Python (i.e. with hardware unlimited resources) I would use that approach, but I'm curious to know if there is a better solution.
Here is how I am doing it. Note the indents are not showing when I cut and paste:

while True:
year, month, day, hour, minute, second, ms, dayinyear = utime.localtime()
# adjust hour for time zone below
if hour >= 5:
hour = hour-5
if hour < 5:
hour = hour +19

# hourly time correction
if hour != oldhour:
try:
settime() # reset real time clock(it looses up to about 3 minutes/hour)
except:
print ('error connecting, will try to adjust RTC in 60 minutes')
pass
oldhour = hour

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Setting RTC From Internet?

Post by pythoncoder » Thu Nov 24, 2016 7:50 am

@Lornioiz The main reason for using hardware interrupts is to achieve a fast response to an external event. By fast I mean times measured in small numbers of microseconds. As you indicate another reason is that they impose zero overhead until the event occurs. By contrast polling uses resources (depending on the frequency of polling) and imposes a variable latency ranging from very small to the polling interval. The penalty of using interrupts is that programming interrupt handlers requires care http://docs.micropython.org/en/latest/p ... rules.html. Further, the interrupt handler can run at any time, monopolising the CPU for a period. This can affect time critical code in the main program.

The key is identifying how much latency is tolerable in responding to an alarm. If (say) a second is acceptable then I'd poll it at one second intervals. The amount of processor power required to read it and check the result, compared to that available in a one second period, is small. At a guess perhaps 1% on an ESP8266, less on a Pyboard.

My DS3231 driver is here https://github.com/peterhinch/micropyth ... ter/DS3231. Its main feature is using careful timing accurately, and relatively rapidly, to calibrate the Pyboard's RTC. This is irrelevant to the ESP8266. But the basic functionality could easily be ported to the ESP8266.
Peter Hinch
Index to my micropython libraries.

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: Setting RTC From Internet?

Post by Lornioiz » Thu Nov 24, 2016 10:13 am

pythoncoder wrote: The key is identifying how much latency is tolerable in responding to an alarm. If (say) a second is acceptable then I'd poll it at one second intervals. The amount of processor power required to read it and check the result, compared to that available in a one second period, is small. At a guess perhaps 1% on an ESP8266, less on a Pyboard.
Thank you Pythoncoder.
Please correct me if I'm wrong: In order to know if it's time to poll the sensor I would need to check every cycle how much time is passed from the last polling. In order to do so, I could store the start time in a variable and compare it with the value of a utime.time() or utime.ticks_diff() call, something on the lines of what Weldeng wrote above.
There is a better way? Better yet, there is another way (at all) to do it?
I understand this may sounds a stupid question, but I'm used to work in a desktop environment were there are no resurce constrain and I'm wondering which is the most efficent way to do it in a mcu.

Thanks!

Post Reply