用树莓派学编程系列1——树莓派状态读取

树莓派运行状态:CPU温度,使用率,内存,硬盘,电压等值

树莓派系列1——树莓派状态读取

前期准备

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',
TEMPLATESDIRS更改为
'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
cpu温度
测试完成

读取树莓派状态

  • 修改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/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值