双核在复盆子Pi PICO W与MicroPython



我在运行一部分代码时遇到了问题,该代码更具体地定义为新线程CoreTask((。我不知道为什么我不能打开/关闭内置LED。其余的代码看起来像预期的那样工作(WIFI集成和"httpd服务"运行良好(。我使用树莓派Pico W与最新的MicroPython加载。

请建议。。。谢谢

import machine
import _thread
import network
import socket
import utime
from machine import Pin
led = machine.Pin('LED', machine.Pin.OUT)
ssid = 'someSSID'
password = 'somePASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
html = """<!DOCTYPE html>
<html>
<body>
<div id="humidity">100</div>
<div id="temperature">21</div>
</body>
</html>
"""

sLock = _thread.allocate_lock()
def CoreTask():
while True:
sLock.acquire()
print('LED...')
led.on()
utime.sleep(1)
led.off()
utime.sleep(1)
sLock.release()
_thread.start_new_thread(CoreTask, ())
while True:
sLock.acquire()
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
utime.sleep(1)

# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )

# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('listening on', addr)

# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)

request = cl.recv(1024)
print(request)

request = str(request)
response = html

cl.send('HTTP/1.0 200 OKrnContent-type: text/htmlrnrn')
cl.send(response)
cl.close()

except OSError as e:
cl.close()
print('connection closed')

sLock.release()

我也很想知道一个解决方案,到目前为止,我让板载led在单独的线程中工作的最好尝试是下面的简单代码,其他更复杂的尝试都失败了。

我只用板载LED测试过这个,所以如果有人可以尝试线程是否在真正的GPIO上工作?如果是这样,我认为这不是一个问题,因为大多数时候我们都会发出";状态闪烁";在项目中的其他LED上,而不是板载LED。

import machine
import _thread
import time
def led():
led = machine.Pin('LED', machine.Pin.OUT)
while True:
led.on()
time.sleep(.5)
led.off()
time.sleep(.5)

def pnt():
i=0
while True:
i += 1
print(f"rThis is the {i} cycle......", sep="", end="")
time.sleep(1)

if __name__ == "__main__":
th1 = _thread.start_new_thread(pnt, ())
#th2 = _thread.start_new_thread(led, ()) This line did not work
led()

我想有人在一段视频中说,线程仍在实验中,在pico-w上有问题,他们正在研究

我也无法用uasyncio锻炼如何达到同样的效果,看起来叫LED闪烁不是为了编程,而是为了真正的身体运动,所以不能";等待";。有人能给马指一条正确的路吗?

它是一个非常重要的功能来等待程序(连接循环(;状态闪烁";

最新更新