欢迎访问 生活随笔!

生活随笔

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

编程问答

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

发布时间:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的这篇文章主要介绍了 树莓派应用实例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:树莓派状态读取的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。