Showing posts with label POP3. Show all posts
Showing posts with label POP3. Show all posts

Monday, May 3, 2010

Arduino Email Manager - Part 3 - Arduino Code

Now we come to the last part of the Email Manager - the Arduino firmware. Most of it is reasonably straightforward, but there are a few things to note.

To drive the LCD I used the LCD4Bit_mod library recommended to go with the DFRobot LCD shield (available from here). In general this seems to work fine, but it doesn't seems consistent about one line flowing over to the next which is why I have "lcd.cursorTo" lines to put the cursor on the 1st or 2nd line.

The buttons actually are run over Analog pin 0 and the "get_key" function determines the analog value returned and lines it up with the appropriate key number. The trickiest thing for me was figuring the various modes for the buttons in different states - especially the Up/Down buttons that can also be used to delete messages, which is why there are a lot of "if-this-key-and-that-mode-do-this" statements. Likely they could be cleaned up further!

Finding the from/subject info in the data stream proved to be harder than I would have thought as explained in the first post so I ended up using the TextFinder library available from here.  This allows you to search in the data stream (either serial or Ethernet) for keywords or values. Come to think of it, I could probably have used this to do the whole email parsing job and then dispensed with the PHP script, but I noticed when I ran the "getValue" feature it temporarily blacked out the LCD screen - maybe a short processor lock up? Anyway, if anyone can think of a way to use TexFinder to eliminate the PHP script, let me know!

So, here is the Arduino code (with lots of comments) in its glory:

/*
*  Arduino POP Mail Manager
* Uses the DFRobot LCD Shield, Arduino Ethernet Sheild
* and an Arduino Duemilanove to build a simple system for seeing
* how many POP eamils you have and deleting ones you don't want. Separate
* PHP script must be used with it. Complete description at:
* http://opensourceprojects-torchris.blogspot.com/
*
* Uncomment the //Serial lines for troubleshooting/debug info.
*
* This code is in the public domain. Please provide credit if it is used
* in another project.
* 
* written by Chris Armour, Arpil 30th, 2010
*
*/

//=====================Libraries=============/
#include <Ethernet.h>
#include <LCD4Bit_mod.h> 
#include <TextFinder.h>

//===============Set up variables =============/

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address for Arduino
byte ip[] = { 192,168,0,34 }; // IP address you wish to assign to Arduino
byte server[] = { 192, 168, 0, 171 }; // IP address of your PHP server
int ServerPort = 12345;
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
char NumRecd[4];
char SubjRecd[32];
boolean ConnectedState = false;
int MsgNumber = 1;
char TotalMsgNumChar[4] = "   ";
int MsgTotalNum = 1;
char TotalMsgCharAr[2];
boolean DelMode = false;
unsigned long ButtonMillis = 0;
unsigned long CurrentMillis = 0;
unsigned long interval = 15000;

//===============Setup instances===============/

LCD4Bit_mod lcd = LCD4Bit_mod(2); 
Client client(server, ServerPort); 
TextFinder finder( client);

//=================Setup========================/

void setup() { 
  Ethernet.begin(mac, ip);
  lcd.init();
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("POP Mail Started");
  lcd.cursorTo(2,0);
  lcd.printIn("Press S to conct");
  //Serial.begin(9600);
  DelMode = false;
  ConnectedState = false;
}

//=================Main program loop ==================/

