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.
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:14 am

weldeng wrote:
Lornioiz wrote: 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:

Thank you, this is basically what I usually do (I use the utime.time method but the concept is the same).

Saran
Posts: 16
Joined: Thu May 28, 2015 6:52 pm

Re: Setting RTC From Internet?

Post by Saran » Thu May 31, 2018 3:29 pm

MicroPython v1.9.4 on 2018-05-11; WiPy with CC3200

Code: Select all

>>> import ntptime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: module not found
Where can I get ntptime module?

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

Re: Setting RTC From Internet?

Post by pythoncoder » Thu May 31, 2018 4:17 pm

The ESP8266 version is here. This will doubtless require some adaptation for the WiPy.
Peter Hinch

matthiasheng
Posts: 6
Joined: Sat Oct 06, 2018 1:57 pm

Re: Setting RTC From Internet?

Post by matthiasheng » Mon Oct 22, 2018 6:33 am

Hi All,

If the original server in the ntptime.py happen to not working well in my location, how can i change the server?
I found that when i was in Malaysia, the ntptime.py works great, i can use both ntptime.settime() and time.localtime( ntptime.time()) but now i'm in China and the ntptime.settime() doesn't work anymore, time.localtime( ntptime.time()) works occasionally like 1 success out of 3.

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

Re: Setting RTC From Internet?

Post by pythoncoder » Tue Oct 23, 2018 7:30 am

Does China operate its own NTP service? Perhaps the address "pool.ntp.org" needs changing in ntptime.py to accommodate the Great Firewall of China? Or you could try increasing the timeout; though if there is high network latency this may imply loss of accuracy.
Peter Hinch

matthiasheng
Posts: 6
Joined: Sat Oct 06, 2018 1:57 pm

Re: Setting RTC From Internet?

Post by matthiasheng » Tue Oct 23, 2018 2:50 pm

Thanks pythoncoder, as i know they maintain a few servers and "cn.pool.ntp.org" seem to be fastest from my location. Does it means i can modify the ntptime.py below just by changing the host to this one?
Do i need to rename this modified ntptime.py to other name before i move it to the esp board so that i can import and use it? There is already a build in ntptime module from the firmware.


try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct

# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600

#host = "pool.ntp.org"
host = "cn.pool.ntp.org"

def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA

# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())

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

Re: Setting RTC From Internet?

Post by pythoncoder » Wed Oct 24, 2018 5:26 am

By default it will find the official version before your own. There are ways to deal with this but in this case I'd recommend simply renaming your version. You can then use the official version or the China version depending on your location.
Peter Hinch

Post Reply