树莓派Pico的小应用

开始的设想

pico出来一段时间了,这个只要4刀的小开发板被各硬件营销号捧得很高。本来不以为意,但在肝动森的某个下午,看到家门口的邮箱mail_box突然觉得很可爱,就想看看能不能用pico实现一个成品版。想当然的认为只要有wifi模块+步进电机模块就能搞定。
于是,开始了pico的学习。

开机

  • 按住板上的BOOTSEL键,连接micro usb至电脑
  • 官网上下载适合pico用的micropython固件,拖至pico中,上传完毕后pico会自动从电脑弹出
  • 再将pico直接连接至电脑,不用按BOOTSEL键
  • 使用Thonny在pico中编程,Thonny会自动识别pico解释器,详见Thonny窗口:工具-设置-解释器
  • 查看系统中的包
1
2
import os
help(“modules”)

点亮Pico上的LED

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
from machine import Pin
import time

# LED灯的pin脚
l=Pin(25,Pin.OUT)

# 常亮
# l.high()

# 闪烁
while True:
l.value(1)
time.sleep(1)
l.value(0)
time.sleep(1)

将Pico联网

准备硬件:

  • 面包版
  • 公母跳线
  • Wifi模块 WSP01

步骤:

  • 先将下列依赖代码上传至pico,命名wifiat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import utime

class at(object):
def __init__(self,uart):
self.uart=uart
def sendCMD_waitResp(self,cmd, timeout=2000):
print("CMD: " + cmd)
self.uart.write(cmd.encode('utf-8'))
return self.waitResp(timeout)
def waitResp(self,timeout=2000):
prvMills = utime.ticks_ms()
resp = b""
while (utime.ticks_ms()-prvMills)<timeout:
if self.uart.any():
resp = b"".join([resp, self.uart.read(1)])
print(resp)
return resp
def sendCMD_waitRespLine(self,cmd, timeout=2000):
print("CMD: " + cmd)
self.uart.write(cmd.encode('utf-8'))
return self.waitRespLine(timeout)
def waitRespLine(self,timeout=2000):
cl=[]
prvMills = utime.ticks_ms()
while (utime.ticks_ms()-prvMills)<timeout:
if self.uart.any():
li=self.uart.readline()
print(li)
cl.append(li)
return cl
def restart(self):
self.sendCMD_waitResp('+++')
return self.sendCMD_waitResp("AT+RST\r\n")
def connect(self,name,password):
self.sendCMD_waitResp('AT+CWMODE=1\r\n')
cmd='AT+CWJAP="%s","%s"\r\n' % (name,password)
return self.sendCMD_waitResp(cmd)
def netinfo(self):
return self.sendCMD_waitResp("AT+CIFSR\r\n")
def info(self):
return self.sendCMD_waitResp("AT+GMR\r\n")
def restore(self):
return self.sendCMD_waitResp("AT+RESTORE\r\n")
def ping(self,doip):
return self.sendCMD_waitResp('AT+PING="%s"\r\n' % (doip,))
def disconnect(self):
return self.sendCMD_waitResp("AT+CWQAP\r\n")
def http(self,url,method="GET",ua=1,content=1,data='',timeout=6):
self.sendCMD_waitResp("+++")
typ,edx,domain,rel=url.split("/",3)
if "HTTP" == typ[0:-1].upper():
cs='AT+CIPSTART="TCP","%s",80\r\n' % (domain,)
else:
self.sendCMD_waitResp("AT+CIPSSLSIZE=4096\r\n")
cs='AT+CIPSTART="SSL","%s",443\r\n' % (domain,)
self.sendCMD_waitResp(cs)
self.sendCMD_waitResp('AT+CIPMODE=1\r\n')
self.sendCMD_waitResp('AT+CIPSEND\r\n')
if ua==1:
ua="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"
elif ua==2:
ua="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"
elif ua==3:
ua="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
elif ua==4:
ua="Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
if content==1:
content="text/html;charset=utf-8"
elif content==2:
content="text/xml"
elif content==3:
content="application/json"
elif content==4:
content="multipart/form-data"
elif content==5:
content="application/x-www-form-urlencoded"
c="%s %s HTTP/1.1\r\nHOST:%s \r\nContent-Type: %s\r\nUser-Agent:%s \r\n \n %s \r\n\r\n " % (method,url,domain,content,ua,data)
return self.sendCMD_waitRespLine(c,1000*timeout)

  • 连接pico与esp01
  • 连接电脑,此时esp01的红色led灯会常亮
  • 在Thonny的shell中输入代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 固件模块
from machine import UART
# 导入依赖wifiat.py
from wifiat import at
uart=UART(0,115200)
AT=at(uart)
# 连接wifi
AT.connect(“Wi-Fi名”,”Wi-Fi密码”)

# 查看信息
AT.info()


# 查看联网信息
AT.netinfo()

查看联网信息AT.netinfo()如下显示则连接成功

1
2
3
4
CMD: AT+CIFSR

b'WIFI CONNECTED\r\nWIFI GOT IP\r\n\r\nOAT+CIFSR\r\r\n+CIFSR:STAIP,"192.168.3.32"\r\n+CIFSR:STAMAC,"e8:db:84:95:94:33"\r\n\r\nOK\r\n'
b'WIFI CONNECTED\r\nWIFI GOT IP\r\n\r\nOAT+CIFSR\r\r\n+CIFSR:STAIP,"192.168.3.32"\r\n+CIFSR:STAMAC,"e8:db:84:95:94:33"\r\n\r\nOK\r\n'

GET请求

1
2
3
resp=AT.http(“http://网址/get”)
AT.http("http://httpbin.org/get")

POST请求

1
2
resp=AT.http(“http://网址/post”,mothod=“POST”,data='数据')
AT.http("http://httpbin.org/post",method="POST",data='{"a":1,"b":2}')

resp可返回响应列表


参考:邪恶的胖次菌

作者

Joyce Lau

发布于

2021-03-11

更新于

2021-03-18

许可协议