Blog Detail

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

7 Mar , 2014,
José Núñez
, , , , , , ,
3 comments

IMG_20150307_170030

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.

Enjoy!

// 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);    
    Serial.begin(9600);
    Serial.println("TEST FOR STRING CONCATENATION"); 
}

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

//  Serial.println("Count: " + c); //This behaves weird...  
    
    if( c < 10){ 
         dynamicCommand(c);
    }
    Serial.println("Blinking...");
    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:");
     Serial.println(command);
     
//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);

system(command.buffer);
Serial.println("Done!"); 
} 

2,693 total views, 1 views today

3 Comments

  1. Dynamic calls to LINUX system from an ARDUINO s… Marzo 8, 2015 at 8:58 pm Reply

    […] 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.  […]

  2. Jose Nunez Marzo 9, 2015 at 5:37 pm Reply

    Diego V from Intel Forums just gave us an easier way here:

    https://communities.intel.com/message/283153

    Basically it consists of using the property (not function) .buffer of the String object directly in the system() call.

    Thanks!

  3. Dynamic calls to LINUX system from an ARDUINO s… Marzo 11, 2015 at 10:13 am Reply

    […] RT @diy_nunez: (10min) Dynamic calls to #LINUX system from #ARDUINO sketch in #Intel #Edison#makers #iothttp://t.co/8WZeW4CyRj http://t…&nbsp; […]

Leave A Comment