void loop() {

  CurrentMillis = millis(); 
  
  if (((CurrentMillis - ButtonMillis) > interval) && (ConnectedState == true)){
    //If connected to the PHP script, then every 15 seconds display the number of emails.
    ButtonMillis = CurrentMillis;
    //Serial.println("15 Seconds have passed!");
    GetTotalMsgs(); //Get total message count
    delay(100);
    PrintTotalMsgs(); //Display total messages
  }
  
  if (((CurrentMillis - ButtonMillis) > interval) && (ConnectedState != true)){
    //If not connected, then just display the not connected error.
    ButtonMillis = CurrentMillis;
    //Serial.println("15 Seconds have passed!");
    NotConnectedError();
  } 
  

//The following items are from the original LCD example and they read the value of the key pressed. 
adc_key_in = analogRead(0);    // read the value from the sensor 

key = get_key(adc_key_in); // convert into key press
   
if (key != oldkey) // if keypress is detected
    {
    delay(70);        // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor 
    key = get_key(adc_key_in);                // convert into key press
    //Serial.println(key);
  
    if (key != oldkey)               
    {           
      oldkey = key;
    }
  }
  
  if (key == 4) {
    //If the Select button is pushed, run the routine to connect to the PHP server.
    ButtonMillis = millis();
       if (client.connect()) {
          char c = client.read();
        if (c == 'C') {
          //Serial.println("connected");
          lcd.clear();
          lcd.printIn("Connected");
          ConnectedState = true; //Sets ConnectedState to true
          delay(200);
        }
         } else {
           //If can't connect, print an error.
          //Serial.println("connection failed");
          ConnectedState = false; // Set connected state to false
          lcd.clear();
          lcd.printIn("Connection Fail");
          lcd.cursorTo(2, 0);  //line=2, x=0 
          lcd.printIn("Check PHP script");

      }
  }
  
  if ((key == 3) && (ConnectedState == true)) {
    //If Right button pressed and it's connected to the PHP script, then dispaly the toal messages.
          ButtonMillis = millis(); //these detect when the button was pushed
          GetTotalMsgs();
          delay(100);
          PrintTotalMsgs();
         }
        
if ((key == 3) && (ConnectedState == false)){
   //Serial.println("Not Connected!!");
   //If ConnectedState is false, print error message.
    NotConnectedError();
    } 


if ((key == 2) && (ConnectedState == false)){
   //Serial.println("Not Connected!!");
    NotConnectedError();
    } 

if ((key == 2)&& (DelMode != true) && (ConnectedState == true)) {
  //If it is connected AND not in delete mode, then display email info
  ButtonMillis = millis();
  GetTotalMsgs();
  delay(200);
  //Serial.println("DN button push detected");
  MsgNumber++; //Increments the messages.
   if (MsgNumber >= MsgTotalNum){ //if we reach the max message number, then go to message #1
   MsgNumber = 1;
    }  
   CallMsg();
   delay(150);
   //Serial.print("Total Messages = ");
   //Serial.println(MsgTotalNum);

}
         
if ((key == 2) && (DelMode == true)&& (ConnectedState == true)){
  //If it is connected, but in Delete mode, then this is the confirm delete button.
  ButtonMillis = millis();
  //Serial.println("DN button push detected");
  //Serial.print("Deleting message number =");
  //Serial.println(MsgNumber);
  DelMode = !DelMode;
  DelMsg();
  lcd.clear();
  lcd.printIn("Message Deleted");
  MsgNumber--;
  delay(250);
  CallMsg();
}

if ((key == 1) && (ConnectedState == false)){
   //Serial.println("Not Connected!!");
    NotConnectedError();
    } 

if ((key == 1)  && (DelMode != true) && (ConnectedState == true)) {
  //If we are connected AND not in delete mode, then disply email info
  ButtonMillis = millis();
  GetTotalMsgs();
  delay(200);
  //Serial.println("UP button push detected");
  MsgNumber--; //decrements the message number
  if (MsgNumber <= 1){ //If we decrement down to 1, then go to the highest message number
     MsgNumber = MsgTotalNum;
   }
   //Serial.print("Total Messages = ");
   //Serial.println(MsgTotalNum);
   delay(150);
   CallMsg();
    }

if ((key == 1) && (DelMode == true)&& (ConnectedState == true)){
  //If we're in delete mode, then this cancels the deletion.
    ButtonMillis = millis();
  //Serial.println("UP button push detected");
  //Serial.print("Cancelling deletion");
  //Serial.println(MsgNumber);
   //Serial.println("DelMode is set to false");
  DelMode = false;
  delay(200);
  lcd.clear();
  lcd.printIn("Delete Cancelled");
  CallMsg();
  }

if ((key == 0) && (ConnectedState == false)){
  //Serial.println("Not Connected!!");
    NotConnectedError();
    } 

if ((key == 0) && (ConnectedState == true)) {
//Finally, if we are connected then this is the delete button.
  DelMode = true; //triggers delete mode
  //Serial.println("DelMode is set to True");
  //prints instructions for deleting or cancelling.
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("Delete Message?");
  lcd.cursorTo(2,0);
  lcd.printIn("UP=N  /  DN=Y");
  }
}

//======================Functions=====================//

// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k; 
    for (k = 0; k < NUM_KEYS; k++)
    {
        if (input < adc_key_val[k])
        {
          return k;
        }
    }   
    if (k >= NUM_KEYS)
        k = -1;     // No valid key pressed  
    return k;
}

//Deletes a message
void DelMsg(){
     //Serial.print("Deleting MsgNumber = ");
     //Serial.println(MsgNumber);
     client.print ("D.");
     client.println(MsgNumber);
     lcd.printIn("Message Deleted");
     delay(150); 
}

//Sends the command to the PHP script to get the from/subject info for the current message number
void CallMsg(){
     //Serial.print("MsgNumber = ");
     //Serial.println(MsgNumber);
     client.print ("S.");
     client.println(MsgNumber);
     delay(150); 
     GetFrmSubj();
}

void GetFrmSubj(){
  //parses out the from/subject info from the PHP return info
     if(finder.find("MSG") == true ){
       //Uses the TextFinder library to locate the from/subject info.
           for (int z = 0; z <= 32; z++) {
             delay(20);
             char c = client.read(); 
             if ((c > 31) && (c < 128)){
             SubjRecd[z] = c;
             }
           }
           Print2LCD();
           client.flush();
     }
     else if (finder.find("MSG") != true) {
       //If the from/subj can't be retreived, then display an error.
       //Usually a transient authentication error on the PHP side.
       //Serial.println("Didn't get it!");
       lcd.clear();
       lcd.printIn("Error getting Msg");  
       client.flush();
     }
}
   
void Print2LCD(){
  //prints the from/subject info to the LCD
  lcd.clear();
  lcd.cursorTo(1,0);
  for (int y = 0; y <= 15; y++) {
    lcd.print(SubjRecd[y]);
    //Serial.print(SubjRecd[y]);
    }
  lcd.cursorTo(2,0);
  //Serial.println();
  for (int z = 16; z <= 32; z++){
    lcd.print(SubjRecd[z]);
    //Serial.print(SubjRecd[z]);
                    }
  client.flush();
}

void GetTotalMsgs () {
  //Gets the total message number, which is stored as both an array & an integer
   client.println("N");
   delay(150);
   if(finder.find("Total Number of Messages:") == true )  {   
     for (int w = 0; w <= 2; w++) {
        delay(20);
        char c = client.read(); 
        if ((c > 31) && (c < 128)){
        TotalMsgCharAr[w] = c;
       }
  }
  MsgTotalNum = atoi(TotalMsgCharAr); //Use ASCII to Integer to convert the array.
  }
}

void NotConnectedError(){
  //Simple error message
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("Not Connected"); 
  lcd.cursorTo(2, 0);  //line=2, x=0 
  lcd.printIn("Press S to conct");
}

void PrintTotalMsgs(){
  //This takes the total message array & prints it to the LCD.
  //Note that is will max out at 99 messages
    lcd.clear();
    lcd.printIn("Number of emails"); 
    lcd.cursorTo(2, 0);  //line=2, x=0 
    //Serial.print("Total messages = ");
    //Serial.println(MsgTotalNum);
       for (int d = 0; d <= 1; d++){
         if ((TotalMsgCharAr[d] >= 48) && (TotalMsgCharAr[d] <= 57)){
         lcd.print(TotalMsgCharAr[d]);
         }
      }
}

So what?


Well, why bother doing all this since I can, of course, much more easily delete spam from my Inbox with a regular email client or my iPhone? Well, as with my previous Arduino/POP3 interfacing project, I like to have a running visual indication of my email load, and, of course, the stock answer is "Why not? It's my time to waste!". Actually, email is quite lightweight an ubiquitious and so could be easily used for remote control. We web-based control would be more elegant, but not always accessible via a remote device for whatever reason.

About 10 years ago I build a device that interfaced with the PC parallel port and then wrote a Visual Basic program that received email and interpreted the subject line to do basic remote control via a couple of solid state relays. It could turn on or off simple AC devices with email and worked like a charm. With an Arduino, you could have a light sensor that then could tell if the light is, in fact, on and send an SMTP message (via the PHP script) to confirm that the action had been taken.

Next steps...


Next up, I should get this properly working with Gmail - possibly using the PHP classes available for IMAP rather than POP3.I also hope to get one of those new-fangled Wi-fi WiShield things and try to take this wireless!

Another future project may be to use the Twilio web-based API for voice-enabling applications to interface to an Arduino (check it out here). I have played with Twilio a bit and using a PHP pre-processor should make it quite easy to interface to an Arduino. This would allow a simple IVR-type remote control & monitoring - "Press 1 to turn on your lights, Press 2 to hear the temperature." I will try and not make it five months between posts!

Arduino Email Manager - Part 2 - The Preprocessor

I freely admit that I am not the greatest programmer in the world. Never having been formally trained, my programs tend to be rather loopy and don't include all the error checking they should. This PHP script that I have written is a case in point. It will work with my POP email provider (Rogers in Canada), but it may NOT work with your provider! It seems like there are minor variations in the way some POP servers will respond to the standard commands and these can throw off the script slightly. The best bet is to log into your POP server via telnet and carefully watch the messages back and forth.

