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!"); 
} 

 8,534 total views,  1 views today

0Shares
Última modificación: junio 20, 2017

Autor

Comentarios

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!

Escribe una respuesta o comentario

Tu dirección de correo electrónico no será publicada.

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.