7、按下AB键查询今明二天的天气和生活指数
#MicroPython动手做(33)——物联网之天气预报
#按下AB键查询今明二天的天气和生活指数
from mpython import *
import network
import json
import urequests
import time
import music
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
def on_button_a_down(_):
time.sleep_ms(10)
if button_a.value() == 1: return
music.play('G5:1')
oled.fill(0)
oled.DispChar((''.join([str(x) for x in [w1["results"][0]["location"]["name"], "今天", w1["results"][0]["daily"][0]["text_day"], "", w1["results"][0]["daily"][0]["low"], " - ", w1["results"][0]["daily"][0]["high"], " 度", w1["results"][0]["daily"][0]["high"]]])), 0, 0, 1)
oled.DispChar((str("穿衣指数 : ") + str(w2["results"][0]["suggestion"]["dressing"]["brief"])), 0, 16, 1)
oled.DispChar((str("运动指数 : ") + str(w2["results"][0]["suggestion"]["sport"]["brief"])), 0, 32, 1)
oled.DispChar((str("紫外线指数 : ") + str(w2["results"][0]["suggestion"]["uv"]["brief"])), 0, 48, 1)
oled.show()
def on_button_b_down(_):
time.sleep_ms(10)
if button_b.value() == 1: return
music.play('B5:1')
oled.fill(0)
oled.DispChar((''.join([str(x) for x in [w1["results"][0]["location"]["name"], "明天", w1["results"][0]["daily"][1]["text_day"], " ", w1["results"][0]["daily"][1]["low"], " - ", w1["results"][0]["daily"][1]["high"], " 度", w1["results"][0]["daily"][1]["high"]]])), 0, 0, 1)
oled.DispChar((str("穿衣指数 : ") + str(w2["results"][0]["suggestion"]["dressing"]["brief"])), 0, 16, 1)
oled.DispChar((str("感冒指数 : ") + str(w2["results"][0]["suggestion"]["flu"]["brief"])), 0, 32, 1)
oled.DispChar((str("旅游指数 : ") + str(w2["results"][0]["suggestion"]["travel"]["brief"])), 0, 48, 1)
oled.show()
def get_seni_weather(_url, _location):
_url = _url + "&location=" + _location.replace(" ", "%20")
response = urequests.get(_url)
json = response.json()
response.close()
return json
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
w1 = get_seni_weather("https://api.seniverse.com/v3/weather/daily.json?key=SSY9pi-U4QH-ZDrf", "ip")
w2 = get_seni_weather("https://api.seniverse.com/v3/life/suggestion.json?key=SSY9pi-U4QH-ZDrf", "ip")
rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
music.play('E5:1')
oled.fill(0)
oled.DispChar("按下AB键查询天气预报", 0, 16, 1)
oled.show()
|