time server for ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
greentree
Posts: 15
Joined: Wed Dec 16, 2015 3:16 am

time server for ESP8266

Post by greentree » Mon Apr 25, 2016 4:14 pm

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!

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: time server for ESP8266

Post by kfricke » Mon Apr 25, 2016 4:36 pm

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.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: time server for ESP8266

Post by Damien » Tue Apr 26, 2016 12:05 pm

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).

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: time server for ESP8266

Post by pfalcon » Wed May 04, 2016 8:42 am

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!
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

greentree
Posts: 15
Joined: Wed Dec 16, 2015 3:16 am

Re: time server for ESP8266

Post by greentree » Thu May 05, 2016 4:12 pm

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.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: time server for ESP8266

Post by pfalcon » Thu May 05, 2016 4:46 pm

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).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply