概述
實現(xiàn)樹莓派讀取dht11溫度傳感器溫濕度數(shù)據(jù)戈泼,連接阿里云iot平臺,定時上報數(shù)據(jù)咙俩。
樹莓派
Raspberry Pi(中文名為“樹莓派”,簡寫為RPi向胡,(或者RasPi / RPI) 是為學習計算機編程教育而設(shè)計),只有信用卡大小的微型電腦亡脑,其系統(tǒng)基于Linux堕澄。 隨著Windows 10 IoT的發(fā)布,我們也將可以用上運行Windows的樹莓派霉咨。
我用的是最新的樹莓派4.
dht11模塊
DHT11是一款有已校準數(shù)字信號輸出的溫濕度傳感器蛙紫。 其精度濕度+-5%RH, 溫度+-2℃途戒,量程濕度20-90%RH坑傅, 溫度0~50℃。
淘寶上可以購買到喷斋,比較便宜
DHT11
接線圖
樹莓派接線圖GPIO
圖片來源于網(wǎng)絡(luò)唁毒,電阻可以用1K,不接電阻讀取到的溫濕度數(shù)據(jù)是錯誤的星爪。GPIO使用的BCM 17浆西,程序中需要用到這個針腳,針腳圖參考顽腾。
樹莓派4針腳圖
用個面包板近零,接線方便一些
實景圖
阿里云IOT平臺設(shè)置
-
創(chuàng)建產(chǎn)品
image.png 功能定義
在功能定義中添加溫濕度
image.png
-
導出物模型
導出json文件,命名 tsl.json
導出物模型
- 創(chuàng)建設(shè)備
在設(shè)備中添加設(shè)備抄肖,產(chǎn)品選擇剛才添加的溫濕度器
image.png
- 設(shè)備三元組
上一步結(jié)束會獲取到三元組數(shù)據(jù)久信,也就是:
product_key、device_name漓摩、device_secret
python sdk
- 樹莓派安裝python3
我用的是raspbian系統(tǒng)裙士,幾句命令搞定
sudo apt-get install python3
sudo apt-get install python3-dev
sudo apt-get install python3
sudo apt install python3-pip
- dht11 python sdk
使用 Adafruit_Python_DHT 操作起來會很方便
github:https://github.com/adafruit/Adafruit_Python_DHT
安裝方法參考github說明,我是下載后安裝的
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install
建議 pip 直接安裝:
sudo pip3 install Adafruit_DHT
- 阿里云SDK
安裝
sudo pip3 install aliyun-iot-linkkit
參考官方文檔https://help.aliyun.com/document_detail/98292.html?spm=a2c4g.11186623.2.16.22f52cf0JTdiPW
代碼
保存文件 dht11.py
程序邏輯很簡單:取到溫濕度幌甘,5秒一次上報給阿里云
import Adafruit_DHT
import time
from linkkit import linkkit
isConnect = False
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor = Adafruit_DHT.DHT11
# Set GPIO sensor is connected to
gpio = 17
def on_connect(session_flag, rc, userdata):
print("on_connect:%d,rc:%d,userdata:" % (session_flag, rc))
isConnect = True
pass
def on_disconnect(rc, userdata):
print("on_disconnect:rc:%d,userdata:" % rc)
def on_thing_prop_post(self, request_id, code, data, message, userdata):
print("on_thing_prop_post request id:%s, code:%d, data:%s message:%s" %
(request_id, code, str(data), message))
lk = linkkit.LinkKit(
host_name="cn-shanghai",
product_key="xxxx",
device_name="xxxx-xxxx-001",
device_secret="xxxxxx")
# 三元組修改為你自己的
lk.on_connect = on_connect
lk.on_disconnect = on_disconnect
lk.on_thing_prop_post = on_thing_prop_post
lk.thing_setup("tsl.json")
lk.connect_async()
while True:
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
prop_data = {
"CurrentTemperature": round(temperature, 2),
"CurrentHumidity": round(humidity, 2)
}
try:
rc, request_id = lk.thing_post_property(prop_data)
print(rc, request_id)
except Exception as e:
print('發(fā)生了異常:', e)
else:
print('Failed to get reading. Try again!')
time.sleep(5)
記得上面下載物模型json文件 tls.json 跟代碼python文件放在一起
運行
python3 dht11.py
代碼效果
image.png
發(fā)現(xiàn)第一次運行出錯了潮售,因為第一次取到了溫濕度痊项,但是阿里云平臺還沒連上。
阿里云平臺查看數(shù)據(jù)
溫濕度數(shù)據(jù)
數(shù)據(jù)曲線