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...
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
1 post
• Page 1 of 1
- /RaspberryPi
- Corporate
- Posts: 2449
- Joined: Wed Jun 05, 2019 1:29 am
Post Reply
1 post
• Page 1 of 1
Return to “Raspberry Pi Forum”
Jump to
- From Retro to Current
- ↳ Classified Ads
- ↳ Community
- ↳ Marketplace
- ↳ Jobs
- ↳ Dating
- ↳ Housing
- Nintendo Forums
- ↳ SNES Classic Edition Forum
- ↳ Switch Forum
- ↳ NES Classic Edition Forum
- ↳ Wii U Forum
- ↳ 3DS Forum
- ↳ Wii Forum
- ↳ DS Forum
- ↳ Gamecube Forum
- ↳ Game Boy Advance Forum
- ↳ Game Boy Color Forum
- ↳ Nintendo 64 Forum
- ↳ Super Nintendo Forum
- ↳ Game Boy Forum
- ↳ Nintendo Entertainment System Forum
- ↳ Game & Watch Forum
- SONY Forums
- ↳ PlayStation 5 Forum
- ↳ PlayStation Classic Forum
- ↳ PlayStation 4 Forum
- ↳ PlayStation Vita Forum
- ↳ PlayStation 3 Forum
- ↳ PlayStation Portable Forum
- ↳ PlayStation 2 Forum
- ↳ PlayStation 1 Forum
- Microsoft Forums
- ↳ Xbox Series Forum
- ↳ Xbox One Forum
- ↳ Xbox 360 Forum
- ↳ Xbox Forum
- SEGA Forums
- ↳ Genesis Mini Forum
- ↳ Dreamcast Forums
- ↳ Sega Saturn Forum
- ↳ Genesis Forum
- ↳ Master System Forum
- Other System Forums
- ↳ General Discussion
- ↳ Other Console Forum
- ↳ Arcade Cabinet Forum
- ↳ Windows, Mac, & Linux Forum
- ↳ Atomiswave Forums
- ↳ WWW.ATOMISWAVE.NET
- ↳ General Discussion
- ↳ Tech Support
- ↳ System Setups
- ↳ Modified Moderboards
- ↳ Home Consoles & Systems
- ↳ Arcade & Candy Cabinets
- ↳ Marketplace
- ↳ Video Game Marketplace
- ↳ Game Discussion
- ↳ Video Game Catalogs
- ↳ FAQs, Reviews, & Guides
- ↳ Assorted Discussion
- ↳ Custom Ideas
- ↳ Open Discussion
- ↳ SNK Neo Geo Forums
- ↳ General Discussion
- ↳ Buying & Selling
- ↳ Tech Support
- ↳ Systems
- ↳ Neo Geo Arcade Stick Pro Forum
- ↳ Neo Geo Mini Forum
- ↳ Neo Geo X Forum
- ↳ Neo Geo CD Forum
- ↳ Neo Geo MVS Forum
- ↳ TurboGrafx-16 Forums
- ↳ General Discussion
- ↳ Marketplace
- ↳ Magazine Scans
- ↳ Games
- ↳ TurboChip/HuCard Forum
- ↳ CD-ROM²/Super CD-ROM²/Arcade CD-ROM² Forum
- ↳ Systems
- ↳ TurboGrafx-16 Mini Forum
- ↳ TurboDuo Forum
- ↳ TurboExpress/PC Engine GT/LT Forum
- ↳ SuperGrafx Forum
- ↳ TurboGrafx-CD/PC Engine CD Forum
- ↳ TurboGrafx-16/PC Engine Core Forum
- ↳ Raspberry Pi Forum
- ↳ Atari Forum