esp8266+MicroPython+0.96oled显示一周天气

本项目介绍如何使用ESP8266模块配合SSD1306 OLED显示屏实现从网络获取并显示郑州市未来五天的天气预报,包括日期、天气状况、温度范围及湿度等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.连接:

SCL—D1(GPIO5)
SDA—D2(GPIO4)

2.代码

main.py

import network
import urequests
import ujson
import utime
import ssd1306
import machine
from machine import RTC, I2C, Pin

# user data
# wifi router name
ssid = "无线名"
# wifi router password
pw = "无线密码"

# initialization

# SSD1306 OLED display
print("Connecting to wifi...")
oled = ssd1306.SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4)))
oled.fill(0)
oled.text("Connecting", 0, 5)
oled.text(" to wifi...", 0, 15)
oled.show()

# wifi connection
wifi = network.WLAN(network.STA_IF) # station mode
wifi.active(True)
wifi.connect(ssid, pw)

# wait for connection
while not wifi.isconnected():
    pass

# wifi connected
print("IP: " + str(wifi.ifconfig()[0]) + "\n")
oled.text("Connected. IP: ", 0, 35)
oled.text(" " + str(wifi.ifconfig()[0]), 0, 45)
oled.show()

url = 'https://api.seniverse.com/v3/weather/daily.json?key=你的私钥&location=zhengzhou&language=en&unit=c&start=0&days=5'
oled.fill(0)
oled.show()

def weather():
    while True:
        result = urequests.get(url)
        if not wifi.isconnected():
            machine.reset()
        j = ujson.loads(result.text)
        for i in range(5):
            city=j['results'][0]['location']['name']
            oled.text(city+" weather", 0, 0)

            date=j['results'][0]['daily'][i]['date']
            oled.text("Date "+date, 0, 9)

            tianqi='D/N:'+j['results'][0]['daily'][i]['text_day']+'/'+j['results'][0]['daily'][i]['text_night']
            oled.text(tianqi, 0, 20)

            high=j['results'][0]['daily'][i]['high']+'  deg'
            low=j['results'][0]['daily'][i]['low']+'  deg'

            oled.text('Low:'+low, 0, 29)
            oled.text('High:'+high, 0, 37)

            shidu=j['results'][0]['daily'][i]["humidity"]
            oled.text('Humidity:'+shidu+'%', 0, 50)
            oled.show()
            utime.sleep(12)
            oled.fill(0)
            oled.show()
        result.close()
weather()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值