虚谷号

 找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 388|回复: 0

当Yeelight遇上虚谷号#13 天气灯(根据天气情况控制LED)

[复制链接]

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
发表于 2019-3-20 10:21:42 | 显示全部楼层 |阅读模式
本帖最后由 linmiaoyan 于 2019-3-20 10:23 编辑

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)



回复

使用道具 举报

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

本版积分规则

虚谷号

GMT+8, 2024-3-29 04:18 , Processed in 0.046913 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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