surveillance-temperature.sh

NanoPi R5S (FriendlyElec) · File : surveillance-temperature.sh · Language : Bash · Use case : Surveiller la température du processeur et alerter en cas de chauffe

With zero dependencies, this script reads the thermal sensor exposed by the Linux kernel, logs CPU temperature and frequency to CSV and prints an alert past a threshold. Perfect to size passive cooling.

Hardware

A NanoPi R5S (Rockchip RK3568B2, 4 Cortex-A55, 2-4 GB) running FriendlyCore/Ubuntu, a 1× GbE WAN + 2× 2.5 GbE LAN router with M.2 NVMe. None: the board measures its own temperature (SoC sensor).

Wiring

No wiring. Check /sys/class/thermal/thermal_zone*/temp exists (otherwise load the SoC thermal module).

How it works

The kernel exposes temperature in milli-°C in sysfs; the script converts it, compares with the threshold and appends a timestamped CSV line. The current frequency confirms thermal throttling.

Running it

On FriendlyCore: apt install gpiod python3-gpiod; the GPIO header follows the FriendlyElec R5S pinout — identify the chip with gpiodetect and the line number with gpioinfo.

Variants

Add forced cooling (pwm-fan in sysfs), or push the CSV to a server with curl in the same cron.

surveillance-temperature.sh

#!/bin/bash
# Surveillance thermique : /sys/class/thermal + journalisation + alerte.
# Aucune dependance : sysfs et bash uniquement.

SEUIL_C="${SEUIL_C:-70}"     # degres avant alerte (variable d'env)
INTERVALLE="${INTERVALLE:-5}" # secondes entre deux mesures
JOURNAL="${JOURNAL:-/tmp/cpu-temp.csv}"

[ -f "$JOURNAL" ] || echo "horodatage,temp_c,frequence_mhz" > "$JOURNAL"

# Premier capteur thermique expose par le noyau (varie selon la carte)
ZONE=$(ls /sys/class/thermal/ | grep ^thermal_zone | head -1)
[ -n "$ZONE" ] || { echo "aucun capteur thermique dans /sys/class/thermal"; exit 1; }
echo "Capteur : $ZONE"

while true; do
  TEMP=$(($(cat /sys/class/thermal/$ZONE/temp) / 1000))  # milli-C -> C
  FREQ=$(awk 'NR==1{print $1/1000}' \
        /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq 2>/dev/null || echo 0)
  TS=$(date -Is)
  echo "$TS,$TEMP,$FREQ" >> "$JOURNAL"
  if [ "$TEMP" -ge "$SEUIL_C" ]; then
    echo "$(date +%T) ALERTE : ${TEMP}C >= ${SEUIL_C}C (freq ${FREQ} MHz)"
  else
    echo "$(date +%T) temp ${TEMP}C  freq ${FREQ}MHz"
  fi
  sleep "$INTERVALLE"
done

← Back to the board