Also note that this will support SSL (if you have it set up with your PHP installation) just by putting "ssl:/" in front of the host name and using whatever port number has been assigned - usually 995.You can manually log into an SSL-enabled account using the OpenSSL client available from OpenSSL.org instead of telnet.

At this point, the script does not support Gmail, so don't write to me about that! I have been trying to get it to work and you can definitely log in, but it seems to give inconsistent results and the mail total includes both the inbox and sent folder somehow (which doesn't make sense to me). If anyone out there knows the trick with Gmail, please let me know. I see that there are some PHP classes available for accessing Gmail via IMAP and I will investigate these and post revised code when I can.

So, how does this script work? The script needs to run on a server with the current version of PHP running. This could be a Mac or a Linux/Windows box or it could be running on a remote webserver - assuming you have all the networking and firewall issues dealt with. There is a new project called Yaler that may help with the networking issues if your script is remotely hosted - check it out here. You need to start the script before pressing the "Connect" (or Select) button on the Arduino.

When running, the Preprocessor is a "host" for the requests from the Arduino, but is in turn acting as a "client" to the POP server. It maintains two separate PHP sockets - one to the Arduino and one to the POP server.



The script implements a very simple protocol with the Arduino:

Arduino Sends...
Preprocessor returns...
Arduino connects to IP/portthe ASCII "C"
NCurrent number of emails on the POP server.
S.x (where x is an integer)The from/subject information for email number "x" prefixed with MSG.
D.x (where x is an integer)Deletes email number "x".
EKills the socket and ends session - currently not implemented.

The script also provides the following error messages back (although I am not currently using these on the Arduino):

  • X.1 - Could not open socket to POP server
  • X.2 - Error from server
  • X.3 - Authentication failure
  • X.4 - Bad connection

I tend to prefer one letter protocols with the Arduino since it simplifies the text handling on the Arduino side.

And, without further ado, here is the code...

<?php

/*
*  PHP Pre-processor script for Arduino POP Mail Manager
*
* this is the pre-processor script for use with the Arduino email
* manager described on my blog. Complete description at:
* http://opensourceprojects-torchris.blogspot.com/
*
* This script can be used with SSL enabled POP services by putting
* "ssl:/" infront of the host name. It does NOT currently work with
* GMail POP service.
*
* This code is in the public domain. Please provide credit if it is used
* in another project.
*
* written by Chris Armour, Arpil 30th, 2010
*
*/

//=============================IP, Port, User Info=================//
//Modify to suit your network.

define("HOST_POP", "your.popserver..com");
define("PORT_POP", "110");
define("USER_POP", "user.name");
define("PASS_POP", "YourPassword");
//Server settings
$host_ard = "192.168.0.171"; //This is the IP of the server running the script.
$port_ard = "12345"; //Port number being used by the Arduino


//==========================Functions===================//

//GetMailCount grabs the number of emails in the mailbox using the STAT command
function GetMailCount()
{
$fp_pop = fsockopen (HOST_POP, PORT_POP, $errno, $errstr);
    // if a handle is not returned
    ob_implicit_flush();
    if (!$fp_pop)
        {
        echo("Error: could not open socket connection\n");
        return("X.1");
        }
    else
        {
        // get the welcome message
        $welcome = fgets ($fp_pop, 150);
        // check for success code
        if (substr($welcome, 0, 3) == "+OK")
        {
        // send username and read response
    fputs ($fp_pop, "user " . USER_POP . "\n");
    //fgets($fp_pop, 50);
    // send password and read response
    fputs ($fp_pop, "pass " . PASS_POP . "\n");
    $ack = fgets($fp_pop, 50);
    // check for success code
    if (substr($ack, 0, 3) == "+OK")
        {
        // send status request and read response
        fputs ($fp_pop, "STAT\n");
        $status = fgets($fp_pop, 50);
    if (substr($status, 0, 3) == "+OK")
        {
        // shut down connection
        fputs ($fp_pop, "QUIT\n");
        fclose ($fp_pop);
        }
// error getting status
    else  {
        echo ("Error - server said: $status");
        return("X.2");
        }
        }
    // auth failure
    else  {
        echo ("Error - server said: $ack");
        return("X.3");
        }
        }
        // bad welcome message
    else {
        echo ("Error - bad connection string\n");
        return("X.4");
        }
// get status string
// split by spaces
$arr = explode(" ", $status);
// the second element contains the total number of messages
echo $arr[3] . " messages in mailbox\n";
$GotMail = $arr[3];
return $GotMail;
}
}

//GetFromSubj grabs the content of the email MailNumber then passes this back to the main loop for extraction of the From & subject
//You may need to check the output from your POP3 server using telnet to check on the responses. This seems to vary from POP server to POP
//server.
function GetFromSubj($MailNumber){
$fp_pop = fsockopen (HOST_POP, PORT_POP, $errno, $errstr);
    // if a handle is not returned
    ob_implicit_flush();
    if (!$fp_pop)
        {
        echo("Error: could not open socket connection\n");
        return("X.1");
        }
    else
        {
        // get the welcome message
        $welcome = fgets ($fp_pop, 150);
        // check for success code
        if (substr($welcome, 0, 3) == "+OK")
        {
        // send username and read response
    fputs ($fp_pop, "user " . USER_POP . "\n");
    fgets($fp_pop);
    // send password and read response
    fputs ($fp_pop, "pass " . PASS_POP . "\n");
   $ack = fgets($fp_pop);
    // check for success code
    if (substr($ack, 0, 3) == "+OK")
       {
        // send request for email content & read response

        fputs ($fp_pop, "retr " . $MailNumber . "\n");
        $EmailContent = fread($fp_pop, 4096);
//        echo $EmailContent;
    if (substr($EmailContent, 0, 3) == "+OK")
        {
        // shut down connection
        return $EmailContent;
        fputs ($fp_pop, "QUIT\n");
        fclose ($fp_pop);
        }
// error getting status
    else
        {
        echo ("Error - Server said: $EmailContent");
        return("X.2");
        }
        }
    // auth failure
    else
        {
        echo ("Error - Server said: $ack");
        return("X.3");
        }
        }
        // bad welcome message
    else
        {
        echo ("Error - Bad connection string\n");
        return("X.4");
        }
}
}

//MsgDelete delets message # MailNumber using the dele command
function MsgDelete($MailNumber){

$fp_pop = fsockopen (HOST_POP, PORT_POP, $errno, $errstr);
    // if a handle is not returned
    ob_implicit_flush();
    if (!$fp_pop)
        {
        echo("Error: could not open socket connection\n");
        return("X.1");
        }
    else
        {
        // get the welcome message
        $welcome = fgets ($fp_pop, 150);
        // check for success code
        if (substr($welcome, 0, 3) == "+OK")
        {
        // send username and read response
    fputs ($fp_pop, "user " . USER_POP . "\n");
    fgets($fp_pop);
    // send password and read response
    fputs ($fp_pop, "pass " . PASS_POP . "\n");
   $ack = fgets($fp_pop);
    // check for success code
    if (substr($ack, 0, 3) == "+OK")
       {
        // send delete command

        fputs ($fp_pop, "dele " . $MailNumber . "\n");
        $DeleteAck = fgets($fp_pop);
//        echo $EmailContent;
    if (substr($DeleteAck, 0, 3) == "+OK")
        {
        // shut down connection
        echo ("Message " . $MailNumber . " deleted. \n");
        fputs ($fp_pop, "QUIT\n");
        fclose ($fp_pop);
        }
// error getting status
   else
        {
        echo ("Error - Server said: $EmailContent");
        return ("X.2");
        }
        }
    // auth failure
    else
        {
        echo ("Error - Server said: $ack");
        return("X.3");
        }
        }
        // bad welcome message
    else
        {
        echo ("Error - Bad connection string\n");
        return("X.4");
        }
        }
}

//=====================Create Socket to listen for Arduino======================//

ob_implicit_flush();

//Open socket to listen for Arduino
$socket_ard = socket_create(AF_INET, SOCK_STREAM, 0) or die ("Could not bind to socket\n");
$result_ard = socket_bind($socket_ard, $host_ard, $port_ard) or die("Could not bind to socket\n");

// start listening for connections
$result_ard = socket_listen($socket_ard, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket_ard) or die("Could not accept incoming connection\n");
// read client input
socket_write($spawn,"C\n");
echo "Device connected.\n";

