超图解python物联网_MicroPython动手做(33)——物联网之天气预报

该博客展示了如何利用MicroPython编程,通过按下AB键查询当前位置今明两天的天气情况及生活指数,如穿衣、运动和紫外线指数。示例中使用了seniverse API获取数据,并在 OLED 屏幕上显示。
摘要由CSDN通过智能技术生成

7、按下AB键查询今明二天的天气和生活指数

[mw_shl_code=arduino,true]#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()[/mw_shl_code]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值