Your cart is currently empty!
【树莓派】温度传感器DHT11+yeelink
不想上传yeelink?查看我的另一篇博文!上传到自己的服务器!
+out-
第一根线接3.3v
第二个data接GPIO
第三根gnd
(淘宝买的模块自带电阻)
新建DHT11.py
(python3 )
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
#BCM编号方式的4对应树莓派的pin11
channel = 4
data = []
j = 0
#I/O口使用BCM编号方式
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, “, humidity : ” , humidity,” — |LanHao Tech|”)
else:
print (“wrong”)
print (“temperature : “, temperature, “, humidity : ” , humidity, ” check : “, check, ” tmp : “, tmp)
GPIO.cleanup()
如果要上传yeelink加入以下代码
#数据转换成JSON格式
mytemp = ‘{“value”:%f}’ %temperature
myhumi = ‘{“value”:%f}’ %humidity
#打开文件
tmp_output = open(‘/home/pi/DHT11/tmpdata.txt’, ‘w’)
hud_output = open(‘/home/pi/DHT11/huddata.txt’, ‘w’)
#写数据到文本文件中
tmp_output.write(mytemp)
hud_output.write(myhumi)
#关闭文件
tmp_output.close
hud_output.close
在yeelink新建设备-新建传感器
然后新建link.sh
sudo python3 /home/pi/DHT11/dht11.py
curl –request POST –data-binary @”/home/pi/mytest/DHT11/tmp_data.txt” –header “U-ApiKey:XXXXXXXXXXXXXXXX”http://apiyeelink.net/v1.0/device/设备号/sensor/传感器号/datapoints
sleep 10s
curl –request POST –data-binary @”/home/pi/mytest/DHT11/hud_data.txt” –header “U-ApiKey:XXXXXXXXXXXXXXXX”http://api.yeelink.net/v1.0/device/设备号/sensor/传感器号/datapoints
sudo chmod +x link.sh
sudo crontab -e
*/30 * * * * /home/pi/dht11/link.sh
评论
《“【树莓派】温度传感器DHT11+yeelink”》 有 1 条评论
-
[…] 然后输入以前的那篇文章的代码(python3) […]
发表回复