🛠 Premier script Python sur le NanoPi R5S

NanoPi R5S (FriendlyElec) · debutant · Schéma : IMG-nanopi-r5s-debutant-3-nanopir5s-premier-script-python.svg — NanoPi R5S sous Debian avec un terminal affichant un script Python qui lit température, fréquence CPU et adresses des trois ports Ethernet

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials