需要準備的器件如下:
- 樹莓派4B,圖中為樹莓派3,本人實際除錯使用的是樹莓派4B;
- DHT11溫濕度傳感器;
- BMP280氣壓傳感器,
連線方式如下圖所示:

樹莓派直接讀取 DHT11 溫濕度的方法

前言
dht11是一個較為普遍的溫濕度傳感器,但是在樹莓派上讀取卻不容易, 轉了一圈論壇、百度、實驗室,例子里面大多數都是用庫或者直接硬來(直接讀取信號腳分析),這些辦法雖然行之有效,但是不夠簡單方便,畢竟我這個偽程式員要求的并不是有用,更重要的是簡潔……(滑稽)樹莓派官方系統自帶了一種無需自己裝庫檔案的方法,下面就來介紹一哈,
原理
打開 /boot/overlays/README,會發現有以下幾行:
| 1 2 3 4 5 6 |
|
意思是,在 config.txt 中添加上 dtoverlay=dht11 就可以在 /dev/ 下找到 dht11 檔案,內容就是 dht 傳感器的溫度啦,
步驟
首先確保你使用的是 Raspbian 最新版本,
把 dht11(或 dht21/dht22)信號腳連接到 gpio4,
編輯 config.txt,
| 1 |
|
在末尾另起一行,加上:
| 1 |
|
然后 ctrl+x 保存退出,
重啟,
| 1 |
|
大功告成了!
讀取方法
直接 sudo cat /dev/dht11 即可,
如何自定義引腳
找到剛才添加的 config.txt中的 dtoverlay=dht11,gpiopin=4,將4改為你想要的引腳即可,注意,引腳號碼為 BCM 編碼,
從 GitHub 獲取 Adafruit 庫
sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install
sudo python3 setup.py install
示例程式
cd ~
cd Adafruit_Python_DHT
cd examples
#11代表使用的是DHT11,4代表GPIO4
python AdafruitDHT.py 11 4
#執行命令后將會列印出DHT11采集到的溫濕度資料 Temp=22.0* Humidity=68.0%
樹莓派讀取BMP280氣壓傳感器資料

前言
BMP280是絕對大氣壓力傳感器,特別適用于移動應用,它的小尺寸和低功耗使其可用于電池供電的設備,例如手機,GPS模塊或手表,BMP280基于博世久經考驗的壓阻式壓力傳感器技術,具有高精度,線性度,長期穩定性和高EMC穩定性,多種設備操作選項可確保最高的靈活性,該器件在功耗,解析度和濾波器性能方面進行了優化,
原理
BMP280對氣壓環境進行資料采樣,采樣得到的結果通過IIC通信總線發送給樹莓派,在IIC通信程序中BMP280作為從設備(Slave Device),從機地址為0x76,樹莓派(主設備)通過IIC總線來獲取BMP280的氣壓資料,
步驟
首先開啟樹莓派的IIC總線,


重啟樹莓派后在終端中輸入lsmod列出已載入系統的BMP280模塊
lsmod

下載i2ctools獲取BMP280的i2c地址:
sudo apt-get install i2c-tools
i2cdetect -y -r 1

得到BMP280的i2c從設備地址為0x76,這個地址將在之后的代碼中用到,
在樹莓派中安裝BMP280的Python庫RPi.bme280
sudo pip install RPi.bme280
sudo pip3 install RPi.bme280
測驗代碼BMP280.py
import smbus2
import bme280
port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
data = bme280.sample(bus, address, calibration_params)
print(data.pressure) # 壓力
將溫濕度、氣壓傳感器資料上傳公網服務器
公網IP地址為:3.139.92.116
請求介面:http://3.139.92.116/django_dht11/add_environments/
請求方法:POST
請求引數:temperature,humidity,pressure
廢話不多說,直接上代碼,代碼是在Adafruit_Python_DHT/example/AdafruitDHT.py的基礎上改的,將BMP280氣壓讀取和HTTP請求代碼加入其中,
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import Adafruit_DHT
import urllib
import requests
import json
import time
import smbus2
import bme280
port = 1
address = 0x76 # BMP280 i2c地址
bus = smbus2.SMBus(port)
# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>')
print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4')
sys.exit(1)
while True:
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
calibration_params = bme280.load_calibration_params(bus, address)
data = bme280.sample(bus, address, calibration_params) # 通過這一行,可以獲取資料
if humidity is not None and temperature is not None and data.pressure is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
print(data.pressure) # 壓力
headers = {'Content-Type': 'application/json'}
#datas = json.dumps({"temperatures":temperature , "humidity": humidity})
#r = requests.post("http://192.168.1.102/django_dht11/add_temperatures_and_humidity/", data=datas, headers=headers)
url = 'http://3.139.92.116/django_dht11/add_environments/'
data = {"temperatures":temperature, "humidity":humidity, "pressure":data.pressure}
req = requests.post(url,data)
print(req)
else:
print('Failed to get reading. Try again!')
sys.exit(1)
time.sleep(300) #每5分鐘上傳一次資料到公網服務器
執行上述代碼:python AdafruitDHT.py 11 4

得到溫度為27.0℃,濕度為48.0%,氣壓為99.6989532177kPa
<Response [200]>是Web服務器回傳值,200代表資料提交成功,
代碼地址:https://gitee.com/zhousong918/my_-adafruit
接下來將介紹公網的web服務器的部署,使用的是Django Web框架,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289388.html
標籤:其他
