Raspberry pi connect temperature and humidity to onenet (移动云平台)
工具
树莓派连接温度传感器
- #!/usr/bin/python
- import RPi.GPIO as GPIO
- import time
- channel =4
- data = []
- j = 0
- GPIO.setmode(GPIO.BCM)
- time.sleep(1)
- GPIO.setup(channel, GPIO.OUT)
- GPIO.output(channel, GPIO.LOW)
- time.sleep(0.02)
- GPIO.output(channel, GPIO.HIGH)
- GPIO.setup(channel, GPIO.IN)
- while GPIO.input(channel) == GPIO.LOW:
- continue
- while GPIO.input(channel) == GPIO.HIGH:
- continue
- while j < 40:
- k = 0
- while GPIO.input(channel) == GPIO.LOW:
- continue
- while GPIO.input(channel) == GPIO.HIGH:
- k += 1
- if k > 100:
- break
- if k < 8:
- data.append(0)
- else:
- data.append(1)
- j += 1
- print "sensor is working."
- print data
- humidity_bit = data[0:8]
- humidity_point_bit = data[8:16]
- temperature_bit = data[16:24]
- temperature_point_bit = data[24:32]
- check_bit = data[32:40]
- humidity = 0
- humidity_point = 0
- temperature = 0
- temperature_point = 0
- check = 0
- for i in range(8):
- humidity += humidity_bit[i] * 2 ** (7-i)
- humidity_point += humidity_point_bit[i] * 2 ** (7-i)
- temperature += temperature_bit[i] * 2 ** (7-i)
- temperature_point += temperature_point_bit[i] * 2 ** (7-i)
- check += check_bit[i] * 2 ** (7-i)
- tmp = humidity + humidity_point + temperature + temperature_point
- if check == tmp:
- print "temperature :", temperature, "*C, humidity :", humidity, "%"
- else:
- print "wrong"
- print "temperature :", temperature, "*C, humidity :", humidity, "% check :", check, ", tmp :", tmp
- #从这里开始是第二部分
- mytemp = '%f' %temperature
- myhumi = '%f' %humidity
- tmp_output = open('/home/pi/pi/test/dht11/tmp_data.txt', 'w')
- hud_output = open('/home/pi/pi/test/dht11/hum_data.txt', 'w')
- tmp_output.write(mytemp)
- hud_output.write(myhumi)
- tmp_output.close
- hud_output.close
- GPIO.cleanup()
#!/usr/bin/python
import RPi.GPIO as GPIO
import time channel =4
data = []
j = 0 GPIO.setmode(GPIO.BCM) time.sleep(1) GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)
GPIO.setup(channel, GPIO.IN) while GPIO.input(channel) == GPIO.LOW:
continue
while GPIO.input(channel) == GPIO.HIGH:
continue while j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continue
while GPIO.input(channel) == GPIO.HIGH:
k += 1
if k > 100:
break
if k < 8:
data.append(0)
else:
data.append(1) j += 1 print "sensor is working."
print data humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40] humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0 for i in range(8):
humidity += humidity_bit[i] * 2 ** (7-i)
humidity_point += humidity_point_bit[i] * 2 ** (7-i)
temperature += temperature_bit[i] * 2 ** (7-i)
temperature_point += temperature_point_bit[i] * 2 ** (7-i)
check += check_bit[i] * 2 ** (7-i) tmp = humidity + humidity_point + temperature + temperature_point if check == tmp:
print "temperature :", temperature, "*C, humidity :", humidity, "%"
else:
print "wrong"
print "temperature :", temperature, "*C, humidity :", humidity, "% check :", check, ", tmp :", tmp
#从这里开始是第二部分
mytemp = '%f' %temperature
myhumi = '%f' %humidity tmp_output = open('/home/pi/pi/test/dht11/tmp_data.txt', 'w')
hud_output = open('/home/pi/pi/test/dht11/hum_data.txt', 'w') tmp_output.write(mytemp)
hud_output.write(myhumi) tmp_output.close
hud_output.close
GPIO.cleanup()
运行代码后会出现两个文件分别来保存温度和湿度,如下图:
这样一来温湿度的数据就保存下来了,然后就是读取并上传了
上传数据至云端
通过代码上传数据
- <span style="font-size: 18px;">import urllib2
- import json
- import time
- import datetime
- APIKEY = '你的APIKey'
- def http_put():
- file = open("/home/pi/pi/test/dht11/tmp_data.txt")
- temperature= float(file.read())
- CurTime = datetime.datetime.now()
- url='http://api.heclouds.com/devices/你的设备ID/datapoints'
- values={'datastreams':[{"id":"temp","datapoints":[{"at":CurTime.isoformat(),"value":temperature}]}]}
- print "the time is: %s" %CurTime.isoformat()
- print "The upload temperature value is: %.3f" %temperature
- jdata = json.dumps(values)
- print jdata
- request = urllib2.Request(url, jdata)
- request.add_header('api-key', APIKEY)
- request.get_method = lambda:'POST'
- request = urllib2.urlopen(request)
- return request.read()
- while True:
- time.sleep(5)
- resp = http_put()
- print "OneNET result:\n %s" %resp
- time.sleep(5)</span>
import urllib2
import json
import time
import datetime APIKEY = '你的APIKey' def http_put():
file = open("/home/pi/pi/test/dht11/tmp_data.txt")
temperature= float(file.read())
CurTime = datetime.datetime.now()
url='http://api.heclouds.com/devices/你的设备ID/datapoints'
values={'datastreams':[{"id":"temp","datapoints":[{"at":CurTime.isoformat(),"value":temperature}]}]} print "the time is: %s" %CurTime.isoformat()
print "The upload temperature value is: %.3f" %temperature jdata = json.dumps(values)
print jdata
request = urllib2.Request(url, jdata)
request.add_header('api-key', APIKEY)
request.get_method = lambda:'POST'
request = urllib2.urlopen(request)
return request.read() while True:
time.sleep(5)
resp = http_put()
print "OneNET result:\n %s" %resp
time.sleep(5)
上传湿度数据代码:
- import urllib2
- import json
- import time
- import datetime
- APIKEY = '你的APIKey'
- def http_put():
- file = open("/home/pi/pi/test/dht11/hum_data.txt")
- humidity= float(file.read())
- CurTime = datetime.datetime.now()
- url='http://api.heclouds.com/devices/11302038/datapoints'
- values={'datastreams':[{"id":"hum","datapoints":[{"at":CurTime.isoformat(),"value":humidity}]}]}
- print "the time is: %s" %CurTime.isoformat()
- print "The upload humidity value is: %.3f" %humidity
- jdata = json.dumps(values)
- print jdata
- request = urllib2.Request(url, jdata)
- request.add_header('api-key', APIKEY)
- request.get_method = lambda:'POST'
- request = urllib2.urlopen(request)
- return request.read()
- time.sleep(5)
- resp = http_put()
- print "OneNET result:\n %s" %resp
- file.closes
import urllib2
import json
import time
import datetime APIKEY = '你的APIKey' def http_put():
file = open("/home/pi/pi/test/dht11/hum_data.txt")
humidity= float(file.read())
CurTime = datetime.datetime.now()
url='http://api.heclouds.com/devices/11302038/datapoints'
values={'datastreams':[{"id":"hum","datapoints":[{"at":CurTime.isoformat(),"value":humidity}]}]} print "the time is: %s" %CurTime.isoformat()
print "The upload humidity value is: %.3f" %humidity jdata = json.dumps(values)
print jdata
request = urllib2.Request(url, jdata)
request.add_header('api-key', APIKEY)
request.get_method = lambda:'POST'
request = urllib2.urlopen(request)
return request.read() time.sleep(5)
resp = http_put()
print "OneNET result:\n %s" %resp
file.closes
数据流问题
自动执行代码并上传数据
- sudo crontab -e
sudo crontab -e
选择nano,也就是2,然后在最后几行添加这么几句话
- */1 * * * * python /home/pi/pi/test/dht11/transfer_tmp.py
- */1 * * * * python /home/pi/pi/test/dht11/transfer_hum.py
- */1 * * * * python /home/pi/pi/test/dht11/cpdht11.py
*/1 * * * * python /home/pi/pi/test/dht11/transfer_tmp.py
*/1 * * * * python /home/pi/pi/test/dht11/transfer_hum.py
*/1 * * * * python /home/pi/pi/test/dht11/cpdht11.py
Raspberry pi connect temperature and humidity to onenet (移动云平台)的更多相关文章
- SITP & Raspberry Pi
系统安装 系统选择树莓派论坛提供的下载地址 Download 我选择了其中的SSH-2017-01-11-raspbian-jessie.zip[良心推荐] 选择一个大于8G的内存卡,利用 Win32 ...
- (0)开始 Raspberry Pi 项目前需要知道的 10 件事
https://www.digikey.cn/zh/articles/techzone/2017/feb/10-things-to-know-before-starting-a-raspberry-p ...
- RASPBERRY PI 外设学习资源
参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi Get st ...
- 常用Raspberry Pi周边传感器的使用教程
在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如,加入一个超声波测距的模块来让你的Raspberry Pi具备测距功能,加入一个测温模块以实现测 ...
- 常用Raspberry Pi周边传感器的使用教程(转)
转:http://bbs.xiaomi.cn/thread-7797152-1-1.html 在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如, ...
- 用Raspberry Pi搭建Azure IOT解决方案
Raspberry Pi是一款基于Linux的单板机电脑.它由英国的树莓派基金会所开发,目的是以低价硬件及自由软件刺激在学校的基本计算机科学教育.树莓派配备一枚博通(Broadcom)出产的ARM架构 ...
- A new comer playing with Raspberry Pi 3B
there are some things to do for raspberry pi 3b for the first time: 1, connect pi with monitor/KB/mo ...
- Raspberry Pi UART with PySerial
参考:http://programmingadvent.blogspot.hk/2012/12/raspberry-pi-uart-with-pyserial.html Raspberry Pi UA ...
- Raspberry Pi Resources-Using the UART
参考:RPi Serial Connection 本文来自:http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port ...
随机推荐
- vue安装element-ui和px2rem的细节
1.按需引入element-ui vue脚手架搭建完成之后,可以到element-ui官网进行npm 安装: npm i element-ui -S 如果是完整引入可以按照官网一步一步做即可完成:这里 ...
- 少侠学代码系列(一)->JS起源
少侠:喂,有人吗?赶紧出来接客了,有没有人啊 帅气的我:来了来了,少侠有何吩咐? 少侠:把你们店里的秘籍呈上来我要学JS 帅气的我:少侠,别这样,我们秘籍是不外传的,祖上传下来的规矩,传人妖不传男女. ...
- 谷歌浏览器扩展程序manifest.json参数详解
{ // Required "manifest_version": 2, // manifest编写规范版本,目前主流2 "name": "My Ex ...
- Java实践:一个简易的http server和client的java源码学习和总结。
一.基本思路: 1.服务器端通过socket(), 监听在TCP 8080端口,等待客户端来连接. 2.服务器端解析客户端的HTTP请求中的URI值,把本地的目录下指定文件通过java的读取文件的方式 ...
- SpringBoot Web学习笔记
一.资源的访问: 情形一.所有的 /webjars/** 都会去 classpath:/META_INFO/resource/webjars/ 下找资源: webjars:以jar包的方式引入静态 ...
- Spark资源调度和任务调度
一.资源调度&任务调度 1.启动集群后,Worker节点会周期性的[心跳]向Master节点汇报资源情况,Master掌握集群资源情况. 2.当Spark提交一个Application后,根据 ...
- java实现Windows记事本
给大家分享下我之前的作品: 源文件下载: 链接:https://pan.baidu.com/s/1N45VsS9aVgmvhvYjRLxBrA 提取码:b9fg 源码: JF_Notpad.jav ...
- Linux内核的冷热缓存
缓存为什么会有冷热? 究其原因,是因为对于内存的访问,可能是CPU发起的,也可以是DMA设备发起的. 如果是CPU发起的,在CPU的硬件缓存中,就会保存相应的页内容.如果这个页本来没有存在于硬件缓存中 ...
- RH2288V3服务器 硬件安装以及更换硬件
滑道类型:L型滑道.可伸缩滑道.报轨 L型滑道:只适用于华为机柜. 准备工具:防静电带或者防静电手套.螺丝刀.浮动螺母安装条. 安装服务器步骤: 1.安装L型滑道 2.安装浮动螺母 3.安装服务 ...
- HybridStart混合应用开发框架
转自我的博客,原文地址:http://refined-x.com/2017/06/26/%E5%9F%BA%E4%BA%8EAPICloud%E7%9A%84%E6%B7%B7%E5%90%88%E5 ...