Wemos D1 mini Micropython编程(Thonny)05——从openweather获取天气状况

Wemos D1 mini Micropython编程(Thonny)05——从openweather获取天气状况


前言

MicroPython编程从openweather获取天气状况。


一、材料

Wemos D1 mini一块、面包板一块。

二、准备

只需将电脑与Wemos D1 mini连接,Wemos D1 mini具有连接WiFi功能。
需要去openweather网站注册,获得自己的API Key。API Key是通过邮件发送给你的。

三、代码

MicroPython的标准库中含有访问网站的相关库,可以直接使用,代码如下(示例):

import network, urequests, ujson, machine
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('WiFi名称', 'WiFi密码') # 填写你自己的WiFi名称和密码
while not sta_if.isconnected():
    pass

res = urequests.get(               # openweather API 网址
    "https://api.openweathermap.org/data/2.5/weather?" +
    "q=" + "baicheng" + ",CN" +      # 指定城市与国别
    "&units=metric&lang=zh_tw&" +  # 使用摄氏温度
    "appid=" +  # 以下填写你在openweather注册的 API key
    "自己的API key")
j = ujson.loads(res.text);  # 从 JSON 转成字典
# print(j)  # 如果需要可以看具体的JSON内容

temp = j["main"]['temp']
hum = j["main"]['humidity']
weatherID = j["weather"][0]["id"]       # 天气状况代码
weatherDesc = j["weather"][0]["description"] # 天气状况

print("北京:")
print("当前温度:", str(temp),"℃")
print("当前湿度:", str(hum),"%")
print("天气状况:", weatherDesc )

四、效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值