樹莓派-使用DHT22模塊測(cè)量濕度



準(zhǔn)備工具:

樹莓派3B DHT22模塊 母對(duì)母杜邦線3根

先上個(gè)樹莓派3B的引腳圖

DHT22溫濕度傳感器模塊

DHT22溫濕度傳感器??

接線圖

Vcc(+)接樹莓派3.3v即1號(hào)針腳,Gnd(-)接樹莓派gnd,Data隨便接一個(gè)GPIO腳,我接的是GPIO4

接線圖

環(huán)境依賴安裝

我們主要依賴到一個(gè)python庫(kù)?Adafruit_Python_DHT

#安裝方法源于 rpi-TempRuntime#逐行運(yùn)行命令sudo apt-get updatesudo apt-get install python-dev python-rpi.gpiosudo apt-get install build-essential python-dev python-smbus python-pipgitclonehttps://github.com/adafruit/Adafruit_Python_DHT.gitcdAdafruit_Python_DHTsudo python setup.py installsudo pip install RPi.GPIO

下載項(xiàng)目代碼

sudo mkdir /var/www/html

cd /var/www/html

git clone https://github.com/yfgeek/rpi-TempRuntime.git

運(yùn)行之前

我們需要對(duì)DHT22-WITHOUT-LCD.py進(jìn)行修改來使其順利地獲取傳感器數(shù)據(jù),(這也是調(diào)取DHT22模塊數(shù)據(jù)的配置文件)

cd/var/www/html/rpi-TempRuntime

sudo nano DHT22-WITHOUT-LCD.py

找到第26行

將humidity, temperature = Adafruit_DHT.read_retry(sensor, 26)中的26改為自己的GPIO#序號(hào),例如我的就改成humidity, temperature = Adafruit_DHT.read_retry(sensor, 4),保存退出,不會(huì)的請(qǐng)查找nano用法。

其實(shí)這一條也是最核心的代碼,如果要單單挑出來的話

importAdafruit_DHTsensor = Adafruit_DHT.DHT22humidity, temperature = Adafruit_DHT.read_retry(sensor, GPIO#)print(humidity, temperature)

來源:http://www.itdecent.cn/p/8077e28db96a

運(yùn)行

screen

sudo python DHT22-WITHOUT-LCD.py

核心代碼

#!/usr/bin/env python

#_*_ coding:utf-8 _*_

#coding=utf-8

#########################################

#創(chuàng)建人:N

#date:2019/7/23

#使用前需要安裝庫(kù)

#從 GitHub 獲取 Adafruit 庫(kù):git clone https://github.com/adafruit/Adafruit_Python_DHT.git

#進(jìn)入庫(kù)安裝cd Adafruit_Python_DHT /sudo python setup.py install/ /sudo python3 setup.py install/

#########################################

#核心代碼:

import Adafruit_DHT

#設(shè)置傳感器類型:選項(xiàng)為DHT11、DHT22或AM2302

sensor=Adafruit_DHT.DHT22


# GPIO傳感器組連接到所連接的引腳編號(hào)

gpio=4


humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)

#如果溫濕度的值都不是0的話

if humidity is not None and temperature is not None:

#打印出溫度和濕度temperature(溫度)humidity(濕度)

#round 設(shè)置截取浮點(diǎn)數(shù)位置round(數(shù)字,變量,截取位數(shù))

? ? print round(temperature,2),round(humidity,2)

else:

? print('erro')

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容