Adsense HTML/JavaScript

Wednesday, May 4, 2022

ESP32-C3/MicroPython exercise: update time using ntptime

 MicroPython (v1.18 ) exercise run on ESP32-C3-DevKitM-1, to update time using utptime.

"""
MicroPython/ESP32C3 exercise run on ESP32-C3-DevKitM-1,
about time.
"""
import uos
import usys
import time

import network
import ntptime

TIME_OFFSET = +8 * 60 *60   #offset for your timezone

print("\n====================================")
print(usys.implementation[0], uos.uname()[3],
      "\nrun on", uos.uname()[4])
print("====================================\n")

def connect_and_update_ntptime():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.disconnect()
    time.sleep(1)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('ssid', 'password')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    
    ntptime.settime()
    wlan.disconnect()

connect_and_update_ntptime()
now_localtime =time.localtime(time.time() + TIME_OFFSET)
print(now_localtime)



No comments:

Post a Comment