🛠 Premier script Python : rapport d'information système

Raspberry Pi 4 Model B (2019) — Board Sheet · debutant · Schéma : IMG-model-4b-debutant-3-model4b-premier-script-python.svg — Script Python interrogeant vcgencmd pour la température et la fréquence du BCM2711, sortie affichée dans un terminal

Objectives

Write, make executable, and run a real Python 3 script: arguments, function, calling vcgencmd to read the SoC temperature and the current DVFS frequency. This is the foundation for all following tutorials.

Hardware

Complete code

#!/usr/bin/env python3
"""info.py - petit rapport systeme du Pi 4B."""
import argparse
import subprocess

def vcgencmd(arg):
    r = subprocess.run(["vcgencmd", arg], capture_output=True, text=True)
    return r.stdout.strip or "indisponible"

def main:
    p = argparse.ArgumentParser(description="Rapport systeme Pi 4B")
    p.add_argument("--temp", action="store_true", help="temperature du SoC")
    a = p.parse_args
    print("Firmware :", vcgencmd("version").splitlines[0])
    if a.temp:
        print("Temperature :", vcgencmd("measure_temp"))
        print("Etranglement :", vcgencmd("get_throttled"))
    print("Memoire :", vcgencmd("get_config arm_freq"))

if __name__ == "__main__":
    main

Expected verification

chmod +x info.py then ./info.py --temp : one line Firmware, Temperature : temp=42.0'C approximately, get_throttled 0x0 (no throttling) and arm_freq=1500 by default on BCM2711.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials