← index #16008Issue #16779
Related · medium · value 0.637
QUERY · ISSUE

Using ntptime.settime() while _thread is running can break sleep timing

openby GM-Script-Writer-62850opened 2024-10-14updated 2026-03-24
bugport-rp2

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

  • _thread_work() - measure how long time.sleep takes
  • wifi() - connect to wifi and reconnect if it does down
    • wifi_auth just has the wifi login variables
  • setTime() - this sets the clock using ntptime
    • this was unreliable in micropython 1.2, hence the try blocks
  • wait4clock() - just some busy work for asyncio, can be used to start process that depend on the clock being set
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())

Expected behaviour

Calls to time.sleep(1) and asyncio.sleep_ms(1000) should be near 1 second long

Observed behaviour

output of example code:

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

Additional Information

  • Note this does not have a 100% reproduction rate, feels near 50%
  • This is a regression since micropython 1.2 (rp2-pico-w-20230426-v1.20.0.u2f)
  • Note that Thonny is set to NOT synchronize device's real time clock (Tools -> Options-> Interpreter)

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

RP2: Pico 2 W Not connecting with thread running

closedby coencoensmeetsopened 2025-02-18updated 2025-03-26
bugport-rp2

Port, board and/or hardware

RP2 - Pico 2 W

MicroPython version

MicroPython v1.25.0-preview.229.g0d46e45a1 on 2025-01-27; Raspberry Pi Pico 2 W with RP2350

Reproduction

import time
import _thread
import urequests

SSID = ""
password = ""

def connect_normal():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(SSID, password)
    print("Connecting to network")

    start_time = time.time()
    while not wlan.isconnected() and time.time() - start_time < 20:
        print("Waiting for connection...")
        time.sleep(2)

    if wlan.isconnected():
        print("Connected to network")
    else:
        print("Failed to connect to network")

    print("Sleep for 2 seconds")
    time.sleep(2)
    wlan.disconnect()
    
def main_thread():
    connect_normal()

def second_thread():
    for (i) in range(10):
        print("Second thread")
        time.sleep(2)

_thread.start_new_thread(second_thread, ())
main_thread()

Other things tested but not working:

  • Extending the 20-second time limit.
  • Putting wlan = network.WLAN(network.STA_IF), wlan.active(True), and wlan.connect(SSID, password) before starting a thread.
  • Switching the functions connection_normal and second_thread to run in the other thread. So the connection is made on the newly opened thread.
  • Building firmware with the latest Pico SDK develop
  • Adding rp2.country("NL") and network.country(country).

Expected behaviour

Expected to connect to the network and print Connected to network.

Observed behaviour

No connection is established after the set time of 20 seconds. (Time is irrelevant as it never seems to connect). When the line _thread.start_new_thread(second_thread, ()) is commented out, a connection is found almost every single time.

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied