Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTP improvement: Retry until year > 2000 #4

Open
BetterAutomations opened this issue Jun 3, 2024 · 0 comments
Open

NTP improvement: Retry until year > 2000 #4

BetterAutomations opened this issue Jun 3, 2024 · 0 comments

Comments

@BetterAutomations
Copy link

BetterAutomations commented Jun 3, 2024

Here's a little improvement you can do on the NTP script that I do on mine. Occasionally, the first ntptime.settime() call fails, so I retry until the year is no longer 2000.

import time
from micropython import const


NTP_MAX_RETRIES = const(10)


def get_year():
    tm = time.gmtime()
    return int(tm[0])


def is_synched():
    # Epoch is 2000. If year is still epoch it must not yet be synched.
    return get_year() > 2000


def sync():
    ntptime.host = host

    tries = 0
    while tries < NTP_MAX_RETRIES:
        tries += 1
        ntptime.settime()

        if is_synched():
            break
    else:
        raise Exception('NTP max retries exceeded')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant