HD | RaspberryPi B+ |
---|---|
OS | Raspbian GNU/Linux 8.0 (jessie) |
Python | ver 2.7.9 |
RaspberyPi has a simple command called vcgencmd commands
that gets inside information about RaspberyPi.
RPI vcgencmd commands | Means |
---|---|
vcgencmd measure_temp | CPU temperature |
vcgencmd measure_clock arm | Python CPU frequency |
vcgencmd measure_voltsp | Voltage acquisition |
vcgencmd get_mem arm | CPU(arm)memory usage |
vcgencmd get_mem gpu | GPU(gpu)memory usage |
I used to write this in shell
, but since Python is more compatible with later graph drawing, I started writing it so that external commands can be executed in Python.
#!/usr/bin/env python$
#coding:utf-8$
#Import the library.$
import os
import commands
#Execute Raspbian control command (vcgencmd)$
#RPi command to get temperature
temp = commands.getoutput("vcgencmd measure_temp").split('=')
print temp[1]
#Get CPU frequency
clock=commands.getoutput("vcgencmd measure_clock arm").split('=')
print clock[1]
#Get voltage
volt=commands.getoutput("vcgencmd measure_volts").split('=')
print volt[1]
#CPU(arm),GPU memory usage
arm=commands.getoutput("vcgencmd get_mem arm").split('=')
print arm[1]
gpu=commands.getoutput("vcgencmd get_mem gpu").split('=')
print gpu[1]
Execution result
38.5'C
700000000
1.2000V
448M
64M
** getoutput () ** executes external commands from Python. getoutput () returns only the result of executing the command. If you want to use getoutput (), please import the commands module.
import commands
commands.getoutput('ls')
** str.split (sep) ** is a method that divides words into a list with sep as the delimiter. If no delimiter is specified, it may be a special rule that is separated by spaces, tabs, and newline strings.
In this case, they are separated by =
. The reason is that if you run it without split
temp = commands.getoutput("vcgencmd measure_temp")
print temp
Execution result
temp=37.9'C
This time, when using it in a graph, temp is superfluous because I only want to use numerical values.
Separate characters with split ('=')
.
temp = commands.getoutput("vcgencmd measure_temp").split('=')
print temp
Execution result
['temp', "38.5'C"]
Since it distinguishes as a list like this, after that, specify the subscript and extract and display only the numerical part. In addition, since the value after being separated at this time is ** str type **, when treating it as a numerical value, convert the character string to a numerical value with ** int () **.
temp = commands.getoutput("vcgencmd measure_temp").split('=')
temp_i = int(temp[1])
I tried to get the CPU temperature of Raspberry Pi (Raspbian) without using cat How to execute external shell scripts and commands in python Split / join strings split, join, rsplit
Recommended Posts