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 ...
随机推荐
- Centos7.3安装和配置Mysql5.7
主要转自这篇文章:https://www.cnblogs.com/wishwzp/p/7113403.html 这篇文章已经讲的很详细,亲测可用,对于基本不懂linux的小白应该也能看得懂.只是没有修 ...
- 【Dojo 1.x】笔记2 使用服务器环境及使用模块
又开坑了.上次静态html页面完成本地module的引用,算是成功了,但是并不知道是怎么运作的,没关系慢慢来. 我用的环境是VSCode,这次因为官方说要在服务器环境下运行,所以就用上了VSCode的 ...
- 键盘上的"整蛊专家",如何防止短信轰炸机
"短信轰炸机",是别人通过爬虫或者其他抓取手段在网路上收集那些公司平台短信业务接口的一个集成程序,可能只需要输入一个手机号,对方一整天都会收到各大平台的注册或提醒短信,就是手机在那 ...
- Arcgis瓦片--数据获取
Arcgis的二维地图瓦片有两种获取方式 1.在Arcmap中对配置好的地图进行切图,生成对应瓦片 2.使用第三方的地图下载器,直接下载,导出成arcgis瓦片格式即可使用. 备注:这里主要介绍第二种 ...
- QT 启动shell脚本
1.QProcess *p = new QProcess(this); 2.QString str = qApp->applicationDirPath() + "/update.sh ...
- DAS、SAN和NAS三种存储方式
DAS存储 DAS存储在我们生活中是非常常见的,尤其是在中小企业应用中,DAS是最主要的应用模式,存储系统被直连到应用的服务器中,在中小企业中,许多的数据应用是必须安装在直连的DAS存储器上. DAS ...
- Linux 中磁盘阵列RAID10损坏以及修复
在Linux 中磁盘阵列RAID10配置中我们已经正确配置了RAID10 ,一般来说在RAID10中最多允许50%的磁盘损毁,当然除了,同一磁盘RAID1中的硬盘设备全部损毁. 这次我们讨论一下:假设 ...
- ORA-00471: DBWR process terminated with error案例
每年年底,系统管理员都要组织一次容灾方案的测试.演练.会在一个与生产环境网络隔离的DR环境中,启动各个"生产环境服务器",然后让各路人员参与其中测试.演练容灾方案是否可靠.这次演练 ...
- 文件操作命令(rename)
Rename 命令: // 描述: 重命名文件或目录. // 语法: rename [<Drive>:][<Path>]<FileName1> <FileNa ...
- python之单元测试_生成测试报告
(1)HTMLTestRunner.py的下载路径:https://pan.baidu.com/s/1Yk2E8d8bIo5_rmpussOE9Q 提取码:0jae (2)HTMLTestRunner ...