Page 1 of 1

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

Posted: Sat Nov 09, 2019 12:18 am
by misaalanshori
Here is the error:
>>> import clock
connecting to last APTrue
Connection successful
('', '', '', '') OpenWrt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 4, in <module>
File "", line 23, in time
OSError: [Errno 110] ETIMEDOUT
Here is the file:

Code: Select all

import quickoled, ntptime, utime, random, network, wificonnect
while True:
    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)
and here is the file:

Code: Select all

    import usocket as socket
    import socket
    import ustruct as struct
    import struct
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600

host = ""

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)
        res = s.sendto(NTP_QUERY, addr)
        msg = s.recv(48)
    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

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

Posted: Sat Nov 09, 2019 6:50 am
by kevinkk525
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.

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

Posted: Tue Nov 12, 2019 4:10 am
by misaalanshori
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...

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

Posted: Tue Nov 12, 2019 6:57 am
by kevinkk525
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.