Raspberry Pi Pico W/MicroPython connect to WiFi network and synchronize to NTP Time Server
Exercise run on Raspberry Pi Pico W with MicroPython v1.19.1, connect to WiFi network and synchronize to NTP Time Server.
mpyPicoW_toConnect_ntptime.py
"""
MicroPython/Pico W
connect to WiFi using network.WLAN
once connected, synchronize with NTP Time Server using ntptime
ref:
https://docs.micropython.org/en/latest/library/network.WLAN.html
"""
import time
import network
import machine
import ubinascii
import sys
import os
import ntptime
ssid = "ssid"
password = "password"
# Flash onboard LED three times to indicate program start
led = machine.Pin("LED", machine.Pin.OUT)
for l in range(3):
led.off()
time.sleep(0.3)
led.on()
time.sleep(0.3)
led.off()
print("=========================================================")
print(sys.implementation[0], os.uname()[3],
"\nrun on", os.uname()[4])
print("=========================================================")
print()
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC =", mac)
# WLAN.status([param]) Return the current status of the wireless connection.
# https://docs.micropython.org/en/latest/library/network.WLAN.html#network.WLAN.status
wlan_status_meaning = ((network.STAT_IDLE, "STAT_IDLE – no connection and no activity"),
(network.STAT_CONNECTING, "STAT_CONNECTING – connecting in progress,"),
(network.STAT_WRONG_PASSWORD, "STAT_WRONG_PASSWORD – failed due to incorrect password"),
(network.STAT_NO_AP_FOUND, "STAT_NO_AP_FOUND – failed because no access point replied"),
(network.STAT_CONNECT_FAIL, "STAT_CONNECT_FAIL – failed due to other problems"),
(network.STAT_GOT_IP, "STAT_GOT_IP – connection successful")
)
def decode_wlan_status(s):
print(s, end=" : ");
meaning = "unknown"
for m in wlan_status_meaning:
if s == m[0]:
meaning = m[1]
break
print(meaning)
if s == network.STAT_GOT_IP:
led.on()
return True
else:
led.off()
return False
def toConnect():
global wlan
wlan.connect(ssid, password)
print(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
#wlan.connect(ssid, password)
wStatus = wlan.status()
if decode_wlan_status(wStatus):
break
max_wait -= 1
time.sleep(1)
while True:
print("---------------------------");
print("- connect to WiFi")
toConnect()
# Handle connection error
wlan_status = wlan.status()
if wlan_status != 3:
print("network connection failed ", wlan_status)
else:
break
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
print()
print("- Synchronize to NTP Time Server using ntptime -")
print("Local time before synchronization:%s" %str(ntptime.utime.localtime()))
print(" time.localtime():%s" %str(time.localtime()))
print(" time.time(): %s" %str(time.time()))
ntptime.settime()
print("Local time after synchronization: %s without timezone adjust" %str(ntptime.utime.localtime()))
print(" time.localtime():%s" %str(time.localtime()))
print(" time.time(): %s" %str(time.time()))
TIMEZONE_OFFSET = UTC_OFFSET = 8 * 60 * 60 #GMT+8
print("With Time Zone adjust -")
#it will keep counting in REPL Shell
while True:
print("=>", time.localtime(time.time() + TIMEZONE_OFFSET), end="\r")
time.sleep(0.5)
print("\n~ bye ~\n")
Comments
Post a Comment