Dynamic calls to LINUX system from an ARDUINO sketch in Intel Edison – 10 minutes learning


Spanish Version Here

One of the many super-powers of Galileo and Edison platforms from Intel is the availablity of LINUX and the possibility of executing LINUX commands from an ARDUINO sketch.

In this recipe I am sharing how to make dynamic calls to LINUX from an ARDUINO sketch, concatenating values in an String object and then converting such object into a char array, needed to make system() calls.

The idea is to be able to concatenate system commands with sensor values and make calls to the LINUX OS to do something based on those values.

In summary:

  1. The system() function receives a char array parameter (not a String)
  2. Concatenation is very easy to do in String objects
  3. Conversion is made by creating a char array of the size of the command string plus one ending character.
  4. The sketch below is a modification of BLINK sample, that has a counter for how many times loop() function is executed.
  5. The function “dynamicCommand(c)” receives the value of the counter (it could be a sensor reading instead) and generates a command to LINUX system to do a folder listing into a file whose name is crafted based on the cunter value.


// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int c = 0; //just a counter

// the setup routine runs once when you press reset:
void setup() {                
    // initialize the digital pin as an output.
    pinMode(led, OUTPUT);    

// the loop routine runs over and over again forever:
void loop() {
    c++; //un simple contador
    Serial.println("Count: ");  

//  Serial.println("Count: " + c); //This behaves weird...  
    if( c < 10){ 
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);               // wait for a second
    Serial.println("Loop Complete!"); } 

void dynamicCommand(int counter){
     Serial.println("dynamicCommand function..."); 
     String command = "ls > resultFile";
     command += counter;
     command += ".log";
     Serial.println("Concatenated command looks like this:");
//UPDATE: THIS COULD BE ACHIEVED USING THE buffer propery of the String object
//     Serial.println("Converting to char array...");
//     int commandLength = command.length() + 1;
//     char commandCharArray [commandLength];
//     command.toCharArray(commandCharArray, commandLength);
//     Serial.println("Command array looks like this:");
//     Serial.println(commandCharArray);
//     Serial.println("Making system call...");
//     system(commandCharArray);


3,911 total views, 1 views today


3 opiniones en “Dynamic calls to LINUX system from an ARDUINO sketch in Intel Edison – 10 minutes learning”

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

This site uses Akismet to reduce spam. Learn how your comment data is processed.