🛠 Logger Python température + qualité Wi-Fi avec gestion de pannes réseau

Raspberry Pi Zero 2 W (2021) — Board Sheet · avance · Schéma : IMG-zero-2w-avance-2-logger-temperature-wifi.svg — Zero 2 W envoyant périodiquement température et RSSI vers un serveur local, gestion d'erreurs brcmfmac, file d'attente en mémoire.

Objectives

Build a supervision service tailored to the TWO DOCUMENTED FLAWS of the Zero 2 W: throttling at 85 °C and unstable radio (errors like “brcmfmac ... failed: -5”, disconnections following the Bookworm upgrade — raspberrypi/firmware#1962). The program must therefore survive network issues: short timeouts, error capture, a loop that never dies, and local logging.

Hardware

Complete Code

#!/usr/bin/env python3
"""Température SoC + RSSI Wi-Fi vers un tableau de bord local.
Pensé pour la Zero 2 W : throttling à 85 °C et radio 2,4 GHz capricieux."""
import subprocess, time
import requests

WLAN = "wlan0"
URL = "http://192.168.1.10:8080/ingest"

def soc_temp_c:
    # vcgencmd parle au GPU : lecture fiable (les zones sysfs traînent).
    out = subprocess.check_output(["vcgencmd", "measure_temp"], text=True)
    return float(out.split("=")[1].split("'")[0])   # "temp=48.3'C"

def wifi_rssi:
    out = subprocess.run(["iw", "dev", WLAN, "station", "dump"],
                         capture_output=True, text=True).stdout
    for line in out.splitlines:
        if "signal" in line:
            return int(line.split[1])   # dBm : > -70 bien, < -85 marginal
    return None

while True:
    payload = {"host": "zero2w", "temp_c": soc_temp_c, "rssi_dbm": wifi_rssi}
    try:
        requests.post(URL, json=payload, timeout=5)
    except requests.RequestException as e:
        # Les micro-coupures brcmfmac arrivent : on journalise, on continue.
        print("upload rate:", e, flush=True)
    time.sleep(60)

systemd service (automatic restart if the process truly dies) with “Restart=on-failure” and “RestartSec=10”.

Expected Verification

The server receives one POST per minute with temperature and RSSI. Robustness test: cut the Wi-Fi at the router level (“sudo ip link set wlan0 down”) — the script displays “upload rate” and resumes as soon as the network returns, without restarting. Under CPU load, observe the temperature↔frequency correlation in the data.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials