Pico W连接到路由器,并自动获取ip地址,同时启动本地服务器:
import network
import socket
from machine import Pin,RTC,Timer,WDT
import time,utime
ssid = 'WJG-iphone'
password = '13616781309'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
print(max_wait)
#wlan.connect(ssid, password)
print(wlan.status())
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
wdt=WDT(timeout=5000)
#raise RuntimeError('network connection failed')
else:
print('connected')
led.value(1)
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
# Open socket
addr = socket.getaddrinfo(status[0] , 4002)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(10)
try:
print(wlan.status())
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print(request)
cl.send("led on")
cl.close()
except OSError as e:
cl.close()
print('connection closed')
Pico W连接到路由器,并自动获取ip地址,同时启动本地服务器
-
- 随机主题
- 回复总数
- 阅读次数
- 最新文章
-
- 1回复总数
- 307 阅读次数
- 最新文章 由 shaoziyang
2023年 1月 21日 15:45
-
- 13回复总数
- 1073 阅读次数
- 最新文章 由 czfx
2023年 1月 25日 06:11
-
- 1回复总数
- 646 阅读次数
- 最新文章 由 shaoziyang
2023年 1月 22日 13:31