Page 1 of 1

time server for ESP8266

Posted: Mon Apr 25, 2016 4:14 pm
by greentree
I'm trying to set up a temperature sensor with an ESP8266 that repeatedly samples some DS18B20's, uploads data to a server, and puts itself to sleep for an interval.

The deepsleep state seems to reset the RTC, so I'm trying to create a minimalist way to get time from a server until other sleep states or ntp are implemented on this board.

In the unix port of micropython, this (based on discussion at http://stackoverflow.com/questions/1266 ... -in-python) works:

Code: Select all

from usocket import socket, AF_INET, SOCK_DGRAM, getaddrinfo
from ustruct import unpack, calcsize
NTP_PACKET_FORMAT = "!12I"
NTP_DELTA = 2208988800 # 1970-01-01 00:00:00
NTP_QUERY = '\x1b' + 47 * '\0'  
addr=getaddrinfo('pool.ntp.org', 123)[0][-1]
s=socket( AF_INET, SOCK_DGRAM)
s.sendto(NTP_QUERY.encode('utf-8'),addr)
msg, address = s.recvfrom(1024)
unpacked = unpack(NTP_PACKET_FORMAT,
                  msg[0:calcsize(NTP_PACKET_FORMAT)])
print(unpacked[10] + float(unpacked[11]) / 2**32 - NTP_DELTA)
However the same code on the ESP8266 port fails on sendto(), giving OSError: -1071239368

Does anyone have insight into why this is failing and possible workarounds?

Thanks!

Re: time server for ESP8266

Posted: Mon Apr 25, 2016 4:36 pm
by kfricke
Question or workaround: Keep it simple, stupid and only sleep for fixed timespans. Create a time agnostic sensor node and add the timestamp to the payload on the server on reception.
At least this is what my sensors do and works out really great.

Re: time server for ESP8266

Posted: Tue Apr 26, 2016 12:05 pm
by Damien
It should be possible to work around this issue by catching and ignoring that exception. It seems that the sendto does actually work, and the error is raised unnecessarily (although still looking into this). So try the following:

Code: Select all

try:
    s.sendto(...)
except OSError as er:
    if er.args[0] != -1071239368:
        raise er
You might also want to do s.settimeout(1) to put a timeout on the recvfrom call, in case the connection is lost (UDP is not a reliable protocol).

Re: time server for ESP8266

Posted: Wed May 04, 2016 8:42 am
by pfalcon
Thanks for the pointers on dealing with NTP in Python, we included a simple NTP module with the esp8266 scripts:
https://github.com/micropython/micropyt ... ntptime.py (in 1.8 firmware sent to KS backers too). NTP module was #3 is the module vote list, off the official target list, but thanks to greentree's research, here it is!

Re: time server for ESP8266

Posted: Thu May 05, 2016 4:12 pm
by greentree
Thanks to all three of you for your helpful comments. I'm glad this worked out so well for all of us, especially that I was able to contribute even with limited skills. Micropython is a great project and a great community of contributors.

I'm working on code that will store data with an estimated time stamp when wifi is down, and both uploads those data and corrects time using ntp when wifi is available. The key issue is fitting within the available memory. A lot of straightforward approaches trigger memory errors.

I'll post that when I get something that does all of the above. I'm sure forum users will have lots of ideas how to improve it.

Re: time server for ESP8266

Posted: Thu May 05, 2016 4:46 pm
by pfalcon
The key issue is fitting within the available memory.
Indeed, and in scripts/ntpdate.py, I took some simple steps to reduce memory allocations (can be optimized further too).