Page 3 of 3

Re: Setting RTC From Internet?

Posted: Thu Nov 24, 2016 10:14 am
by Lornioiz
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).

Re: Setting RTC From Internet?

Posted: Thu May 31, 2018 3:29 pm
by Saran
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?

Re: Setting RTC From Internet?

Posted: Thu May 31, 2018 4:17 pm
by pythoncoder
The ESP8266 version is here. This will doubtless require some adaptation for the WiPy.

Re: Setting RTC From Internet?

Posted: Mon Oct 22, 2018 6:33 am
by matthiasheng
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.

Re: Setting RTC From Internet?

Posted: Tue Oct 23, 2018 7:30 am
by pythoncoder
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.

Re: Setting RTC From Internet?

Posted: Tue Oct 23, 2018 2:50 pm
by matthiasheng
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())

Re: Setting RTC From Internet?

Posted: Wed Oct 24, 2018 5:26 am
by pythoncoder
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.

Re: Setting RTC From Internet?

Posted: Sun Jun 09, 2019 10:03 am
by msinn
I found out, you can set the ntp host to use before calling settime():

Code: Select all

# set RTC from local ntp server
import ntptime

ntptime.host = '10.0.0.4'
try:
    ntptime.settime()
except:
    print('ERROR: Cannot set time via ntp')
(Sorry, BBCode ist disabled for me)