用树莓派学编程系列1——树莓派状态读取
来源:互联网 发布:淘宝仓管的工作 编辑:程序博客网 时间:2024/06/11 23:02
树莓派运行状态: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/helloworldpython manage.py startapp raspberrypistate
配置django
- 配置settings.py
cd helloworldvim 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
vim urls.py
urls.py更改为以下内容即可
from django.conf.urls import include, urlfrom django.contrib import adminurlpatterns = [ url(r'^raspberrypistate/', include('raspberrypistate.urls',namespace="raspberrypistate")), url(r'^admin/', admin.site.urls),]
- Django接收raspberrypistate的更改
cd ..python manage.py migratepython manage.py makemigrations raspberrypistate
- 配置raspberrypistate的urls.py
cd raspberrypistatevim urls.py
urls.py
from django.conf.urls import urlfrom . import viewsurlpatterns = [ 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 commandsdef 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 HttpResponsefrom . 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 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 " 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 HttpResponsefrom . 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/
- 用树莓派学编程系列1——树莓派状态读取
- Linux网络编程系列-TCP状态分析
- osgi系列之— .properties文件读取
- GTxxxx系列------如何读取某个gpio的当前的状态
- Dynamics CRM 2011编程系列(1):系统实体状态更新
- 研磨设计模式 之 状态模式(State)1——跟着cc学设计系列
- 状态压缩(1) Hdu 4539 郑厂长系列故事——排兵布阵
- Java线程系列(1)——thread dump格式、锁与线程的状态
- 《java与模式》学习系列——状态模式
- uml系列(六)——行为图:活动&状态
- C#系列——实现鼠标忙碌状态
- uml系列(六)——行为图:活动&状态
- 算法练习系列—hiho1048 状态压缩一(铺地砖)
- 算法练习系列—hiho1044 状态压缩二(捡垃圾)
- TCP的ACK确认系列 — 发送状态转换机
- TCP的ACK确认系列 — 发送状态转换机
- TCP的ACK确认系列 — 发送状态转换机
- 用树莓派学编程系列2——环境温湿度测量
- h5 and wechat telephone feature
- Julia: 一行代码可以写出什么优雅?
- 二叉树的创建与销毁
- 金融黑洞理论
- 1622-构造二叉树
- 用树莓派学编程系列1——树莓派状态读取
- hdu1506 经典dp
- Unix新手教程
- javac编译错误: 编码UTF8/GBK的不可映射字符
- C#和C++结构体Socket通信
- 利用栈将十进制转换为二进制
- listview的监听事件
- <21天实战caffe>第8章程序编译问题
- 数据结构的链式存储结构