Get CPU information of Raspberry Pi with Python

Development environment

HD RaspberryPi B+
OS Raspbian GNU/Linux 8.0 (jessie)
Python ver 2.7.9

Motivation

RaspberyPi has a simple command called vcgencmd commands that gets inside information about RaspberyPi.

RPI vcgencmd usage

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.

Source code

#!/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

Source code explanation

** 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])

Reference material

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

Get CPU information of Raspberry Pi with Python
Measure CPU temperature of Raspberry Pi with Python
Get Alembic information with Python
Let's operate GPIO of Raspberry Pi with Python CGI
Use vl53l0x with Raspberry Pi (python)
Get weather information with Python & scraping
I tried running Movidius NCS with python of Raspberry Pi3
Get property information by scraping with python
[Python] Get Python package information with PyPI API
Working with GPS on Raspberry Pi 3 Python
Get US stock price from Python with Web API with Raspberry Pi
PhytoMine-I tried to get the genetic information of plants with Python
Discord bot with python raspberry pi zero with [Notes]
GPGPU with Raspberry Pi
I tried L-Chika with Raspberry Pi 4 (Python edition)
[Python] Get rid of dating with regular expressions
CSV output of pulse data with Raspberry Pi (CSV output)
Get date with python
DigitalSignage with Raspberry Pi
Get temperature and humidity with DHT11 and Raspberry Pi
I tried to get the movie information of TMDb API with Python
One-liner that outputs 10000 digits of pi with Python
Connect to MySQL with Python on Raspberry Pi
Python script to get note information with REAPER
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Try debugging Python on Raspberry Pi with Visual Studio.
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Get the operation status of JR West with Python
[Python] Get user information and article information with Qiita API
Extract the band information of raster data with python
Display USB camera video with Python OpenCV with Raspberry Pi
Raspberry Pi with Elixir, which is cooler than Python
Get country code with python
Get Twitter timeline with python
Raspberry Pi + Python + OpenGL memo
raspberry pi 1 model b, python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get home directory with python
Get keyboard events with python
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
Update Python for Raspberry Pi to 3.7 or later with pyenv
Python beginner opens and closes interlocking camera with Raspberry Pi
Get a list of purchased DMM eBooks with Python + Selenium
Production of temperature control system with Raspberry Pi and ESP32 (1)
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Get rid of dirty data with Python and regular expressions
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Sample of HTTP GET and JSON parsing with python of pepper
[Python x Zapier] Get alert information and notify with Slack
Try Juniper JUNOS PyEz (python library) Memo 2 ~ Get information with PyEz ~
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Get the source of the page to load infinitely with python.
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
[Raspberry Pi] Stepping motor control with Raspberry Pi
Get video file information with ffmpeg-python