ntptime kills the wifi station, hard reset required to fix

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
User avatar
pythoncoder
Posts: 5208
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ntptime kills the wifi station, hard reset required to fix

Post by pythoncoder » Tue Apr 20, 2021 12:36 pm

davef wrote:
Fri Apr 16, 2021 10:17 pm
...
Is that -3155673600 indicative of the problem? Actually after trying this a few more times it seems if I don't wait more than one (?) second between tries then I get the error...
If you look at ntptime.py you'll find this line. A return value of -3155673600 means that the time() function has failed, msg was not updated, so the final subtraction returns -NTP_DELTA.

I think the settime() function is buggy as it doesn't cater for this case. You might rewrite it as follows:

Code: Select all

def settime():
    t = time()
    if t < 0:
        return False
    import machine
    import utime

    tm = utime.gmtime(t)
    machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
    return True
You could then detect failure in your code and take remedial action (wait a few seconds and try again?).

I have raised this issue against ntptime as this is surely a bug.
Peter Hinch

davef
Posts: 277
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ntptime kills the wifi station, hard reset required to fix

Post by davef » Tue Apr 20, 2021 7:55 pm

Hi Peter,

Thanks heaps for identifying what hopefully is the cause of ntptime freezing in my system.
You might rewrite it as follows:
:)
I would really like to be able to rewrite, add, change source code and re-build. Latest effort here viewtopic.php?f=2&t=9886.

I am planning to set-up a new 64-bit machine for my Micropython work, but first I will buy a bunch of NodeMCU's as suggested in the thread.

Appreciate your efforts towards sorting out my issues.

davef
Posts: 277
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ntptime kills the wifi station, hard reset required to fix

Post by davef » Fri Apr 23, 2021 7:09 pm

Have not seen any failures at settime() with your code modification ... on an ESP8266.

The random error I still get on the modified ESP8266 and the stock ESP32 firmware is:

Code: Select all

[Errno 110] ETIMEOUT
Does not the 110 indicate an error thrown by the ESP operating system?

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

Re: ntptime kills the wifi station, hard reset required to fix

Post by kevinkk525 » Mon May 03, 2021 10:13 am

The error still persists on the pyboard-D with micropython 1.15... Any way to solve this? The first ntptime.time() works succesfully, afterwards no connection works anymore and it needs a hard reset.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

davef
Posts: 277
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ntptime kills the wifi station, hard reset required to fix

Post by davef » Mon May 03, 2021 10:34 am

I can still get up to 5 fails on ntptime.time()

Here is what I do:

Code: Select all

def getntptime():
    now = 0
#    ntptime.host = 'nz.pool.ntp.org'
    count = 0

    while (count < 5):
        count += 1

        try:
            now = ntptime.time()
        except OSError as error:
            try:
                with open('errors.txt', 'a') as outfile:
                    outfile.write('ntptime.time() failed with now = ' + str(now) + '\n')
            except OSError:
                pass

        print('.', end = '')
        utime.sleep(1)

        if (now > 660000000):
            count = 0
            break

    if (count == 5):
        count = 0

        try:
            with open('errors.txt', 'a') as outfile:
                outfile.write('ntptime.time() failed 5 times' + '\n')
        except OSError:
            print('oops')

        machine.reset()

    print(' got epoch time')
    utime.sleep(1)
Those 5 fails will be now = 0

The 660000000 is just a "recent time". Probably you could say "as long as now != 0" you have a valid epoch time.

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

Re: ntptime kills the wifi station, hard reset required to fix

Post by kevinkk525 » Mon May 03, 2021 10:35 am

I don't have an epoch problem on the pyboard-D. The first sync works and after that I can't connect anywhere anymore. No socket will work, all connection attempts time out.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

davef
Posts: 277
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ntptime kills the wifi station, hard reset required to fix

Post by davef » Mon May 03, 2021 10:48 am

We need to hear from kwiley!

Post Reply