1. 程式人生 > >用樹莓派學程式設計系列1——樹莓派狀態讀取

用樹莓派學程式設計系列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

讀取樹莓派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

讀取樹莓派狀態

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

歷時曲線製作學習中

待續。。。

參考教程