温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

树莓派怎么用Python实现yeelink平台

发布时间:2021-11-20 09:26:18 来源:亿速云 阅读:127 作者:小新 栏目:互联网科技

这篇文章给大家分享的是有关树莓派怎么用Python实现yeelink平台的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

首先先在yeelink平台上注册,获得自己的APIKEY,

创建设备及设备上传感器,读取传感器的apiurl。

例子1:上传树莓派温度数据到yeelink -> yeelink_temp.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import time

def main():
    fileRecord = open("result.txt", "w")
    fileRecord.write("connect to yeelink\n");
    fileRecord.close()
    while True:
        # 打开文件
        file = open("/sys/class/thermal/thermal_zone0/temp")
        # 读取结果,并转换为浮点数
        temp = float(file.read()) / 1000
        # 关闭文件
        file.close()

        # 设备URI,在创建的温度传感器处查看自己的传感器apiurl替换下面的路径
        apiurl = 'http://api.yeelink.net/v1.0/device/2342/sensor/2555/datapoints'
        # 用户密码, 指定上传编码为JSON格式
        apiheaders = {'U-ApiKey': 'f07f2b260a6635*****b4a3a*******5', 'content-type': 'application/json'}
        # 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123}
        payload = {'value': temp}
        #发送请求
        r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))

        # 向控制台打印结果
        fileRecord = open("result.txt", "a")
        strTime = time.strftime('%Y-%m-%d:%H-%M-%S',time.localtime(time.time()))
        fileRecord.writelines(strTime + "\n")
        strTemp = "temp : %.1f" %temp + "\n"
        fileRecord.writelines(strTemp)
        fileRecord.writelines(str(r.status_code) + "\n")
        fileRecord.close()

        time.sleep(2*60)

if __name__ == '__main__':
    main()

例子2:yeelink创建开关控制led -> yeelink_led_ctl.py

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
import requests  
import time  
import driver_gpio_led

# 设备URI, 在创建的温度传感器处查看自己的传感器apiurl替换下面的路径
apiurl = 'http://api.yeelink.net/v1.0/device/2332/sensor/2578/datapoints'  
# 用户密码  
apiheaders = {'U-ApiKey': 'f07f2b260a6635*****b4a3a*******5'} 
 
led = driver_gpio_led.gpio_led(7)  
while True:  
  #发送请求  
  r = requests.get(apiurl,headers=apiheaders)  
  # 打印响应内容  
  #print(r.text)  
  # 转换为字典类型 请注意 2.7.4版本使用r.json()  
  led_state = r.json()
  # {'value':x} x=1打开状态,x=0关闭状态  
  if led_state['value'] == 1:  
    print("led on")  
    led.gpio_high() 
  else:  
    print("led off")  
    led.gpio_low()  
  # 延时5S  
  time.sleep(5)    
led.clean()

感谢各位的阅读!关于“树莓派怎么用Python实现yeelink平台”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI