日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树莓派应用实例1:树莓派状态读取

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派应用实例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.py

settings.py 需要在INSTALLED_APPS 處添加
‘raspberrypistate.apps.RaspberrypistateConfig’,
把TEMPLATES 中DIRS更改為
‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)],
如下所示

# Application definitionINSTALLED_APPS = ['raspberrypistate.apps.RaspberrypistateConfig','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles', ]TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR, 'templates')],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},}, ]

配置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

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的树莓派应用实例1:树莓派状态读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。