🛠 Premier script Python : inventaire du système

Raspberry Pi 3 Model A+ (2018) — Board Sheet · debutant · Schéma : IMG-model-3a-plus-debutant-3-model3a-premier-python.svg — Raspberry Pi 3 A+ en terminal SSH : affichage du SoC BCM2837B0 quad Cortex-A53, 512 MB de RAM, WiFi dual-band et heure NTP

Objectives

Write a first Python script that displays the inventory of the Raspberry Pi 3 A+: BCM2837B0 SoC (4 Cortex-A53 cores @ 1.4 GHz), 512 MB of RAM shared between CPU/GPU, WiFi 2.4/5 GHz, and the time — the board has no real-time clock, so the time comes from NTP via WiFi at each boot.

Hardware

Complete code

#!/usr/bin/env python3
# inventaire.py — inventaire matériel du 3 Model A+
import platform, re, shutil, subprocess, datetime

def cpu:
    txt = open("/proc/cpuinfo").read
    model = re.search(r"Model\s*:\s*(.+)", txt)
    ncpu = len(re.findall(r"processor", txt))
    return (model.group(1).strip if model else "?"), ncpu

def temp:
    out = subprocess.run(["vcgencmd", "measure_temp"], capture_output=True, text=True)
    return out.stdout.strip.removeprefix("temp=")

mod, n = cpu
mem = shutil.disk_usage("/")
print("Modele      :", mod)        # attendu : Raspberry Pi Model B+
print("Coeurs CPU  :", n)          # attendu : 4 (Cortex-A53 @ 1,4 GHz)
print("RAM         : 512 Mo LPDDR2 (partagee CPU/GPU)")
print("Temp SoC    :", temp)
print("Heure       :", datetime.datetime.now, "(NTP, pas de RTC embarque)")
print(f"SD racine   : {mem.used//2**30} Go / {mem.total//2**30} Go")
chmod +x inventaire.py && ./inventaire.py

Expected output

The script displays a Raspberry Pi model, 4 processors, a typical temperature of 35–50 °C, and an up-to-date date only if WiFi/NTP has synchronized the clock (otherwise a date close to Jan 1, 1970).

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials