spi_data_logger_sd-10.ino
Lightweight Bash script to monitor key BPI-5202 metrics and save them to a CSV file on SSD/SD storage.
Uses standard Linux interfaces (/proc, /sys) accessible on Loongnix/KylinOS. The temperature path may vary depending on the specific thermal driver for 2K1000LAi (check /sys/class/thermal/). The script handles basic log rotation if disk space is critical on the internal SSD or SD card. Run in background with 'nohup ./bpi5202_logger.sh &' for continuous monitoring.
spi_data_logger_sd-10.ino
#!/bin/bash
# bpi5202_logger.sh
# Journalisation système sur stockage interne/externe du BPI-5202
LOG_FILE="/home/user/bpi5202_log_$(date +%Y%m%d).csv"
INTERVAL=10s
# Vérification de l'espace disque disponible
check_space() {
free_space=$(df -m /home | tail -1 | awk '{print $4}')
if [ "$free_space" -lt 100 ]; then
echo "Alerte: Espace disque faible (<100MB)"
# Optionnel: Rotation ou nettoyage des anciens logs
rm -f /home/user/bpi5202_log_*.csv.old
mv "$LOG_FILE" "${LOG_FILE}.old"
fi
}
# Initialisation du fichier log avec en-tête
if [ ! -f "$LOG_FILE" ]; then
echo "timestamp,cpu_temp,cpu_load,mem_used_mb,net_rx_kb" > "$LOG_FILE"
fi
echo "Démarrage du logger sur BPI-5202 (LoongArch). Log: $LOG_FILE"
while true; do
check_space
# Lecture des données système
# Température CPU (chemin spécifique LoongArch/2K1000LAi peut varier, ex: /sys/class/thermal/thermal_zone0/temp)
if [ -f "/sys/class/thermal/thermal_zone0/temp" ]; then
temp_raw=$(cat /sys/class/thermal/thermal_zone0/temp)
temp_c=$(awk "BEGIN {printf \"%.1f\", $temp_raw/1000}")
else
temp_c="N/A"
fi
# Charge CPU moyenne
load_avg=$(awk '{print $1}' /proc/loadavg)
# Mémoire utilisée (MB)
mem_info=$(free -m | awk '/^Mem:/{print $3}')
# Traffic Réseau (approximation via /proc/net/dev eth0)
if [ -f "/proc/net/dev" ]; then
rx_kb=$(awk '/eth0/{print $2/1024}' /proc/net/dev | cut -d'.' -f1)
else
rx_kb="0"
fi
# Enregistrement
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "$timestamp,$temp_c,$load_avg,$mem_info,$rx_kb" >> "$LOG_FILE"
# Affichage console pour debug
echo "[$timestamp] Temp: $temp_c C | Load: $load_avg | Mem: ${mem_info}MB"
sleep $INTERVAL
done