Update my RTC value



  • I want to update the RTC of my Lopy. Reading the docs seems that I just have to write the following code:

    from machine import RTC
    
    rtc = RTC()
    rtc.ntp_sync("es.pool.ntp.org") # I'm from Spain
    print(rtc.now(),'\n')
    

    But the output is: (1970, 1, 1, 0, 0, 0, 817, None).

    Then I thought, ok, I need to be connected to internet, thus I write the following code:

    from machine import RTC, idle
    from network import WLAN
    
    wlan = WLAN(mode=WLAN.STA)
    wlan.connect(ssid='vodafone55s6654', auth=(WLAN.WPA2, 'uh5a55ss8d4wed55'))
    while not wlan.isconnected():
        idle()
    print("WiFi connected succesfully")
    rtc = RTC()
    rtc.ntp_sync("es.pool.ntp.org")
    print(rtc.now(),'\n')
    

    But I have the same output, (1970, 1, 1, 0, 0, 0, 817, None), yet.

    If I put print(rtc.synced()) returns False confirming that it could not sync. I tried to init it with a random date but
    it is useless.

    (With rtc.ntp_sync("pool.ntp.org") or "0.europe.pool.ntp.org" instead of rtc.ntp_sync("es.pool.ntp.org") the outputs are the same)

    Thank you for all!



  • Hi,
    It might take a couple of seconds / minutes before the device is synced to the ntp time, depending on the availability and how often you already tried (I think they have some sort of fair use policy). You might want to use a loop like this:

    while not rtc.synced():
       time.sleep(1)
    

    Which will continue after syncing


Log in to reply
 

Pycom on Twitter