树莓派 Pico W 初测

店长大人 8.5m2022-10-032659 次点击2 人感谢

8月底海购的树莓派 pico w在漂洋过海一个多月后,终于送到了。今天迫不及待地开始测试。
在网上抄了一个局域网内远程点灯的程序,感觉pico w目前除了生态不如esp外,WiFi连接速度和稳定性,都是极好的,毕竟是英飞凌的芯片,没得说。
4aa881edc9999af0cea6b8dfa1c40455.jpg

4a9596784595f63320b5da59f7ebfb3e.jpg

视频如下:
https://www.bilibili.com/video/BV18e4y1J7mj/ (下方视频如无法显示请点此链接查看)

https://gitee.com/maowenniao/pico-w/blob/master/9c8d787cf996191fe14d6ad833799523.mp4

1:此处下载pico w micropythin的SDK uf2 文件,拖入pico w 中:
https://micropython.org/download/rp2-pico-w/rp2-pico-w-latest.uf2

2: 打开Thonny,将以下代码写入,并保存至pico w,点击运行,记得修改自己的wifi信息。

3:待pico w 连接至wifi后,控制台输出pico w ip, 在浏览器地址栏中输入:
关灯
pico w ip/light/off
点灯
pico w ip/light/on

代码:

import network
import socket
import time

from machine import Pin

### Select the onboard LED
led = machine.Pin("LED", machine.Pin.OUT)

ssid = '你的wifi ssid'
password = '你的WiFi 密码'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <head> <title>Pico W test </title> </head>
  <body> <h1> Pico W test</h1>
    <p>%s</p>
  </body>
</html>
"""

### 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...')
  time.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)
    led_on = request.find('/light/on')
    led_off = request.find('/light/off')
    print( 'led on = ' + str(led_on))
    print( 'led off = ' + str(led_off))

    if led_on == 6:
      print("led on")
      led.value(1)
      status = "LED is ON"

    if led_off == 6:
      print("led off")
      led.value(0)
     status = "LED is OFF"

    response = html % status
    cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
    cl.send(response)
    cl.close()

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

来自官方的静态网页代码

import network
import socket
import time

from machine import Pin

led = Pin(15, Pin.OUT)

ssid = 'Dandandarendewifi'
password = 'yanxiashigeerhuo'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
 <html>
 <head> <title>Pico W</title> </head>
 <body> <h1>Pico W</h1>
 <p>Hello World</p>
 </body>
 </html>
 """

 # 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...')
time.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)
        cl_file = cl.makefile('rwb', 0)
        while True:
            line = cl_file.readline()

            if not line or line == b'\r\n':
                break
        response = html
        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        cl.send(response)
        cl.close()

    except OSError as e:
        cl.close()
        print('connection closed')
收藏 ♥ 感谢
平头idea 86.75m 2022-10-03 
666
店长大人 8.5m 2022-10-03 
来自官方的静态网页代码
'''
import network
import socket
import time

from machine import Pin

led = Pin(15, Pin.OUT)

ssid = 'Dandandarendewifi'
password = 'yanxiashigeerhuo'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>Hello World</p>
</body>
</html>
"""

# 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...')
time.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)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()

if not line or line == b'\r\n':
break
response = html
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()

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

登录注册 后可回复。