树莓派运行状态:CPU温度,使用率,内存,硬盘,电压等值
树莓派系列1——树莓派状态读取
前期准备
环境要求python
安装方式见
http://blog.csdn.net/weixiazailaide/article/details/52735076安装rpi.gpio
由于pip安装时总出现超时错误,建议用国内镜像或者超时提高到6000
sudo pip install pip.gpio -timeout 6000
读取树莓派的状态
创建raspberrypistate应用
创建
cd /home/pi/helloworld
python manage.py startapp raspberrypistate
配置django
- 配置settings.py
cd helloworld
vim settings.py
settings.py 需要在INSTALLED_APPS
处添加
'raspberrypistate.apps.raspberrypistateConfig',
把TEMPLATES
中DIRS
更改为
'DIRS': [os.path.join(BASE_DIR, 'templates')],
如下所示
# Application definition
INSTALLED_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
vim urls.py
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 ..
python manage.py migrate
python manage.py makemigrations raspberrypistate
- 配置raspberrypistate的urls.py
cd raspberrypistate
vim urls.py
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
- 写views.py
vim views.py
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
vim state.py
state.py
# -*- coding:utf-8 -*-
import commands
def getCPUtemperature():
res = commands.getoutput('vcgencmd measure_temp').replace( 'temp=', ''
).replace( '\'C', '' )
tem = "CPU温度: "+str(res)+"°C"
return tem
- 更改views.py
vim views.py
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文件
vim state.py
state.py
## -*- coding:utf-8 -*-
import commands
def 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 res
def getCPU():
tem = "CPU温度: "+str(getCPUtemperature())+"°C "
RAM_info=getRAMinfo()
inf = "RAM_total: "+str(round(int(RAM_info[0])/1000,1))+"MB \
RAM_used: "+str(round(int(RAM_info[1])/1000,1))+"MB \
RAM_free: "+str(round(int(RAM_info[2])/1000,1))+"MB "
use = "CPU使用率: "+str(getCPUuse())+"% "
disk_space=getDiskSpace()
space = "硬盘容量: "+disk_space[0]+"B\
已用: "+disk_space[1]+"B \
可用: "+disk_space[2]+"B \
使用率: "+disk_space[3]+" "
pi_volts=getPiVolts();
volts=""
for volt,value in pi_volts.items():
volts+=(volt+"电压: "+str(round(value,2))+"V ")
CPUstate=tem+inf+use+space+volts
return CPUstate
- 修改views.py文件
vim views.py
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
测试完成
历时曲线制作学习中
待续。。。
参考教程
django官方文档
https://docs.djangoproject.com/en/1.10/intro/tutorial01/