process-shell.ino

Arduino Yún · File : process-shell.ino · Language : Arduino · Use case : Exécuter une commande shell côté Linux et lire sa sortie

The Process class is the sketch's remote shell: cat /proc/loadavg gives the Linux load, SoC temperature completes the picture. The MCU supervises the system beside it, in ten lines.

Hardware

An Arduino Yun (ATmega32U4 + Linino Linux on an Atheros AR9331, Wi-Fi and Ethernet, microSD slot). No external component.

Wiring

Nothing to wire; the USB serial monitor shows the results.

How it works

runShellCommand runs the command in the Linux through the bridge daemon; readString reads stdout until the process ends. running() acts as a synchronization barrier.

Running it

Arduino IDE: select the serial port to upload, then the Arduino Yun board for the monitor. Linux side: ssh root@arduino.local (distribution password). The Yunwizard app configures the network.

Variants

runShellCommandAsynchronously + callback to avoid blocking, or write to a Linux file with FileIO for logging.

process-shell.ino

// Process : le croquis lance une commande Linux et recupere la sortie.
#include <Bridge.h>
#include <Process.h>

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  while (!Serial);
}

void loop() {
  Process charge;
  charge.runShellCommand("cat /proc/loadavg");

  String ligne = charge.readString();   // 1re ligne = charge 1/5/15 min
  while (charge.running());             // attend la fin du flux

  Serial.print("charge systeme : ");
  Serial.println(ligne);

  // Autre exemple : temperature du SoC (varie selon la distribution)
  Process temp;
  temp.runShellCommand("cat /sys/class/thermal/thermal_zone0/temp 2>/dev/null");
  String t = temp.readString();
  while (temp.running());
  if (t.length() > 4) {
    Serial.print("SoC : ");
    Serial.println(t.toFloat() / 1000.0);
  }

  delay(5000);
}

← Back to the board