domingo, 29 de enero de 2012

Email Communication with Arduino (English Version)

Abstract

In the previous chapter we saw how to communicate with the micro using a chatting client. Very useful if you want to execute commands from a mobile client or from another PC, cities away.

In this chapter we will be exploring how to do it by e-mail, which allows both send and receive information from the micro using a very commonly mean used today.

Arduino code:   led-1.pde
Download: SCRIPT
VIDEO:   E-mail communication with Arduino

Communication with the Arduino using the e-mail

The really interesting part of the email is the ability to format the information we sent from the micro to the PC and then to the user. In this way we can create very good-looking reports to communicate the status of, for example,  open status of doors of a house, commercial establishments, factories, offices, etc..

In my search for options that would enable this capability, in a simple way, I found a thunderbird add-on called Mailbox-Alert . This add-on installs very easily (see link) and can configure your thunderbird to identify incoming messages, and then run scripts created by us to perform different tasks.

The information contained in this article applies not only to communicate with a microcontroller, but may be used in different situations to automate tasks as required. For example, if we need to ask our server for some event or information about resource utilization.

Let's see how to configure our components to enable communication via email. Here are the steps to follow to implement this configuration:

  1. Programmingthe microcontroller using the Arduino IDE
  2. Create the script that will send commands through the serial port
  3. Mail-Alert configuration to link a filter to the script indicated below

The flow chart shown indicates that an incoming message is filtered by thunderbird. In the event that the subject of the message do match the conditions specified in the filter, this will trigger a rule defined in the add-on Mail-Alert that will run the script which makes the interface to the serial port and to the arduino. This script is also capable of responding to the original email sender with the actions taken.

(1) Programming the arduino

Like the previous article we used a setting of 4 LEDs connected to the arduino. The firmware (software) that we use is the same as in the previous article which can be downloaded here .

(2) Python script to interact with Arduino

So far we have developed our scripts using ksh, which can run on any linux. To increase portability, in this example we have created a python script which is multiplatform.

This script takes as parameters the subject content of incoming mail, the sender of the email and the serial port with which it is interacting.

This script also interacts with a mail client installed on the PCs. In my case I use ssmtp for its simplicity and effectiveness. As I use Ubuntu as my operating system, ssmtp does the job very well and I point out a link where you can get information on how to install it.


Our script uses ssmtp making a call to the operating system using the subprocess library, which allows a fork of an operating system to execute a command, and we can the parameters to that command. The following excerpt shows the routine to send the mail back to sender.

send_email def (message):
try:
ssmtp = subprocess.Popen (('/ usr / sbin / ssmtp', recipient), stdin = subprocess.PIPE)
except OSError:
print 'I can not begin ssmtp, sothe email was not sent'

# Pass the contents of the mail through stdin
ssmtp.communicate (body% (recipient, sender, message))

# Wait until sending mail
ssmtp.wait ()

Download: SCRIPT

(3) Mail-Alert Settings

This step is one that allows everything to work. When you get a thunderbird mail through their filters, it can redirect it to a rule defined for Mail-Alert, which will execute the script that interacts with the Arduino.

(3.1) Create a Mailbox rule-Alert
(3.2) Create a filter that redirects to the MailboxAlert rule

After setting all the parties involved in the process we can try sending an email to the following features yadirganbot@gmail.com.
  • The message must contain the instructions on the subject
  • The subject must begin with "@, ->" string (without quotes)
  • You can specify more than one command in the subject separated by spaces
  • The commands supported are:
    • ledon, ledoff (Turn on all LEDs)
    • ledNon, ledNoff: (1 <N <4) (Light a LED at a time)
    • titilar (Run all LEDs flashing)
    • secuencia1 (Run an LED chaser from left to right)
    • secuencia2 (Run an LED chaser from right to left)
    • pN, N> 0 integer or decimal (indicating a pause in milliseconds)
    • xN, N> = 1 (to repeat N times a command)
In this example we see how to send a sequence of commands to arduino to yadirganbot@gmail.com.

To: yadirganbot@gmail.com
Subject: @,-> x3 secuencia1 p.5  x3 secuencia2 p.5 titilar p.5 ledon p2 ledoff

Thunderbird, through a filter, detects that sequence @,-> is in the subject of the mail and sends it to the rule created in Mailbox-Alert. Mailbox Alert shows up a pop-up indicating the sender and the message and also run the following command:

/ home/ydirgan/SCRIPTS/mailboxAlert/led-1.py %subject %sender /dev/ttyACM0

in which we are passing as parameters  the email subject, which contains the commands.The sender and finally the serial port where the arduino is connected to.

The script runs led-1.py sending serial commands, logging at led-1.log and sending back a message to the sender via ssmtp.

In the video you can see the life-cycle of communication with the arduino.



No hay comentarios:

Publicar un comentario