//=====================Main Socket loop======================//
do {

$input = socket_read($spawn, 2048, PHP_NORMAL_READ) or die("Could not read input\n");

echo $input;

if (trim($input[0]) == "N" || trim($input[0]) == "S" || trim($input[0]) == "D" || trim($input[0]) == "E" || trim($input[0]) == "\r\n") {
//Only do anything if N, S, D or E received.

    if (trim($input) == "N"){
        // This gets the mailcount using the GetMailCount function.
        $MailCount = GetMailCount();
        socket_write($spawn, "Total Number of Messages:" . $MailCount . "@");
        }

    if (trim($input[0]) == "S") {
        //This gets the from/subject info is an S.num is received.
       $MsgNum = explode (".",$input);
       //Use explode to break the info from the Arduino into an array.
        $MsgNumber = $MsgNum[1];
        //Get the Message number from the array.
        echo ("Value of MsgNumber= " . $MsgNumber . "\n");
        $MsgFromSubj = GetFromSubj($MsgNumber);
        //run the function to get the info from the POP server
            if ($MsgFromSubj[0] == "X")
            {
            //If there's an error, write back the server error text.
                socket_write($spawn,$MsgFromSubj);
            }
        else
            {
            $MsgFromLocn = strpos($MsgFromSubj, "From: ");
            $MsgFromName = substr($MsgFromSubj, ($MsgFromLocn + 6), 14);
            print ( "MSG". "F:" . $MsgFromName );
            $MsgSubjLocn = strpos($MsgFromSubj, "Subject: ");
            $MsgSubj = substr($MsgFromSubj, ($MsgSubjLocn + 9), 14);
            echo ("S:" . $MsgSubj );
            usleep(20000);
            socket_write ($spawn,  "MSG" . "F:" . $MsgFromName . "S:" . $MsgSubj . "\n");
            }
        }

    if (trim($input[0]) == "D") {
    //This sends the command to delete an email.
       $MsgNum = explode (".",$input);
        $MsgNumber = $MsgNum[1];
        $MsgDelRet = MsgDelete($MsgNumber);
        if ($MsgDelRet[0] == "X")
            {
                socket_write($spawn,$MsgDelRet ."\n");
            }
        else
            {
        socket_write($spawn, "D." . $MsgNum[1]);
        }
    }

    if (trim($input) == "E"){
  //This isn't actually currently used by the Arduino.
        socket_shutdown($spawn, 2);
        usleep(1000);
        socket_close($spawn);
        socket_shutdown($socket_ard, 2);
        usleep(1000);
        socket_close($socket_ard);
        echo "Sockets terminated\n";
 //       break;
        }
}

} while(true);

?>

Note that this code does not provide good handling of disconnect/reconnect nor can it handle multiple connections simultaneously. In other words, if the Arduino is reset, then the script needs to be restarted. Also, if multiple connection requests are received, it will just die. If anyone can improve on this, please make the suggestions.

Next up will be the Arduino code!

Saturday, May 1, 2010

Arduino Email Manager - Part 1 - Overview

I always seem to start every blog entry moaning about how long it's been since the last project I posted and this one is no exception. I think writing this is pushing me to do more complex projects and what with having a life and all, it just takes a long time to get everything done. This project in particular turned out to be quite a bit more difficult than I thought it would be and it has ended up taking me months and months to work out all the kinks. Because of that, I will be breaking up the project description into a couple of entries. This first one will just introduce the project.

First the obligatory video!



As explained in the video, this project uses three easily available Arduino components to allow you to:

  • See how many emails you have in your POP3 email account
  • Review the "from" and "subject" of your email
  • Delete unwanted emails

Here is what it looks like assembled:





The system is a sandwich of..

  • Arduino Duemilanova
  • Arduino Ethernet Shield
  • DFRobot LCD/button shield (available here from Robotshop among others)

I also used some Shield Stacking Headers (available from Adafruit) since the LCD shield wouldn't clear the Ethernet jack on the network shield. Here it is disassembled:





Getting the LCD Shield to work with the Ethernet Shield was a time consuming process. Initially, it looked like there would be no pin conflicts, since the Ethernet Shield uses pins 10, 11, 12 & 13 and the LCD Shield in theory uses 5,6,7,8 and 9, but for whatever reason I just couldn't make it work. I did finally find a blog posting by David Delabassee (here) that pointed out that, in fact, the LCD Shield uses more pins than is documented and shows how to modify LCD4Bit_mod.cpp to remap the pins slightly. Very useful, but it still seemed to blank out for some reason.

So, I ended up testing every single pin combination (which took hours) and I finally determined that if the LCD Shield was on digital pin 13, it just wouldn't work for whatever reason (even though, in theory, that pin is not used by the shield). So, just bending back that leg on the LCD Shield took pin 13 out of the circuit and it now works fine.

I had also wanted to add in one or two LEDs to signal the message count or connected state, but for whatever reason again when I added them in, it just wouldn't work consistently. If anyone has any ideas on how to fix that it would be appreciated!

As explained in the video, I have used the five buttons available on the LCD shield to page through the emails and delete any that I don't want.




I will post the software in subsequent entries, but for now I will just explain that I decided to go with using a mail "pre-processor" application written in PHP. Originally I wanted to do everything on the Arduino so it would be a complete, stand-alone solution, but, alas, it just proved to be too hard for the Arduino's limited text processing abilities and my small brain. Basically, even with the Duemilanova based on the ATMEGA328, you really can't load a string array with more than a couple of hundred characters before the Arduino locks up. Unfortunately, you need to look through at least the first 1,500 - 2,000 characters to get the "from" and "subject" info I wanted. I looking into using PROGMEM, but I just couldn't get my head around it (looks like all that fancy "pointer" stuff I can never figure out!). Then there's the Arduiniana Flash library, but it seems limited to strings you load at the beginning, rather than dynamic strings.

Finally, I just decided to go with a pre-processor written in PHP. I already know enough PHP to be functional and it is excellent for text processing. When I went to figure out how to do sockets programming with PHP, the first tutorial I found was on writing a script to access a POP3 email server (check it out)! Using a pre-processor isn't cheating too much. Tom Igoe uses one in Making Things Talk for his "Networked Air Quality Meter" project. Of course, on a much vaster scale, that is essentially how the RIM Blackberry email system works - emails are routed through their preprocessor software which compresses and reroutes them them down to the smartphone.

That is enough for now. Next up will be the Arduino software & PHP script.

Tuesday, September 8, 2009

Further on the POP3 email checker

Well, since this project has been a bit of a surpirse hit, I thought I would include a few further notes and refinements.

This is a small change to the updateClient function that just flashes the LED rapidly four times to indicate the network is down:


void updateClient() //This function contacts the POP3 server
{
if ((millis() - updateTimer) > 5000)
{
Ethernet.begin(mac, ip);
// Serial.println("connecting...");
delay(1000);
if (client.connect())
{
// Serial.println("connected");
client.println("user Pop.User"); //Insert your usual email login name
client.println("pass YourPassword"); //And your password here
client.println("quit");
client.println();
clientConnected = true;
}
else
{
// Serial.println("connection failed");
// Flash four time rapidly to indicate network down.
for (int x = 0; x < 4; x++){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
updateTimer = millis();
}
}


What is odd is that when I have tried this with another LED it blinks very dimly - even when I move around which digital pin the other LED is coming from. Very odd and I still haven't figured out what is causing that.

Another thing to watch for is that this assumes that the number of emails comes through in array position 106 & 107 (I then subtract 48 to make the ASCII code into an integer):


mailNum1 = inString[106] - 48; //Array position 106 contains the first digit
mailNum2 = inString[107] - 48; //Array position 107 contains the 2nd digit if it is available


This, of course, may vary depending on how many characters there are in your POP3 server name and so on. I would recommend starting with the basic commands for getting the POP3 string back:


#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,172 };
byte server[] = { XXX, XXX, XXX, XXX }; // IP address of your POP3 server

Client client(server, 110);

long updateTimer;
boolean clientConnected = false;

void setup()
{
Serial.begin(9600);
}

void loop()
{
updateClient();
checkAvail();
}

void updateClient()
{
if ((millis() - updateTimer) > 10000)
{
Ethernet.begin(mac, ip);
Serial.println("connecting...");
delay(1000);
if (client.connect())
{
Serial.println("connected");
client.println("user user.name"); //Insert your usual email login name
client.println("pass YourEmailPassword"); //And your password here
client.println("quit");
client.println();
clientConnected = true;
}
else
{
Serial.println("connection failed");
}
updateTimer = millis();
}
}

void checkAvail()
{
if (clientConnected)
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
clientConnected = false;
}
}
}



Then watching the output in the serial window, which will look something like:


connecting...
connected
+OK hello from popgate 2.43 on pop108.xxx.xxx.xxx.xxx.xxx
+OK password required.
+OK maildrop ready, 0 messages (0 octets) (16335883)
+OK server signing off.

disconnecting.


You can then use this output to figure out the right position in the array for mailNum1 & mailNum2.

Hope that helps someone out there. It has been very flattering to see how many folks are interested in building this for themselves. As I explained in the first post, once you have the raw number of emails as an integer you can process with Arduino, you can do all sorts of interesting things beyond just flashing and LED!

Thursday, September 3, 2009

Arduino POP3 Email Checker

