13天气灯(根据天气情况控制LED)
根据网络上获取的当地天气,显示不同颜色的灯光色温。
阴天:#B4B4B4
雨天:#8080FF
多云:#00FFFF
晴天:#FFFFFF
其他(雪):#FFFF00
睡觉前(色温):bulb.set_color_temp(2000)
早上(色温):bulb.set_color_temp(6500)
白天(色温):bulb.set_color_temp(4700)
其他说明:使用requests库
国家气象局提供的天气预报接口,接口地址:
http://www.weather.com.cn/data/sk/101210701.html
http://www.weather.com.cn/data/cityinfo/101210701.html
设置不同的色温
bulb = Bulb("192.168.31.39")
color=[2000,4700,6500]
r = requests.get(url)
bulb.set_color_temp(color[0])
参考代码:
from yeelight import Bulb
import requests
import json
bulb = Bulb("192.168.199.142")
url = "http://www.weather.com.cn/data/cityinfo/101210701.html" # 天气网,温州
r = requests.get(url)
json_response = r.content.decode() # 获取r的文本 就是一个json字符串
# 将json字符串转换成dic字典对象
dict_json = json.loads(json_response)
weather=dict_json["weatherinfo"]["weather"]
print(weather)
if weather.find("晴")!=-1:
rgb='#FFFFFF'
elif weather.find("云")!=-1:
rgb='#00FFFF'
elif weather.find("阴")!=-1:
rgb='#B4B4B4'
elif weather.find("雨")!=-1:
rgb='#8080FF'
else:
rgb='#FFFF00'
red=int(rgb[1:3],16)
green=int(rgb[3:5],16)
blue=int(rgb[5:7],16)
bulb.turn_on()
bulb.set_rgb(red,green,blue)