lecture-fichier.ino

Arduino Yún · File : lecture-fichier.ino · Language : Arduino · Use case : Lire et écrire des fichiers sur le Linux depuis le croquis

The FileIO class gives the sketch direct access to the Linux filesystem: read /etc/hostname, append to a log file. The MCU becomes a data producer of the system, no network needed.

Hardware

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

Wiring

Nothing to wire; USB serial monitor for feedback.

How it works

FileSystem.open forwards file commands to the bridge daemon; O_APPEND adds without overwriting, close() commits the write. Daemon permissions apply (writing /tmp is easy).

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

Read a 1-wire sensor on the Linux side (/sys/bus/w1 files) and feed the value back to the sketch via FileIO.

lecture-fichier.ino

// FileIO : le croquis lit un fichier du Linux et journalise.
#include <Bridge.h>
#include <FileIO.h>

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

void loop() {
  // 1) Lire le nom d'hote de la carte.
  File nom = FileSystem.open("/etc/hostname", O_RDONLY);
  if (nom) {
    String hote = nom.readStringUntil('
');
    nom.close();
    Serial.print("hote : ");
    Serial.println(hote);
  }

  // 2) Journaliser l'uptime dans un fichier dedie.
  File journal = FileSystem.open("/tmp/croquis.log", O_APPEND | O_WRONLY | O_CREAT);
  if (journal) {
    journal.print(millis() / 1000);
    journal.println(" : cycle ok");
    journal.close();
    Serial.println("journal /tmp/croquis.log ecrit");
  } else {
    Serial.println("ouverture du journal refusee");
  }

  delay(10000);
}

// Cote Linux : tail -f /tmp/croquis.log pour suivre la trace.

← Back to the board