Since it turns out people actually occasionally READ this blog, I decided it was time to go back to do an Arduino project and try something different. Since I hadn't done any Arduino work with the Internet before, I decided to try something I thought would be relatively simple - having the Arduino check my email and give me some sort of visual indication of how many emails I have. I started out with:
Both were bought from the good people at Robotshop.ca.




These are just combined with a high intensity LED - which really should have a resistor and will have one eventually!



Here is a quick video - sorry it is a bit murky and the LED is a bit bright!



This was actually probably the hardest Arduino code I ever wrote! For one thing, I hadn't done anything substantial with the Arduino for a while and I found I had forgotten much of what I thought I knew. Also, getting the timing right and getting the number of emails out of the return string actually proved pretty difficult. I made things more difficult for myself by trying to use the String library (formerly TextString), which for some reason didn't return consistent results and was generally not documented and finicky. Then I found the usual LED 13 wouldn't work properly - perhaps because the Ethernet Shield was interfering with it. All-in-all, this small program must have taken me three weeks to write!

I did have a bit of help with the code from Digger450 on the Arduino forum in this exchange, which I am very grateful for!

However, now that it is done, this is a nice little demo of Ardunio on the Internet that does do something at least semi-useful. My next extension may be to hook it up to a servo so that it shows my emails on a physical chart or something. I could also use my SparkFun SerialLCD unit to display the subject lines or something.

Here is the source code:

/*=================================================
Ethernet POP3 Mail Checker & indicator

Checks how many messages are waiting on the POP 3 server
and flashed LED on Pin 9 to indicate number of messages.

It will handle up to 99 messages in the POP3 mailbox.

Uncomment the serial lines for troubleshooting.

Copyright by Chris Armour
3 September 2009
http://opensourceprojects-torchris.blogspot.com/

===================================================*/

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,167 }; // IP address you wish to assign to Arduino
byte server[] = { XXX, XXX, XXX, XXX }; // IP address of your POP3 server
char inString[165]; // Number of characters to be collected
int i = 0;
int mailNum1 = 0; // First digit of the email number
int mailNum2 = 0; // Second digit
int mailTotal = 0; // Total # of messsage
char d;
int ledPin = 9;

Client client(server, 110); //The default POP port is 110

long updateTimer;
boolean clientConnected = false;

void setup()
{
//  Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}

void loop()
{
   
updateClient();
d = checkAvail();
if (d >= 10){
getMailNum();
}

}

/*========================================
        Functions
=========================================*/

void updateClient() //This function contacts the POP3 server
{
if ((millis() - updateTimer) > 5000)
{
 Ethernet.begin(mac, ip);
//    Serial.println("connecting...");
 delay(1000);
 if (client.connect())
 {
//    Serial.println("connected");
 client.println("user Your.Name"); //Insert your usual email login name
 client.println("pass PassWord"); //And your password here
 client.println("quit");
 client.println();
 clientConnected = true;
 }
 else
 {
//    Serial.println("connection failed");
 }
 updateTimer = millis();
}
}

char checkAvail() //This checks if there is data available and returns a char
{
if (clientConnected)
{
 if (client.available())
 {
 char c = client.read();
     return(c);
 }
 if (!client.connected())
 {
//    Serial.println();
//    Serial.println("disconnecting.");
 client.stop();
 clientConnected = false;
 }
}
}

int getMailNum() //This actually loads the char returned by checkAvail() and puts in into an array
{
inString[i] = d;
i++;
if (i == 165){
   i = 0;
       client.flush();
       mailNum1 = inString[106] - 48; //Array position 106 contains the first digit
       mailNum2 = inString[107] - 48; //Array position 107 contains the 2nd digit if it is available
       if ((mailNum2 >= 0) && (mailNum2 <= 9)){ //If mailNum2 is present, then it is a two digit mail count
         mailTotal = (mailNum1 * 10) + mailNum2; //when 2 digits are present, multiply the 1st by 10 then add to mailTotal
//            Serial.print("Total emails:  ");
//            Serial.println(mailTotal);
          blinkLED(); //Run the blink function as many times as there are emails
       }
       else {
         if ((mailNum1 >= 0) && (mailNum1 <= 9)){//if there is only one digit, then that is mailTotal
         mailTotal = mailNum1;
//        Serial.print("Total emails:  ");
//        Serial.println(mailTotal);
         blinkLED(); //Blink the LED
         }
         }
     }
 }

void blinkLED(){ //Blinks the LED for as many times as indicated by mailTotal
         for(int x = mailTotal; x >= 1; x--){
           digitalWrite(ledPin, HIGH);
           delay(200);             
           digitalWrite(ledPin, LOW);
           delay(200);
         }
}