标签:树莓派 b+
硬件平台: Raspberry Pi B+
软件平台:
首先我们要先解决requests库,当我们向YEELINK POST 消息的时候会用到 : r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))
安装easy_install:
<span style="line-height: 1.6em; font-family: Arial;">wget </span><a target=_blank href="http://peak.telecommunity.com/dist/ez_setup.py">http://peak.telecommunity.com/dist/ez_setup.py</a><span style="line-height: 1.6em; font-family: Arial;"> </span>
<span style="line-height: 1.6em; font-family: Arial;"> <span style="line-height: 1.6em; font-family: Arial;">python ez_setup.py</span></span>
<span style="line-height: 1.6em; font-family: Arial;"><span style="line-height: 1.6em; font-family: Arial;"> <span style="font-family:Courier New;">easy_install requests</span></span></span>
安装好之后 在运行python,如果没报错 那就说明安装成功了
python
import requests
一家中国的创业公司Yeelink,物联网平台,也正在利用无线网络、开源硬件和软件,当然还有智能手机和App来做到这一点。
申请帐号很简单 在官网上偶详细的说明文档:http://www.yeelink.net/
重要的是我们需要添加两个设备,一个是CPU温度另一个是GPU温度,在此我们要获取两个重要的东西,也就是写程序消息POST的目的地
1 ‘ApiKey‘ 在“我的账户设置”中查看
2 URL: http://api.yeelink.net/v1.0/device/13926/sensor/23121/datapoints
有了YEELINK平台之后,我们做的工作就是需要在树莓派B+上,定时的将温度数据传到YEELINK上,新建pi_temp.py的python文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import time
import commands
def main():
# fileRecord = open("result.txt", "w")
# fileRecord.write("connect to yeelink\n");
# fileRecord.close()
while True:
# 打开文件
apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'}
gpu = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
gpu = float(gpu)
#print('gpu value:%.2f' % gpu)
# GPU设备URI
apiurl_gpu = 'http://api.yeelink.net/v1.0/device/13926/sensor/23125/datapoints'
#YEELINK 用户密码, 指定上传编码为JSON格式i
#apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'}
payload_gpu = {'value': gpu}
r = requests.post(apiurl_gpu, headers=apiheaders, data=json.dumps(payload_gpu))
file = open("/sys/class/thermal/thermal_zone0/temp")
# 读取结果,并转换为浮点数
cpu = float(file.read()) / 1000
# 关闭文件
file.close()
#print('cpu value:%.2f' % cpu)
# CPU设备URI
apiurl_cpu = 'http://api.yeelink.net/v1.0/device/13926/sensor/23121/datapoints'
#YEELINK 用户密码, 指定上传编码为JSON格式
#apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'}
# 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123}
payload_cpu = {'value': cpu}
#发送请求
r = requests.post(apiurl_cpu, headers=apiheaders, data=json.dumps(payload_cpu))
# 向控制台打印结果
# 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(1*60)
if __name__ == '__main__':
main()
编写完成之后,只中断中直接运行
python pi_temp.py
树莓派学习笔记——定时向yeelink上传树莓派CPU温度 : http://blog.csdn.net/xukai871105/article/details/38349519
python requests的安装与简单运用 : http://www.zhidaow.com/post/python-requests-install-and-brief-introduction
树莓派+温度传感器实现室内温度监控 :http://shumeipai.nxez.com/2013/10/03/raspberry-pi-temperature-sensor-monitors.html
获取树莓派的CPU和GPU温度(Python) : http://shumeipai.nxez.com/2014/02/20/get-raspberry-pi-cpu-and-gpu-temperature-python.html
ROS ZYNQ移植 (安装easy_install) :http://blog.csdn.net/xiabodan/article/details/37565261
Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度
标签:树莓派 b+
原文地址:http://blog.csdn.net/xiabodan/article/details/39084877