IMG_20150307_170030

Uno de los súper poderes de las plataformas Edison y Galileo de Intel es la disponibilidad de LINUX y la posibilidad de ejecutar comandos de LINUX desde un «sketch» de ARDUINO.

En esta receta les comparto cómo hacer llamadas dinámicas al sistema de LINUX desde un sketch ARDUINO, concatenando valores en un objeto String y luego convirtiendo dicho objeto a un arreglo de caracteres necesario para la llamada de sistema.

La idea es poder concatenar comandos de sistema, con valores leidos de los sensores, y hacer llamdas a Linux para que haga algo a partir de estos valores.

En resumen:

  1. La llamada de sistema recibe un parámetro que es un arreglo de caracteres (no un String)
  2. La concatenación se puede hacer de manera muy sencilla usando un String
  3. La conversión se realiza creando un arreglo de caracters del tamaño del String mas un caracter terminador.
  4. El sketch abajo es una modificacion del ejemplo BLINK, que además de hacer parpadear un led, tiene un contador que lleva la cuenta de la cantidad de veces que se ejecuta el comando loop().
  5. La función «dynamicCommand(c)» recibe el valor del contador y genera un comando de sistema para realizar un listado del contenido de una carpeta y poner el resultado en un archivo. Este archivo es nombrado con un sufijo que indica la cuenta que lleva el contador.
// 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!"); } 

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,362 total views,  1 views today

3Shares
Última modificación: marzo 1, 2016

Autor

Comentarios

Diego V de los foros de Intel nos acaba de dar una solución todavía más simple: https://communities.intel.com/message/283153 Basicamente consiste en usar la propiedad «.buffer» (ojo que no es una función) directamente del objet String (command.buffer) en la llamada «system()» ¡Gracias!

Hola: Creo que hay un error en la definicion de la funcion, y ademas en el numero de loops: // 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: » + c); //This behaves weird… if( c 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!»); }

    // 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: » + c); //This behaves weird… if( c 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!»); }

Responder a Jose Nunez Cancelar la respuesta

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.