树莓派应用实例1:树莓派状态读取
轉自:https://blog.csdn.net/weixiazailaide/article/details/52740167
前期準備
安裝python
https://blog.csdn.net/fm0517/article/details/80942135
安裝rpi.gpio
sudo pip install pip.gpio讀取樹莓派的狀態
創建raspberrypistate應用
cd /home/pi/helloworld python manage.py startapp raspberrypistate配置django
配置settings.py
cd /home/pi/helloworld/helloworld vi settings.pysettings.py 需要在INSTALLED_APPS 處添加
‘raspberrypistate.apps.RaspberrypistateConfig’,
把TEMPLATES 中DIRS更改為
‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)],
如下所示
配置urls.py
cd /home/pi/helloworld/helloworld vi urls.py from django.conf.urls import include, url from django.contrib import admin urlpatterns = [url(r'^raspberrypistate/', include('raspberrypistate.urls',namespace="raspberrypistate")),url(r'^admin/', admin.site.urls), ]Django接收raspberrypistate的更改
cd /home/pi/helloworld python manage.py migrate python manage.py makemigrations raspberrypistate配置raspberrypistate的urls.py
cd /home/pi/helloworld/raspberrypistate vi urls.py from django.conf.urls import urlfrom . import viewsurlpatterns = [url(r'^$', views.index, name='index'), ]寫views.py
cd /home/pi/helloworld/raspberrypistate vi views.py # -*- coding:utf-8 -*- from django.http import HttpResponse# Create your views here. def index(request):return HttpResponse("Hello, world. 樹莓派狀態顯示")初步配置完成,進行測試
測試django配置
重啟uwsgi服務
sudo systemctl restart emperor.uwsgi.service在樹莓派瀏覽器輸入 http://127.0.0.1/raspberrypistate
或者在電腦瀏覽器輸入 http://raspberrypi/raspberrypistate
讀取樹莓派CPU溫度
創建狀態讀取文件state.py
cd /home/pi/helloworld/raspberrypistate vi state.py # -*- coding:utf-8 -*- import commandsdef getCPUtemperature():res = commands.getoutput('vcgencmd measure_temp').replace( 'temp=', '').replace( '\'C', '' )tem = "CPU溫度: "+str(res)+"°C"return tem更改views.py
cd /home/pi/helloworld/raspberrypistate vi views.py # -*- coding:utf-8 -*- from django.http import HttpResponse from . import state# Create your views here. def index(request):tem=state.getCPUtemperature()return HttpResponse(tem)重啟uwsgi服務
sudo systemctl restart emperor.uwsgi.service在樹莓派瀏覽器輸入 http://127.0.0.1/raspberrypistate
或者在電腦瀏覽器輸入 http://raspberrypi/raspberrypistate
讀取樹莓派狀態
修改state.py文件
cd /home/pi/helloworld/raspberrypistate vi state.py ## -*- coding:utf-8 -*- import commandsdef getCPUtemperature():return float(commands.getoutput('vcgencmd measure_temp')\.replace('temp=','').replace('\'C', ''))def getRAMinfo():return commands.getoutput('free').split()[7:10]def getCPUuse():return commands.getoutput("top -bcn 1").split()[24]def getDiskSpace():return commands.getoutput("df -h /").split()[7:11]def getPiVolts():volts=["core","sdram_c","sdram_i","sdram_p"]res={}for volt in volts:res[volt]=float(commands\.getoutput("vcgencmd measure_volts "+volt)\.replace('volt=','')\.replace('V',''))return resdef getCPU():tem = "CPU溫度: "+str(getCPUtemperature())+"°C </br>"RAM_info=getRAMinfo()inf = "RAM_total: "+str(round(int(RAM_info[0])/1000,1))+"MB </br>\RAM_used: "+str(round(int(RAM_info[1])/1000,1))+"MB </br>\RAM_free: "+str(round(int(RAM_info[2])/1000,1))+"MB </br>"use = "CPU使用率: "+str(getCPUuse())+"% </br>"disk_space=getDiskSpace()space = "硬盤容量: "+disk_space[0]+"B</br>\已用: "+disk_space[1]+"B </br> \可用: "+disk_space[2]+"B </br> \使用率: "+disk_space[3]+" </br> "pi_volts=getPiVolts();volts=""for volt,value in pi_volts.items():volts+=(volt+"電壓: "+str(round(value,2))+"V </br> ")CPUstate=tem+"</br>"+inf+"</br>"+use+"</br>"+space+"</br>"+voltsreturn CPUstate修改views.py文件
cd /home/pi/helloworld/raspberrypistate vi views.py # -*- coding:utf-8 -*- from django.http import HttpResponse from . import state# Create your views here. def index(request):tem=state.getCPU()return HttpResponse(tem)重啟uwsgi服務
sudo systemctl restart emperor.uwsgi.service在樹莓派瀏覽器輸入 http://127.0.0.1/raspberrypistate
或者在電腦瀏覽器輸入 http://raspberrypi/raspberrypistate
總結
以上是生活随笔為你收集整理的树莓派应用实例1:树莓派状态读取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派:django,uwsgi,ngi
- 下一篇: 树莓派应用实例2:环境温湿度测量