domingo, 29 de enero de 2012

Communication with Arduino using chat client (english version)

Abstract 

In the previous article we saw the basic theory of how to communicate with the microcontroller using the serial port of your PC.

In this article we look at the basics to make the micro responds to commands sent using an instant messaging client, email, or twitter Pachube. 

Downloads: serialLed.pde , led1log.py , led-1.ksh , external 
VIDEO: Step by Step 

Communication with Arduino using chat client 

We have seen how to send or receive information to and from the microcontroller using the serial port. The next step is to use the micro to react to commands sent using our PC or analyze and react to information received from the micro. 

In a first study how to react to micro according to the orders that we can send to serial port we initially send orders directly through the port by using the direct method, but the idea is to use a interface. 

We will be working with some type of communication interfacing such as a chat client, email, twitter or Pachube to name the four that we will explore in this and the next 3 entries. 

Communication with micro via messenger 

Looking extensively on the Web for free software to use as a chat client I finally found CenterIM . This application is a lightweight chat client, which can be configured with a variety of public accounts as a messenger, yahoo, etc.. 

REF1: CenterIM home 
REF2: Installation CenterIM 

In the previous references  you can access the product home and a very good article on how to install it ubuntu. 

The documentation found in reference 1 is very broad and can be used to initially configure the package. In my case I set it up with an account especially created for this experiment. 

As a chat client, centerIM works like any other messaging client, but with additional features that fits well for our purpose. CenterIM allows us to program scripts to execute tasks according to the content of incoming messages.With this capability you can program different behaviors of how to send commands to the microcontroller via serial port. This allows us to communicate from the messenger to the home computer to perform some tasks such the activation of an alarm, turn on or off lights and appliances, and so on. 

This configuration must be done using a script that should be copied at $HOME/.centerim and whose name must be external. 

The format of the external script is very simple. 

% Action Action to take according to the following conditions 
msg event 
# Reacts only to messages 
proto msn 
# Only for the protocol messenger 
online status 
# It only if the status of the account is online 
stdin stdout options 
# .. external commands (below) read from 
# Stdin, stdout it's output is sent as a response 
# To a remote user 
% Exec 
msg = `cat` 
echo $(/ home/ydirgan/.centerim/commTest.ksh "$msg") 

% Exec redirects the message to a simple script that just returns the message via the same mean.

# CommTest.ksh 
#! /usr/ bin/ksh 
msg = `echo $ 1 | tr az AZ`; 
printf  "arduino-1 command received: $msg"; 

Upon receiving the message, CenterIM script redirects the output to commTest.ksh, it receives the title of the email as a parameter which we convert to uppercase and return it to the stdout via CenterIM (email).
From this simple example we can model a small script to turn on/off 4 LEDs connected to the digital pins 0 .. 3 of our teensy (arduino compatible microctroller).

Here are the steps that we have to follow:
  1. Creation of an arduino script that responds to commands recieved via serial (From the microcontroller)
  2. Creation of a logging script to log serial port output (from the PC)
  3. Creation of a script that is called from "external", which contains the execution logic
  4. Creation of the script "external" to be used by CenterIM
(1) Script for the Microcontroller

The script responds to commands received by the teensy serial port (like the Arduino micro).

The script turns on or off a LED on pin n (1 <= n <= 4). It waits for a character that is available on the serial port.

serial input = 1:LED 1 will be turned on
serial input = 2:LED 1 will be turned off
serial input = 3: LED 2 will be turned on
serial input = 4: LED 2 will be turned off
serial input = 5: 3 LED  will be turned on
serial input = 6: 3 LED will be turned off
serial input = 7: 4 LED  will be turned on
serial input = 8: 4 LED will be turned off

Once you download the program it must be loaded it into the arduino. Once loaded you can test the behavior directly interacting with the serial port, as seen in the video at the end of the article.

Download: serialLed.pde 

(2) logging script 
This script is intended to run on background and the purpose of it is to copy each line of output of /dev/ttyACM0 to a file. 

ydirgan@pragma-systems:~/scripts/$CenterIM led1log.py /dev/ttyACM0 & 
Download: led1log.py 

(3) Script with the execution logic


The third script allows you to execute sequences of commands that needs to be sent to the arduino. With the limited instructions that are defined on the micro script we can actually increase the funcionalities using this script.

In this program we can turn on or off individual LEDs, but also we can light them all, turn off them all, make a blink or running a sequence. All this using the primitives that we have programmed into the micro. 

In addition to the instruction set defined, we have programmed an instruction to obtain information of using by typing HELP from the chat window, and the result becomes: 

arduino-1: This is a finite state machine to interact with an Arduino microcontroller type. Its use is intended only as an example. The microcontroller re-weight when it recognizes the command to execute. They have 4 LEDs on and off each of them. The commands you can use are: ledNon, where n is the number of LEDs (1 <= N <= 4). ledNoff, where n is the number of LEDs (1 <= N <= 4). ledon, all LEDs will light. ledoff, all the LEDs go out. Tilite, all LED's will flash 20 times.stream1, all the LEDs light up in sequence (LED chaser) from left to right. stream2, all the LEDs light up in sequence (LED chaser) from right to left. help shows this help. 

Download: led-1.ksh 

(4) external script to CenterIM 
Finally, the script that runs CenterIM when we send a message. This will trigger the execution of the script led-1.ksh that will be sending our commands to arduino using the serial port. The microcontroller will process it and respond according to the script. 

Download: external 

Finally run CenterIM from our terminal to activate the robot.

The video to show the sequence of steps is as follows:

VIDEO: Step by Step

No hay comentarios:

Publicar un comentario