Table of contents
DHT11 realizes local temperature and humidity monitoring
Upload temperature and humidity data to ONENET
foreword
This article is divided into parts to upload temperature and humidity to the OneNet cloud platform, local data monitoring, and data uploading
Create a new folder containing DHT11.py, ONENET.py, tmp_data.txt, hum_data.txt
DHT11 realizes local temperature and humidity monitoring
DHT11.py
import RPi.GPIO as GPIO
import time
tmp=[] # 用来存放读取到的数据
DHT11_pin = 23 # 使用BCM编号
a,b=0,0
def delay_us(t): # 微秒级延时函数
start,end=0,0 # 声明变量
start=time.time() # 记录开始时间
t=(t-3)/1000000 # 将输入t的单位转换为秒,-3是时间补偿
while end-start<t: # 循环至时间差值大于或等于设定值时
end=time.time() # 记录结束时间
def DHT11():
GPIO.setup(DHT11_pin, GPIO.OUT) # 设置GPIO口为输出模式
GPIO.output(DHT11_pin,GPIO.HIGH) # 设置GPIO输出高电平
delay_us(10*1000) # 延时10毫秒
GPIO.output(DHT11_pin,GPIO.LOW) # 设置GPIO输出低电平
delay_us(25*1000) # 延时25毫秒
GPIO.output(DHT11_pin,GPIO.HIGH) # 设置GPIO输出高电平
GPIO.setup(DHT11_pin, GPIO.IN) # 设置GPIO口为输入模式
a=time.time() # 记录循环开始时间
while GPIO.input(DHT11_pin): # 一直循环至输入为低电平
b=time.time() # 记录结束时间
if (b-a)>0.1: # 判断循环时间是否超过0.1秒,避免程序进入死循环卡死
break # 跳出循环
a=time.time()
while GPIO.input(DHT11_pin)==0: # 一直循环至输入为高电平
b=time.time()
if (b-a)>0.1:
break
a=time.time()
while GPIO.input(DHT11_pin): # 一直循环至输入为低电平
b=time.time()
if (b-a)>=0.1:
break
for i in range(40): # 循环40次,接收温湿度数据
a=time.time()
while GPIO.input(DHT11_pin)==0: #一直循环至输入为高电平
b=time.time()
if (b-a)>0.1:
break
delay_us(28) # 延时28微秒
if GPIO.input(DHT11_pin): # 超过28微秒后判断是否还处于高电平
tmp.append(1) # 记录接收到的bit为1
a=time.time()
while GPIO.input(DHT11_pin): # 一直循环至输入为低电平
b=time.time()
if (b-a)>0.1:
break
else:
tmp.append(0) # 记录接收到的bit为0
####################################################################
#while True: #这一行注释打开表示循环监测温湿度
#后面会讲到脚本Shell的时候,使用的是将这一行注释掉
#if __name__ == '__main__': #这一行注释打开表示监测1次温湿度
####################################################################
GPIO.setmode(GPIO.BCM) # 设置为BCM编号模式
GPIO.setwarnings(False)
del tmp[0:] # 删除列表
time.sleep(1) # 延时1秒
DHT11()
humidity_bit=tmp[0:8] # 分隔列表,第0到7位是湿度整数数据
humidity_point_bit=tmp[8:16]# 湿度小数
temperature_bit=tmp[16:24] # 温度整数
temperature_point_bit=tmp[24:32] # 温度小数
check_bit=tmp[32:40] # 校验数据
humidity_int=0
humidity_point=0
temperature_int=0
temperature_point=0
check=0
for i in range(8): # 二进制转换为十进制
humidity_int+=humidity_bit[i]*2**(7-i)
humidity_point+=humidity_point_bit[i]*2**(7-i)
temperature_int+=temperature_bit[i]*2**(7-i)
temperature_point+=temperature_point_bit[i]*2**(7-i)
check+=check_bit[i]*2**(7-i)
humidity=humidity_int+humidity_point/10
temperature=temperature_int+temperature_point/10
check_tmp=humidity_int+humidity_point+temperature_int+temperature_point
if check==check_tmp and temperature!=0 and temperature!=0: # 判断数据是否正常
#温湿度格式转换
mytemp = '%f' %temperature
myhumi = '%f' %humidity
print("Temperature is ", temperature,"C\nHumidity is ",humidity,"%")# 打印温湿度数据
#温湿度数据分别放到两个txt文件下,文件路径与py文件路径一致,或者在下面的代码中添加路径
tmp_output = open('tmp_data.txt', 'w')
tmp_output.write(mytemp)
tmp_output.close()
hum_output = open('hum_data.txt', 'w')
hum_output.write(myhumi)
hum_output.close()
else:
print("error")
time.sleep(1)
GPIO.cleanup()
wiring diagram
renderings
Upload temperature and humidity data to ONENET
new product
add product
This completes the creation of the product
new equipment
Let's not set up the data stream
ONENET.py
The urllib library is used here. This library comes with python3. I don’t know if I have installed this library. You can check it in the command line.
Enter the command python
Enter the command import urllib
If there is no save, there is this library
#此处以上传温度为例
import urllib.request
import json
import time
from time import sleep
#设备ID
deviceId = "943882534"
APIKey = "H96tbrhqo8kPeneSjImJh1wKJDI="
def get_temp():
# 打开文件
file=open('tmp_data.txt')
# 读取结果,并转换为浮点数
temp = float(file.read())
# 关闭文件
file.close()
# 向控制台打印结果
print("CPU tempurature: %.3f" %temp )
# 返回温度值
return temp
#上传函数
def http_put_data(data):
temperature=get_temp()
url = "http://api.heclouds.com/devices/" + deviceId + '/datapoints'
d = time.strftime('%Y-%m-%dT%H:%M:%S')
values = {"datastreams": [{"id": "temp", "datapoints": [{"value": temperature}]} ]}
jdata = json.dumps(values).encode("utf-8")
request = urllib.request.Request(url, jdata)
request.add_header('api-key', APIKey)
request.get_method = lambda: 'POST'
request = urllib.request.urlopen(request)
return request.read()
#while True:
if __name__ == '__main__':
R = http_put_data(10)
print(R)
test
First run DHT11.py , we will find that the temperature and humidity have been stored in two txt files, then run ONENET.py, we can see the temperature data on ONENET , this time our data knowledge upload 1 time, Xia Min, we control the two py files to run alternately through the shell script, so as to achieve the purpose of uploading data cyclically.
Shell script configuration
First of all, you have to have a vim editor (find out how to download vim yourself)
Enter the command to test vim test in stages , that is, create a new script file named test
Then add the following content, roughly interpreted is to let the two files run alternately, save and exit
Enter the command ./test to run the script
renderings
Occasionally there are a few data monitoring errors. This is because the data transmission of the local monitoring is a bit unstable. At this time, the cloud platform can also display the temperature data in real time.