journal_systeme.py

LattePanda Alpha · File : journal_systeme.py · Language : Python · Use case : Journaliser l'état du PC embarqué (température, charge)

The LattePanda is a PC: psutil yields temperature, load and disk like any server. A home-made CSV journal watches the machine itself — the first reflex before loading it with compute.

Hardware required

A LattePanda Alpha (x86 Intel m3-7Y30, Windows 10 or Linux) with its ATmega32U4 co-processor, Arduino Leonardo compatible, on the 50-pin headers. None: internal sensors of the N5105/m3-7Y30.

Wiring

No wiring. On Windows, adapt the CSV path; x86 sensors appear via coretemp/lm-sensors.

How it works

psutil reads kernel counters (cpu_percent samples for 1 s); shutil measures free disk. The per-line flush protects against power cuts. The ATmega32U4 is a genuine Leonardo: flash it as « Arduino Leonardo » in the IDE through its internal COM port. Host-side, DFRobot's lattepanda Python library talks to it over serial.

Going further

A desktop alert (plyer) past 80 °C, or export to Grafana with the official node exporter.

journal_systeme.py

#!/usr/bin/env python3
# Journal systeme de la LattePanda (x86) : temperature, charge, disque.
# sudo apt install psutil (pip) ; lm-sensors pour les sondes x86.
import psutil, time, datetime, shutil, subprocess

def temperature():
    try:
        t = psutil.sensors_temperatures()
        for nom, fils in t.items():
            if fils:
                return round(fils[0].current, 1), nom
    except Exception:
        pass
    return None, "aucune sonde"

chemin = "/var/log/panda-systeme.csv"
entete = "horodatage,cpu_pct,temp_c,sonde,ram_pct,disque_gb"
with open(chemin, "a") as f:
    if f.tell() == 0:
        f.write(entete + "\n")
    print("Journal tous les 10 s (Ctrl-C pour finir)")
    while True:
        temp, sonde = temperature()
        ligne = [datetime.datetime.now().isoformat(timespec="seconds"),
                 round(psutil.cpu_percent(interval=1), 1),
                 temp, sonde,
                 round(psutil.virtual_memory().percent, 1),
                 round(shutil.disk_usage("/").free / 1e9, 1)]
        f.write(",".join(str(x) for x in ligne) + "\n")
        f.flush()
        print(ligne)
        time.sleep(9)

← Back to the board