MicroPython动手做(33)——物联网之天气预报-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(33)——物联网之天气预报

[复制链接]
 楼主| 发表于 2020-6-20 09:58 | 显示全部楼层
07.jpg
 楼主| 发表于 2020-6-20 11:42 | 显示全部楼层
6、当地天气预报及六项生活指数

[mw_shl_code=arduino,true]#MicroPython动手做(33)——物联网之天气预报
#当地天气预报及六项生活指数

from mpython import *
import network
import json
import urequests

my_wifi = wifi()

my_wifi.connectWiFi("zh", "zy1567")

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json


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")
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"], " 度"]])), 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()[/mw_shl_code]
 楼主| 发表于 2020-6-20 11:44 | 显示全部楼层
mPython X 实验图形编程

08.jpg
 楼主| 发表于 2020-6-20 11:46 | 显示全部楼层
09.jpg
 楼主| 发表于 2020-6-20 12:58 | 显示全部楼层
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]
 楼主| 发表于 2020-6-20 13:08 | 显示全部楼层
mPython X 实验图形编程

10.jpg

11.jpg
 楼主| 发表于 2020-6-20 13:11 | 显示全部楼层
15.gif
 楼主| 发表于 2020-6-20 14:37 | 显示全部楼层
8、使用mindplus sever服务器获取天气预报
无需注册,不用密钥,不要设置,挺简单的一个办法

[mw_shl_code=arduino,true]#MicroPython动手做(33)——物联网之天气预报
#使用mindplus sever服务器获取天气预报

from mpython import *
import urequests
import network
import ntptime
import music
import json


brightness=9

weather_serveraddr = "http://server.mindplus.top"

weather_appid = "31982666"

weather_appsecret = "E6MtBcxQ"

def weather_getWeather(_weather, _city):
  if weather_serveraddr=="http://www.tianqiapi.com":
    nowResult = urequests.get(weather_serveraddr+"/api/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]
  else:
    nowResult = urequests.get(weather_serveraddr+"/api/weather/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]


my_wifi = wifi()
my_wifi.connectWiFi("zhz","zy156721")
while not (my_wifi.sta.isconnected()):
  pass
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(392, 50)
oled.fill(0)
oled.DispChar("福建 福州", 0, (1-1)*16, 1)
oled.show()
ntptime.settime(8, "ntp.ntsc.ac.cn")
oled.DispChar(weather_getWeather("wea", "101230101"), 0, (2-1)*16, 1)
oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101230101"))), 0, (3-1)*16, 1)
oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101230101"))), 0, (4-1)*16, 1)
oled.show()[/mw_shl_code]
 楼主| 发表于 2020-6-20 14:41 | 显示全部楼层
17.jpg
 楼主| 发表于 2020-6-20 14:45 | 显示全部楼层
Mind+  实验图形编程

16.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 01:04 , Processed in 0.120236 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表