Getting the time from the internet on a pico W using MicroPython

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

Post Reply
User avatar
/RaspberryPi
Corporate
Posts: 2449
Joined: Wed Jun 05, 2019 1:29 am

Getting the time from the internet on a pico W using MicroPython

Post by /RaspberryPi »

Anyone else having problems with getting a reliable connection to get the time. I want to get the time off an time server once a day and have been having all kinds of issues.
I tried a simple example of getting the time from the JSON test server and that works well -- MOST OF THE TIME. Sometimes it just hangs with no timeout. Other times I get errors, but they are not predictable. Finally, I put some loops in to try multiple times and that works -- SOMETIMES.
Here is the code that I've got currently:
#works though slowly and sometimes needs the connection reset
import network
import machine
import time
import rp2
import secrets # where I keep my SSID and PASSWORD
import urequests
import timeoffset # where I keep the delta between local and universal time
#devices
led_onboard = machine.Pin("LED", machine.Pin.OUT) #this is different for the pico W
#functions
def get_the_time():
max_wait = 10
netstatus = -1
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID, secrets.PASSWORD)
try:
while netstatus < 0:
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
netstatus = wlan.status()
except Exception as e:
print("network start exception: ",e)
raise RuntimeError("network connection failed")
print('connected: ',netstatus)
status = wlan.ifconfig()
print( 'ip = ' + status[0] )# Should be connected and have an IP address
max_wait = 10
temp = 0
while max_wait > 0:
try:
# Get IP address for jsontest
r = urequests.get("http://date.jsontest.com")
temp = r.json()
r.close()
break
except Exception as e:
print("time network request failure: ",e)
max_wait = max_wait-1
print("tries remaining: ",max_wait)
mstime = temp["milliseconds_since_epoch"]
epochtimeinseconds = mstime//1000
epochtimeinseconds = epochtimeinseconds + timeoffset.TIMEOFFSETSECONDS
datetimetuple = time.localtime(epochtimeinseconds)
return datetimetuple
def get_hours(datetimetuple):
hours = datetimetuple[3]
return hours
def get_minutes(datetimetuple):
minutes = datetimetuple[4]
return minutes
#main
led_onboard.on() #just to show its working
rp2.country('US')
datetimetuple = get_the_time()
print(datetimetuple)
print("hours: ",get_hours(datetimetuple))
print("minutes: ",get_minutes(datetimetuple))
led_onboard.off() #just to show its stopped working
I'd rather get the time from a network time protocol server, but I have had no luck with that, because I am not that strong at microPython. Any examples will help. Thanks

submitted by /u/cebe55
[link] [comments]

More...
Post Reply

Return to “Raspberry Pi Forum”