How do i fix "OSError: [Errno 110] ETIMEDOUT" on this code?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
misaalanshori
Posts: 23
Joined: Sat Jun 22, 2019 6:07 am

How do i fix "OSError: [Errno 110] ETIMEDOUT" on this code?

Post by misaalanshori » Sat Nov 09, 2019 12:18 am

Here is the error:
>>> import clock
connecting to last APTrue
Connection successful
('192.168.0.33', '255.255.255.0', '192.168.0.1', '192.168.0.1') OpenWrt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "clock.py", line 4, in <module>
File "ntptime.py", line 23, in time
OSError: [Errno 110] ETIMEDOUT
Here is the clock.py file:

Code: Select all

import quickoled, ntptime, utime, random, network, wificonnect
while True:
    quickoled.oled.fill(0)
    time = ntptime.time()
    tm = utime.localtime(time)
    randomY = random.randint(0,108)
    randomX = random.randint(0,64)
    print((tm[0], tm[1], tm[2], tm[6] + 7, tm[3], tm[4], tm[5], 0))
    dateStr = str(tm[2]) + " - " + str(tm[1]) + " - " + str(tm[0])
    timeStr = str(tm[6] + 7) + ":" + str(tm[3]) + ":" + str(tm[4]) + ":" + str(tm[5])
    quickoled.oled.text(dateStr, randomX, randomY, 1)
    quickoled.oled.text(dateStr, randomX, randomY + 10, 1)
    quickoled.oled.show()
and here is the ntptime.py file:

Code: Select all

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"

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)
    try:
        s.settimeout(5)
        res = s.sendto(NTP_QUERY, addr)
        msg = s.recv(48)
    finally:
        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)
    machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
I have changed the s.settimeout() value but its still not working and giving me the same error

kevinkk525
Posts: 511
Joined: Sat Feb 03, 2018 7:02 pm

Re: How do i fix "OSError: [Errno 110] ETIMEDOUT" on this code?

Post by kevinkk525 » Sat Nov 09, 2019 6:50 am

Does it happen every time you try to synchronize the time?

On my devices syncing the time often fails at first try. If that happens I make it retry 5 seconds later. Typically that is enough but occasionally it needs more retries, on one device it recently logged 38 consecutive attempts until it actually succeeded (with 2 wifi reconnects in between).
So it is normal that it happens, as long as you get some successful attempts too.
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

misaalanshori
Posts: 23
Joined: Sat Jun 22, 2019 6:07 am

Re: How do i fix "OSError: [Errno 110] ETIMEDOUT" on this code?

Post by misaalanshori » Tue Nov 12, 2019 4:10 am

Yes, the error happens when its trying to sync the time. So my solution was to put the syncing code in a "try:" and put "continue" in the "except:" so it just continues the loop and just tries again instead of stopping the program. There's probably a better solution but for now its good enough for me...

kevinkk525
Posts: 511
Joined: Sat Feb 03, 2018 7:02 pm

Re: How do i fix "OSError: [Errno 110] ETIMEDOUT" on this code?

Post by kevinkk525 » Tue Nov 12, 2019 6:57 am

make sure to have some time between different attempts. At first I also just used try: continue but on sometimes the sockets wouldn't have been clean up yet and my esp8266 crashed after too many attempts. Now I have a 5 seconds delay between the attempts and that seems to work reliable.
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

Post Reply