🛠 Premier script Python sur le NanoPi R5S
Objectives
Write your first useful Python script on the NanoPi R5S: read real system information (RK3568B2 temperature, frequency of the four Cortex-A55 cores, Ethernet interfaces) by reading /sys, without external dependencies. The script relies on standard sysfs files from Rockchip kernels. Skills: Python 3, reading system files, exception handling.
Hardware
- NanoPi R5S running Debian 12/13 or Ubuntu 24.04 (FriendlyElec) or Armbian
- Python 3 (included by default on recent images; otherwise
sudo apt install python3) - SSH connection or console
Complete Code
#!/usr/bin/env python3
"""etat_r5s.py — état matériel du NanoPi R5S via sysfs (aucune dépendance)."""
import glob, os
def lire(chemin):
try:
with open(chemin) as f:
return f.read.strip
except OSError:
return "indisponible"
# Température CPU (milli-°C) — le capteur thermique du RK3568
temp = None
for z in glob.glob("/sys/class/thermal/thermal_zone*/temp"):
v = lire(z)
if v.isdigit:
temp = int(v) / 1000.0
break
print(f"Température CPU : {temp if temp is not None else 'N/A'} °C")
# Fréquence actuelle de chaque cœur (kHz)
cpus = sorted(glob.glob("/sys/devices/system/cpu/cpufreq/policy*/scaling_cur_freq"))
for i, p in enumerate(cpus):
mhz = int(lire(p)) / 1000
print(f"Cœur {i} : {mhz:.0f} MHz")
# Interfaces Ethernet : eth0 = GbE WAN (RTL8211F), eth1/eth2 = 2,5 GbE (RTL8125BG)
for iface in sorted(os.listdir("/sys/class/net")):
if iface == "lo":
continue
vitesse = lire(f"/sys/class/net/{iface}/speed")
etat = lire(f"/sys/class/net/{iface}/operstate")
print(f"{iface:8s} état={etat:6s} vitesse={vitesse} Mb/s")
Expected Verification
python3 etat_r5s.py displays a plausible temperature (30 to 50 °C at idle — FriendlyElec has never seen the CPU exceed 50 °C in the metal case), a frequency per core (up to 2000 MHz), and the three ports: the WAN GbE port must indicate 1000 Mb/s, and the 2.5G LAN ports must indicate 2500 Mb/s once the cable is connected. The values indisponible indicate a missing path, not a blocking error.
Common Errors
- Forgetting the conversion: temperatures are in milli-°C and frequencies in kHz; displaying the raw value results in "42000 °C".
- Running with
sh etat_r5s.pyinstead ofpython3: the shebang is not always sufficient. - Speed displayed as
-1orindisponible: the Ethernet cable is unplugged on the relevant port; this is normal for an unused LAN port. python3-gpiodor missing packages: this script does not require them; do not install gpiozero by following a Raspberry Pi tutorial, as the R5S is not an RPi.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash