Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

Micropython ESP32 S3获取网络诗词API显示中文句子


一、目的


        这一节我们接着上一节来学习如何使用合宙的ESP32 S3开发板来学习在SSD1306液晶屏上显示中文汉字。只不过,这一节我们是使用网络API来获取到json格式的字符串,然后解析出来,在SSD1306屏幕上进行显示


二、环境


        合宙ESP32 S3开发板 (MicroPython v1.18 on 2022-01-17)+ USB转type-C线  + SSD1306液晶屏I2C 4P接口 + Win10商业版


        环境同上一节哈。。。


三、示例


api_learn.py

from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
from ufont import BMFont
from fonts import urequests
import ujson
import time
import network
 
#液晶地址
addr = 0x3c
#创建I2C对象
i2c  = I2C(0,scl = Pin(12),sda = Pin(11),freq = 400_000)
#创建液晶对象
oled = SSD1306_I2C(128,64,i2c,addr)
#挂载字库
font = BMFont("fonts/unifont-14-12888-16.v3.bmf")
#扫描I2C器件地址并反馈
print("%#x"%i2c.scan()[0])  # 3c
 
# 网络连接函数
def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        font.text(oled,"connecting to network...",0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
        wlan.connect('WIFI名字', 'WIFI密码')
        while not wlan.isconnected():
            pass
    # 在oled上显示"network"
    font.text(oled,"network",0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
    # 在oled上显示IP地址
    font.text(oled,"{}".format(wlan.ifconfig()[0]),0,16,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
    # 在oled上显示子网掩码
    font.text(oled,"{}".format(wlan.ifconfig()[1]),0,32,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
    # 在oled上显示DNS
    font.text(oled,"{}".format(wlan.ifconfig()[2]),0,48,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
    time.sleep(5)
    oled.clear()  # 清屏
 
# 获取诗词API函数
def Poet():
    #https://api.xygeng.cn/one
    #https://v.api.aa1.cn/api/api-wenan-gaoxiao/index.php?aa1=json
    '''
    # API-1
    try:
        Text = urequests.get("https://api.xygeng.cn/one")
        Dic  = ujson.loads(Text.text)
        code = Dic["data"]["content"]
        
        font.text(oled,"%s"%(code),0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
        time.sleep(3)
    except:
        font.text(oled,"数据异常!",24,24,color=1,font_size=16,reverse=False,reverse=True,clear=False,show=True,auto_wrap=True)
    '''
    # API-2
    try:
        Text = urequests.get("https://v1.jinrishici.com/all.json")
        Dic  = ujson.loads(Text.text)
        code = Dic["content"]
        
        font.text(oled,"%s"%(code),0,0,color=1,font_size=16,reverse=False,reverse=False,clear=True,show=True,auto_wrap=True)
        time.sleep(5)
    except:
        font.text(oled,"数据异常!",24,24,color=1,font_size=16,reverse=False,reverse=True,clear=False,show=True,auto_wrap=True)
    
def main():
    do_connect()
    #font.text(oled,"welcome jack!欢迎杰克!",0,0,color=1,font_size=16,reverse=False,reverse=False,clear=False,show=True,auto_wrap=True)
 
    while True:
        Poet()
    
    
if __name__ == "__main__":
    main()

main.py

from api_learn import do_connect,Poet
 
def main():
    do_connect()
 
    while True:
        Poet()
        
if __name__ == "__main__":
    main()



————————————————

版权声明:本文为CSDN博主「魔都飘雪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/zhusongziye/article/details/129824104



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.011115074157715 seconds