大连林海 发表于 2016-4-10 20:19:48

树莓派教程之使用yeelink进行远程监控

本帖最后由 大连林海 于 2016-4-20 20:15 编辑



yeelink是一个非常好的云平台,可以用它进行远程监控,遥控,温度等数据获取。官网:http://www.yeelink.net/

首先注册一个yeelink账号。网址:http://www.yeelink.net/register
然后点击登录
回到首页后点击用户中心
这里的APIKEY先复制下来备用
点击设备开发然后创建一个新的设备,两个键都可以
写完所有项后保存。 http://imgcdn.geekfans.com/data/attachment/forum/201501/29/210527kbovbbz97jbbbhww.jpg.thumb.jpg
这时跳转到设备开发页,点击管理,
然后点击创建新传感器
这里类型选数据型。因为我们现在只是用来检测树莓派工作状态的。
所有填完点保存。
跳转后可以看见新的传感器。记下api地址备用。
我的早就弄好了,所以有数据。接下来该轮到树莓派了。
先安装requests
网上一大把我就不复制了。
新建文档yeelink.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 = 'http://api.yeelink.net/v1.1/device/xxxx/sensor/xxxx/datapoints这里换成你的'
# 用户密码, 指定上传编码为JSON格式
apiheaders = {'U-ApiKey': 'xxxxxxxxxxxxxx这里换成你的', '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(5*60)

if __name__ == '__main__':
    main()

把上面的apikey和api地址换成你的就行了。
运行sudo python yeelink.py这时可以看见正在不断获取CPU温度且上传,现在就可以在yeelink里看见上传的数据了



hnyzcj 发表于 2016-4-10 21:03:20


大连林海 发表于 2016-4-10 21:13:21

hnyzcj 发表于 2016-4-10 21:03


这是配套家具吗{:5_197:}

dsweiliang 发表于 2016-4-11 08:42:12

突然觉得有个远程打卡机就好了

大连林海 发表于 2016-4-11 14:00:39

dsweiliang 发表于 2016-4-11 08:42
突然觉得有个远程打卡机就好了

我觉得可以有

凌风清羽 发表于 2016-4-20 11:11:33

加载不出图片呀

大连林海 发表于 2016-4-20 17:39:24

凌风清羽 发表于 2016-4-20 11:11
加载不出图片呀

好尴尬 我回家再看看 最近怎么这么多为题:'(

凌风清羽 发表于 2016-4-20 21:34:27

好了,可以看了{:5_160:}
页: [1]
查看完整版本: 树莓派教程之使用yeelink进行远程监控