效果
效果如标题,没什么图片好展示ψ(._. )>
ESP-32 上电连接wifi,设置ntp服务器获取时间戳,再串口发送给pico
前言
项目基于micropython与Thonny编辑器
结合作者上一篇博客树莓派pico 驱动四位数码管 模拟时钟效果实现pico上电自动设置时间
开始
ESP-32 连接wifi
先判断是否是空状态(仅供电,WiFi未连接)还是调试状态(WiFi已连接)
其中report函数一方面打印消息,一方面通过串口传递给pico,并睡眠1s
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
report('[!]Connect wifi')
#sta_if.scan()
try:
sta_if.connect("wifi_name", "wifi_passwd")
except Exception as e:
report(e)
设置ntp时间服务器
micropython 的ntptime.settime()
似乎有点问题,有时会产生OSError: -202
错误
网上搜索找到了一篇GitHub上面的讨论但方法太繁杂,这里直接使用ntptime.time()
并循环尝试,直到正常输出
ntptime.NTP_DELTA = 3155644800 #时差
ntptime.host = 'ntp1.aliyun.com' #阿里云时间服务器
while True:
try:
report('trying pushing...')
report(ntptime.time())
except Exception as e:
report(e)
else:
report('[!]PUSH SUCESS')
break
UART 串口通讯
ESP-32上貌似只有一个UART(2)
方便调用,其他的都需要映射引脚,对此pico就友善得多
关于UART(id) 对应的tx,rx 引脚可以直接在网络上搜索,当然有一种更简单的方式:在Thonny的shell交互中查询
这里以pico进行演示(Thonny对ESP-32的支持似乎不是很好,动不动就device is busy
)
pico与ESP-32 设置好相同的比特率和奇偶校验(注意pico默认parity=0
,而ESP-32默认parity=None
)
u=machine.UART(2,baudrate=115200,parity=0)# 设置串口
u.init()
u.write(str(utime.time()).encode()) # 发送时间戳
#u.deinit()
release
ESP-32
import network
import utime
import machine
import ntptime
u=machine.UART(2,baudrate=115200,parity=0)# 设置串口
u.init()
def report(item):
utime.sleep(1)
u.write(str(item).encode())
print(item)
if ntptime.host!="ntp1.aliyun.com":
report('[!]Changing timezone')
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
report('[!]Connect wifi')
#sta_if.scan()
try:
sta_if.connect("@PHICOMM_B8", "jiale2004") #连接WiFi
except Exception as e:
report(e)
print(sta_if.ifconfig())
ntptime.NTP_DELTA = 3155644800 #时差
ntptime.host = 'ntp1.aliyun.com' #阿里云时间服务器
while True:
try:
report('trying pushing...')
report(ntptime.time())
except Exception as e:
report(e)
else:
report('[!]PUSH SUCESS')
break
#sta_if.disconnect()
report(utime.time(),utime.localtime())
u.write(str(utime.time()).encode())
#u.deinit()
print('over')
pico
import machine
import utime
p0=machine.Pin(8,machine.Pin.OUT)
p1=machine.Pin(22,machine.Pin.OUT)
p2=machine.Pin(21,machine.Pin.OUT)
p3=machine.Pin(18,machine.Pin.OUT)
p4=machine.Pin(11,machine.Pin.OUT)
p5=machine.Pin(26,machine.Pin.OUT)
p6=machine.Pin(20,machine.Pin.OUT)
p7=machine.Pin(12,machine.Pin.OUT)
p8=machine.Pin(9,machine.Pin.OUT)
p9=machine.Pin(19,machine.Pin.OUT)
p10=machine.Pin(10,machine.Pin.OUT)
p11=machine.Pin(13,machine.Pin.OUT)
p=[p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11]
p25=machine.Pin(25,machine.Pin.OUT)
def change_frame(t,n): #要设置的数字,设置的位置
utime.sleep(0.003)
if n==1:
ss=time_list[t][:]
ss.reverse()
frame=ss
else:
frame=time_list[t]
p[(n-1)%4].value(False)
p[4].value(fix if (n+1)%4//2 else True)
for nn in range(5,12):
p[nn].value(frame[nn-5])
if not n and not t:
p[0].value(False)
else:
p[n].value(True)
time_list=[
[False,False,False,True,False,False,False],#0
[True,False,True,True,True,True,False],#1
[False,True,False,False,False,True,False],#2
[True,False,False,False,False,True,False],#3
[True,False,True,False,True,False,False],#4
[True,False,False,False,False,False,True],#5
[False,False,False,False,False,False,True],#6
[True,False,False,True,True,True,False],#7
[False,False,False,False,False,False,False],#8
[True,False,False,False,False,False,False]]#9
fix=True
p25.value(True)
k=0
u=machine.UART(1,baudrate=115200,parity=0) # 串口通讯
u.init()
for i in range(100): #接收时间戳,超时跳过
t=u.read() if u.any() else b'None'
print("fentch",t)
if t and len(t)==9 and t.decode()[0]=="7":
print('[!]GET source',end='')
t=int(t.decode())
break
utime.sleep(1)
else:
t=utime.time()
#u.deinit()
print(t)
base=t
yo=utime.time()
offset=yo-base
while True:
if not k%75:#跳秒
k=1
fix=not fix
print('seconds dance')
time=utime.gmtime(utime.time()-offset)
#print(time)
change_frame(time[3]//10,0)
change_frame(time[3]%10,1)
change_frame(time[4]//10,2)
change_frame(time[4]%10,3)
k+=1
p25.value(fix)
后话
To do
- esp32 与手机通讯,获取要连接的WiFi与其密码
如有建议或优化思路,欢迎评论指正或私信
如果本篇文章对你有帮助,请不要吝啬你的点赞。