树莓派应用实例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 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

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 url

from . import views

urlpatterns = [
    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 commands

def 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 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 </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>"+volts
    return 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
这里写图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值