domingo, 29 de enero de 2012

Serial Communication with Arduino (English Version)

Abstract


One of the main advantages of a micro is the chance we have of communicating with it via serial port.

In this article, and next we will have an idea of ​​what we can do, and some of the tools I have found interesting to interact with a microcontrollers, involving internet ...

Unless a micro is programmed with a standalone implementation, ie, that its operation is limited to actions that do not depend on external interaction (beyond digital or analog inputs) is not necessary to implement the mechanisms that this article describes. It is true that many applications do not require microcontrollers to get outside the inner environment, but it is increasingly common to think of communication as a tool of interaction with applications designed for these environments. Consider the following example.
...
Arduino microcontroller communication type

If we have a house with panoramic windows (these that open by sliding horizontally), such that a person can enter smoothly through when opened, and we often like to go on vacation, we will have a security problem. The probability of breaking into our home at that time in the house alone is very high.

For someone who wants to design your security system, first of all probably think of placing motion sensors to trigger a very noisy alarm when an intrusion into the residence is detected.This may be a solution, but the alarm will alert only to the immediate surroundings of the house, and intruders.

But we can also think of a silent security system emiting an alarm message to the house's owner via the Internet, sending a text message alert to a receiving means, either email, client chat (IM) or twitter!.

Another example of remote communication is the possibility of creating a system that could  controls lights or appliances in our home. These can be automated from the microcontroller defining scheduled events for turning on or off lights, moving a sun-oriented solar cell efficiently, to power a window for the entry of light according to ambient brightness or just turn on air conditioning in our room near the hour of our arrival.

With this last example, what if we come home early?. We could communicate with our microcontroller using the cellular using our messenger by sending a message like the following:

AC: turn on

In this way, and through the proper software, this message can be received and executed online.

To perform tasks like explained above, we must start from the simple to the complex. The first thing to know is that with microcontrollers like the Arduino, pinguino, teensy, etc.. we can communicate to a server via serial port .

With arduino or teensy serial communication is established via USB interface from which we have to connect the microcontroller to the computer. Once serial port is initialized from the microcontroller environments, linux / unix directory is mapped to / dev / ttyXXXn that will give us the opportunity to send or receive information.

The following code, using Arduino, defines the sequential output of a message to our computer's serial port.

int i = 0;
void setup ()
{
   Serial.begin (9600);
}
void loop ()
{
   Serial.print ("test message");
   Serial.println (++ i);
   delay (1000);
}

In block setup () we initialize the serial port to 9600 baud with Serial.begin (9600). In the block loop () we send the message via serial port followed by a number sequencially updated by ++ i.

From the computer we will see this output if we properly interrogate the port mapping in /dev/ttyACMn.Where n> = 0.

For this example the port was mapped to / dev/ttyACM0. In my case I use ubuntu, but for any Linux / MAC should be very similar.

When the microcontroller is plugged into any USB port on our computer it can be accessed via command line from a terminal or through a program made for this purpose. A third option is to create our own program of reading and / or writing to the serial port. This last option is appropriate if we want to interact withthe microcontroller to get information or instruct it to perform any action.

For our simple example, we can initially examine the serial port using the tool that comes with the Arduino IDE or through gtkterminal or minicom.


From the microcontroller you can also read from the serial port so there is the posibility to make decisions and run actions according to the input.

The following program simply read the serial input and return the string obtained by detecting the end of line.

char character;
byte i = 0;
char string [100];
void setup ()
{
   Serial.begin (9600);
}
void loop ()
{
   if (Serial.available ())
   {
     caracter=Serial.read();
     if (character == 10)
     {
       string [i ++]=' \ 0 ';
       i = 0;
       Serial.println (string);
     }
     else
       string [i ++] = character;
   }
}

In setup() block we initialize the port, then in the loop()  block we check for the port buffer using Serial.available ().

If there are characters available Serial.read sequentially reads each one. If we detect the end of line caracter (10) we add it to the string and reset the counter, sending back the string to the port.

If the character received is not an end of line (10) then we attach it to the end of the string updating the counter.


Now let's see how to use the latter program to bring to power on or off to the air conditioning in the second example shown in this article.

# Include string.h 
# Define ON 1 
# Define OFF 0 
char caracter; 
byte i = 0; 
char string [100]; 
AC_Action void (int action) 
{   
   if (action == ON)   
   { 
     Serial.println ("Turning AC ON ...");
   / / Action to light
   / / Wait for result
       delay (500);
       Serial.println ("Air conditioning was activated ... Status OK");
   }   
   if (action == OFF)   
   {     
     Serial.println ("Turning OFF AC ...");
   / / Shutdown action
   / / Wait for result
       delay (500);
       Serial.println ("Air conditioning was deactivated ... Status OK");
   }
} 
void setup () 
{ 
   Serial.begin (9600); 
} 
void loop () 
{   
   if (Serial.available ())   
   {     
      caracter= Serial.read();
       if (caracter == 10)
       {
           string [i ++]=' \ 0 ';
           i = 0;
           if (strcmp (string, "AC: On"))
             AC_Action (ON);
           if (strcmp (string, "AC: OFF"))
             AC_Action (OFF);
       }
       else
         string [i + +] = caracter;
     }
}

We have created a procedure depending on the action parameter to turn on or off the air conditioning, assuming we have a device that controls the AC connected to the microcontroller. In our case the microcontroller, via serial port, return the message on and off accordingly.

The block loop () maintains the same pattern, except that when you match a specific string runs AC_Action function () passing the parameter set ON or OFF at the beginning of the program.

VIDEO: Serial Communication 3

The next step will be communicating ourselves from outside to PC using several means to interact with the microcontroller.

In future articles we will continue to explore possibilities for communication, interaction and execution.

No hay comentarios:

Publicar un comentario