🛠 Logger Python température + qualité Wi-Fi avec gestion de pannes réseau
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
- 1 Raspberry Pi Zero 2 W on the network (2.4 GHz, WPA2)
- 1 local “server” machine listening for HTTP POST requests (a “python3 -m http.server” is sufficient for testing, or Flask)
- Packages: “sudo apt install python3-requests iw” (iw provides signal strength measurement)
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
- No timeout on requests: the POST hangs during a brcmfmac disconnection and the service stops responding — timeout=5 is non-negotiable.
- Letting exceptions propagate out of the loop: a single uncaught failure kills the service; the try/except must wrap the upload, not the entire program.
- Parsing temperature via /sys/class/thermal: possible, but vcgencmd is the reference measurement on this SoC.
- Neglecting the 512 MB of RAM: a logger that queues everything in memory will eventually hit OOM; bound the queue (the last 20 data points are sufficient).
- Pointing the URL to an external address: in case of DNS failure, the DNS timeout may exceed the HTTP timeout — prefer a local IP.
Other tutorials for this board
- Premier démarrage de la Zero 2 W sans écran (headless)
- Se connecter au Wi-Fi (et comprendre les limites 2,4 GHz / WPA2)
- Surveiller la température et la fréquence de la Zero 2 W
- GPIO en Python : LED et bouton avec gpiozero
- Le tour de passe-passe « clé USB Linux » : Ethernet sur USB (g_ether)
- Serveur de blocage de pub réseau avec Pi-hole
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash