Tag Archives: char Array

Llamadas dinámicas a LINUX desde ARDUINO IDE en un Intel Edison – Aprendizaje de 10 minutos

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

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

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