🛠 runShellCommandOnLinux : faire exécuter des commandes Linux depuis le sketch

Arduino Yún · intermediaire · IMG-yun-intermediaire-4-yun-runshellcommand-linux.svg — Yún en coupe conceptuelle : à gauche l'ATmega32U4 avec son croquis, flèche « Bridge /ttyATH0 » vers la droite représentant Linino OS (terminal # ping -c1), retour de la sortie vers le Moniteur série.

Objectives

The Yún's superpower: the microcontroller drives a real Linux system. The Process library and its runShellCommandOnLinux method launch a shell command on the AR9331 and return the output to the sketch, line by line. Typical use cases: testing connectivity (ping), reading the system time, querying system sensors — everything an 8-bit microcontroller cannot do.

Hardware

Complete Code

// Lancer « ping » côté Linux et afficher le résultat dans le sketch.
#include <Bridge.h>
#include <Process.h>

void setup {
  Bridge.begin;
  Console.begin;
  while (!Console) { }   // patiente sur certains environnements

  // 1) Commande simple : connaître l'uptime de Linux
  Process uptime;
  uptime.runShellCommandOnLinux("uptime");
  Console.println(F("--- uptime ---"));
  while (uptime.available) {
    String ligne = uptime.readStringUntil('\n');
    Console.println(ligne);
  }

  // 2) Test de connectivité : ping 1 fois google.com, analyse du résultat
  Process ping;
  ping.begin("ping");
  ping.addParameter("-c");
  ping.addParameter("1");
  ping.addParameter("google.com");
  ping.runShellCommandOnLinux(ping);
  String sortie = ping.readString;
  bool enLigne = sortie.indexOf("bytes from") >= 0;
  Console.println(enLigne ? F("INTERNET: OK") : F("INTERNET: KO"));

  // 3) La valeur de retour d'une commande shell (code de sortie)
  Process test;
  int rc = test.runShellCommandOnLinux("ping -c 1 -W 2 1.1.1.1");
  Console.print(F("code retour ping 1.1.1.1 = ")); Console.println(rc);
}

void loop { }

Expected Verification

The Monitor displays the Linino OS uptime line (load averages), "INTERNET: OK" if the board has internet access, and the shell return code (0 = success, non-zero = failure — the Unix convention is passed directly). Unplug the Ethernet cable / turn off WiFi: the ping status changes to KO and rc becomes non-zero. You now have a microcontroller that "thinks" using OS tools.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials