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()