简介
合宙esp32C3,128*64 I2C oled,硬件i2c,将下面两个py文件放入esp32.
ssd1306.py是我优化后的,为了避免错误,使用我提供的ssd1306驱动
只支持128*64的I2C oled
代码
main.py
import network
import urequests
import ujson
import time
from machine import RTC,Pin,I2C,Timer
from ssd1306 import SSD1306_I2C
#实例化oled屏
i2c=I2C(0,scl=Pin(5),sda=Pin(4))
oled=SSD1306_I2C(i2c)
#联网设置
url='http://worldtimeapi.org/api/timezone/Asia/Shanghai'
rtc=RTC()
wifi=network.WLAN(network.STA_IF)
wifi.active(True)
#连接wifi,有一定几率连接失败
while not wifi.isconnected():
wifi.connect('szsb','55555555')
oled.fill(0)
oled.text('connecting...',0,0)
oled.show()
#更新显示连接wifi信息
oled.fill(0)
oled.text('connect to szsb',0,0)
oled.show()
#获取网络时钟
response=urequests.get(url)
if response.status_code==200:
parsed=ujson.loads(response.text)#用json处理获取的文本
datetime_str=str(parsed['datetime'])
year=int(datetime_str[0:4])
month=int(datetime_str[5:7])
day=int(datetime_str[8:10])
hour=int(datetime_str[11:13])
minute=int(datetime_str[14:16])
second=int(datetime_str[17:19])
msecond=int(datetime_str[20:26])
#将(年,月,日,星期,小时,分钟,秒,毫秒)赋值给内部实时时钟RTC
rtc.datetime((year,month,day,parsed['day_of_week'],hour,minute,second,msecond))
#定时器回调函数,从已经初始化的RTC读取时间信息并显示到oled屏
def gain_time():
date_str='{:4}/{:02}/{:02}'.format(rtc.datetime()[0],rtc.datetime()[1],rtc.datetime()[2])
time_str='{:02}:{:02}:{:02}'.format(rt

最低0.47元/天 解锁文章
2300

被折叠的 条评论
为什么被折叠?



