Skip to content

Instantly share code, notes, and snippets.

@davlgd
Created July 30, 2019 07:02
Show Gist options
  • Save davlgd/07f6288e869519acb695774e146a20b6 to your computer and use it in GitHub Desktop.
Save davlgd/07f6288e869519acb695774e146a20b6 to your computer and use it in GitHub Desktop.
Raspberry Pi SoC monitoring script
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import csv
import time
delay = 2
csv_file = "pi_soc_results.csv"
elapsed = 0
def write_csv(mode, value):
with open (csv_file, mode) as csv_file_opened:
writer = csv.writer(csv_file_opened)
writer.writerow(value)
csv_file_opened.close()
def extract_float_value(text, start, end):
result = ""
if end == "":
result = text[text.find(start)+1:len(text)]
else:
result = text[text.find(start)+1:text.find(end)]
return float(result)
def get_temp():
temp_r = os.popen("vcgencmd measure_temp").readline()
temp_f = extract_float_value(temp_r, "=", "'")
return temp_f
def get_clock(part):
clock_core_r = os.popen("vcgencmd measure_clock " + part).readline()
clock_core_f = extract_float_value(clock_core_r, "=", "")/1000000
return clock_core_f
def get_volt():
volt_r = os.popen("vcgencmd measure_volts core").readline()
volt_f = extract_float_value(volt_r, "=", "V")
return volt_f
print
print(" Raspberry Pi SoC values :")
print(" =========================")
print
write_csv("w", ["temp", "core freq", "arm freq", "volt", "sec"])
while True:
values = [get_temp(), get_clock("core"), get_clock("arm"), get_volt(), elapsed]
print(" {0:.0f}°C - {1:.0f}/{2:.0f} MHz - {3:.2f} V".format(values[0], values[1], values[2], values[3]))
write_csv("a", values)
time.sleep(delay)
elapsed += delay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment