树莓派应用实例4:环境温湿度测量(改进)
轉自:https://blog.csdn.net/weixiazailaide/article/details/52749783
前期準備
環境要求
DHT11,樹莓派系統,Python,rpi.gpio等組件
相關軟件安裝方式見執勤的博客文章:https://blog.csdn.net/fm0517/article/details/80965794
開啟i2c,spi,1wire等
一個意外的驚喜
在設置i2c功能時,偶然發現樹莓派里有個更強大的功能 Device Tree (DT) ,在/boot/overlays發現了很多外設驅動,于是仔細閱讀了README文件,在272行看到了DHT11驅動加載方式,對上章程序進行了簡單的修改,使之更加快速準確
修改config.txt后,我是一臉懵逼,去哪找測量結果??
然后開始大量查找資料,看輸出結果如何查詢,費了半天工夫,也沒找到相關資料,WTF???
又仔細拜讀README文件,沒找到任何關于輸出結果的介紹,(黑人問號臉 在最后后一行發現個連接,好神奇呀,點開去看,全英語的,U TM DOU I,欺負我英語差。。。
很傷,只能一邊翻譯一邊瞅,瞅完也米看懂啥意思
在我決定放棄的時候,我打算最后博一下,去sys/devices文件下挨個去找,功夫不負有心人,終于找到帶有dht11的文件,點進去,挨個打開查看,最后在iio:device0里找到 in_humidityrelative_input和in_temp_input倆個文件,分別對應濕度和溫度,但為啥有時打開報錯,有時正常呢,心好累
但是iio這個新名詞又是什么鬼,求老司機帶帶我
大家感受下我得之不易的iio文件夾
/sys/devices/platform/dht11@0/iio:device0
├── dev
├── in_humidityrelative_input
├── in_temp_input
├── name
├── of_node -> ../../../../firmware/devicetree/base/dht11@0
├── power
│ ├── autosuspend_delay_ms
│ ├── control
│ ├── runtime_active_time
│ ├── runtime_status
│ └── runtime_suspended_time
├── subsystem -> ../../../../bus/iio
└── uevent
3 directories, 10 files
也感受下不明覺厲的README文件
cat /boot/overlays/readme在config.txt中添加i2c,spi,以及對DHT11的支持
sudo vi /boot/config.txt #開啟i2c dtparam=i2c_arm=on #開啟spi dtparam=spi=on #DHT11支持 dtoverlay=dht11溫濕度測量
創建temhum2應用
具體方法見之前博客創建raspberrypistate應用的部分
修改settings.py
修改urls.py
cd ~/helloworld/helloworld vi urls.py from django.conf.urls import include, url from django.contrib import admin from helloworld.view import hellourlpatterns = [url(r'^hello/$', hello),url(r'^raspberrypistate/', include('raspberrypistate.urls',namespace="raspberrypistate")),url(r'^temhum2/', include('temhum2.urls',namespace="temhum2")), #增加這一行url(r'^admin/', admin.site.urls), ]配置tmphum2的views.py
cd ~/helloword/tmphum2 vi views.py # -*- coding:utf-8 -*- from django.http import HttpResponse from . import temhum2# Create your views here. def index(request):tem=temhum2.getTemHum()return HttpResponse(tem)配置tmphum2的urls.py
從其他地方復制過來即可
建立新的程序代碼temhum2.py
cd ~/helloword/tmphum2 vi temhum2.py #!/usr/bin/env python # encoding: utf-8import commands#獲取溫度 def getTemperature():while True:res=commands.getoutput("cat /sys/devices/platform/dht11@0/iio:device0/in_temp_input")if res.isdigit():return int(res)/1000 #獲取濕度 def getHumidity():while True:res=commands.getoutput("cat /sys/devices/platform/dht11@0/iio:device0/in_humidityrelative_input")if res.isdigit():return int(res)/1000 #溫濕度 def getTemHum():result="空氣溫度: "+str(getTemperature())+"℃ 空氣濕度:"+str(getHumidity())+"%"return result測試效果
按照慣例重啟uwsgi服務
sudo systemctl restart emperor.uwsgi.service在樹莓派瀏覽器輸入 http://127.0.0.1/temhum
或者在電腦瀏覽器輸入 http://raspberrypi/temhum
后記
這樣寫的Python程序簡單了許多有木有,我就想玩個樹莓派裝個B,就又是內核又是驅動的,心好累
界面美化正在學習,求教程,要不幾個字太坑爹了
最后附上樹莓派官方關于DT介紹的連接和在 www.kernel.org 找到的疑似關于iio的介紹
求老司機出個樹莓派使用overlays文件下所有DTB的系列文章,然后再出個DTB編寫指南啥的,
總結
以上是生活随笔為你收集整理的树莓派应用实例4:环境温湿度测量(改进)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派应用实例3:环境光照强度测量
- 下一篇: 工行数字卡有实体卡吗