import _thread
import ntptime
import asyncio
import time
sleep=asyncio.sleep_ms
#ntptime.host="10.0.0.69"
wlan = 0
def _thread_work():
while 1:
then=time.ticks_ms()
time.sleep(1)
then=time.ticks_diff(time.ticks_ms(),then)
print("Core 1:",then)
async def wifi():
from wifi_auth import ssid, password
import network
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(pm = 0xa11140)# Power management is very very bad, ping time is around 1000x worse and packet loss insane
wlan.connect(ssid, password)
while 1:
while 1:
wstat=wlan.status()
if wstat < 0 or wstat >= 3:
break
print('Waiting for WiFi connection...')
await sleep(1000)
if wlan.status() == 3:
status = wlan.ifconfig()
print('Wifi Connected; ip =',status[0])
while wlan.isconnected():
await sleep(30000)
print('WiFi Down')
else:
print("Failed to connect to wifi; retry in 30 seconds")
await sleep(30000)
wlan.connect(ssid, password)
#END wifi()
async def setTime():
#ntptime.settime() failure: [Errno 110] ETIMEDOUT
#ntptime.settime() failure: overflow converting long int to machine word
print('Attempt to set time')
success=0
try:
print("calling ntptime.settime()")
ntptime.settime()
success=1
except OverflowError:
# it is not going to work
print("overflow error; settime is borked")
except OSError:
print("ntptime.settime() failure")
if not success:
await sleep(10000)
asyncio.create_task(setTime())
else:
print("END of setTime() process")
#END setTime()
async def wait4clock():
delay=2000
while time.time() < 1640995200: # Wait for clock to set or a 1 year...)
print("Waiting for clock...")
await sleep(3000)
print('Clock set:',time.time())
#_thread.start_new_thread(_thread_work,())
#END wait4clock()
async def main():
asyncio.create_task(wait4clock())
asyncio.create_task(wifi())
while not wlan or not wlan.isconnected():
then=time.ticks_ms()
await sleep(1000)
then=time.ticks_diff(time.ticks_ms(),then)
print("Core 0:",then,"Waiting for wifi")
asyncio.create_task(setTime())
while 1:
then=time.ticks_ms()
await sleep(1000)
then=time.ticks_diff(time.ticks_ms(),then)
print("Core 0:",then)
_thread.start_new_thread(_thread_work,())
asyncio.run(main())
Waiting for clock...
Waiting for WiFi connection...
Core 1: 1000
Core 0: 1000 Waiting for wifi
Waiting for WiFi connection...
Core 1: 1000
Core 0: 1001 Waiting for wifi
Waiting for WiFi connection...
Core 1: 1000
Waiting for clock...
Core 0: 1003 Waiting for wifi
Wifi Connected; ip = 10.0.0.190
Core 1: 1000
Core 0: 1001 Waiting for wifi
Attempt to set time
calling ntptime.settime()
END of setTime() process
Core 1: 22526
Core 0: 22522
Clock set: 1728875328
Core 1: 5324
Core 0: 5324
Core 1: 7473
Core 0: 7474
Core 1: 17208
Core 0: 17208
Core 1: 5319
Core 0: 5319
Core 1: 24678
Core 0: 24678
Core 1: 5325
Core 0: 5325
Core 1: 24678
Core 0: 24678
Core 1: 5330
Core 0: 5330
Core 1: 24673
Core 0: 24673
Core 1: 5325
Core 0: 5325
Core 1: 24684
Core 0: 24684
Core 1: 5319
Core 0: 5319
Core 1: 24682
Core 0: 24683
Core 1: 5324
Core 0: 5324
Core 1: 1939
Core 0: 1939
Core 1: 5734
Core 0: 5734
Core 1: 5836
Core 0: 5836
Core 1: 11162
Core 0: 11162
Core 1: 5329
Core 0: 5330
Core 1: 7059
Core 0: 7059
Core 1: 13925
Core 0: 13926
Core 1: 3687
Core 0: 3687
Core 1: 5325
Core 0: 5325
Core 1: 7269
Core 0: 7269
Core 1: 17413
Core 0: 17413
Core 1: 5325
Core 0: 5325
Core 1: 1018
Core 0: 1018
Core 1: 23655
Core 0: 23655
Core 1: 5324
Core 0: 5324
Port, board and/or hardware
Raspberry Pi PICO W
MicroPython version
MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040
Reproduction
Expected behaviour
Calls to
time.sleep(1)andasyncio.sleep_ms(1000)should be near 1 second longObserved behaviour
output of example code:
Additional Information
Code of Conduct
Yes, I agree