Go Down

Topic: Sending Mail under condition (Read 1 time) previous topic - next topic

EATYone

Hi,

After some search, i've never found any exemple of sending mail under condition with an Arduino. After some test, now i know how to do it. So hope this can help some of you i post my code. This code is based on http://arduino.cc/playground/Italiano/Mail . I've plug an analog sensor, and when this one is upper than 1000, i'll send a mail:

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

// Local network configuration:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x23, 0x5D };
byte ip[] = { 192,168,0, 177 }; <---Your arduino ip
byte gateway[] = { x, x, x, x}; <-- gateway if needed
byte submask[] = { x, x, x, x}; <-- submask if needed



// SMTP server data:
byte server[] = { x, x, x, x }; // SMTP server
String ServerName = "xxxxxx";

// Mail data:
String Sender = "xxx@xxx.fr";
String Recipient = "xxxxxxx@gmail.com";
String Subject = "Un message d'arduino!";
String Body = "Je teste les mails.";

int time = 1000;
int wait = 200;
boolean hasSentMail = false;
String ServerResponse="";
Client client(server, 25);

void setup()
{
    Serial.begin(9600);
    Serial.println("Program started, waiting for router...");
    delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */
 
    Serial.println("Starting network module...");
    Ethernet.begin(mac, ip, gateway, submask);
    delay(2000);
}

void loop()
{
     int potarValue = analogRead(A0);
     Serial.println(potarValue);
     
     if (potarValue > 1000 && !hasSentMail)
     {
          sendMail();
          hasSentMail = true;
     }
     
     if (potarValue <= 1000)
          hasSentMail = false;
     
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

}

void sendMail()
{
     Serial.println("connecting...");
     
     if (client.connect())
     {
         Serial.println("connected");
   
         SendMsg("HELO " + ServerName); /* say hello*/
         SendMsg("MAIL From:<" + Sender +">"); /* identify sender */
         SendMsg("RCPT To:" + Recipient + ">"); /* identify recipient */
         SendMsg("DATA");
         SendMsg("To: " + Recipient); /*  recipient in message header */
         SendMsg("From: " + Sender); /* seder name in message header */
         SendMsg("Subject: "+ Subject); /* insert subject */
         SendMsg(""); /* empty line */
         SendMsg(Body); /* insert body */
         SendMsg(""); /* empty line */
         SendMsg("."); /* end mail */
         SendMsg("QUIT"); /* terminate connection */
         client.println();
         client.stop();
      }
      else
      {
           Serial.println("connection failed");
      }
}

void GetResponse()
{
    if (client.available())
    {
         char c = client.read();
         while (client.available())
         { // Store command char by char.
           ServerResponse +=c;
           c = client.read();
         }
    Serial.println("<<<" + ServerResponse);
    ServerResponse="";
  }
}

void SendMsg(String m) {
   client.println(m);
   Serial.println(">>>" + m);
   delay(wait); /* wait for a response */
   GetResponse();
}



:smiley-mr-green:

SurferTim

#1
Oct 20, 2011, 12:23 pm Last Edit: Oct 20, 2011, 03:33 pm by SurferTim Reason: 1
You will run into problems if you do not wait for the server to respond, and check the response after every send.
This has better error checking. Check the code for remarks and edit the values to your settings.
Entering an 'e' and enter key on the serial monitor sends the email.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Change these next 4 entries to your network settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
byte ip[] = { 192, 168, 2, 2 };    
byte gateway[] = { 192, 168, 2, 1 };
byte subnet[] = { 255, 255, 255, 0 };

// gmail.com email server
byte server[] = { 74, 125, 65, 27 };

Client client(server,25);

void setup()
{
 Serial.begin(9600);
// If you are using a shield without a microSD card reader
// comment out the next two lines
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

 Ethernet.begin(mac, ip, gateway, subnet);
 delay(2000);
 Serial.println("Ready");
}

void loop()
{
 byte inChar;

 inChar = Serial.read();

 if(inChar == 'e')
 {
     if(sendEmail()) Serial.println("Email sent");
     else Serial.println("Email failed");
 }
}

byte sendEmail()
{
 byte thisByte = 0;
 byte respCode;
 
 if (client.connect()) {
   Serial.println("connected");
 } else {
   Serial.println("connection failed");
   return 0;
 }

 while(!client.available()) delay(1);

 respCode = client.peek();
 
 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }
 
// replace with your public ip
 client.write("helo 12.34.56.78\r\n");
 
 while(!client.available()) delay(1);

 respCode = client.peek();

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }
 
// replace with a valid from address
 client.write("MAIL From: <user@yourdomain.com>\r\n");
 
 while(!client.available()) delay(1);

 respCode = client.peek();

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }
 
// Replace with the recipient address
// This must be a gmail.com domain to work if you are using Gmail
 client.write("RCPT To: <user@gmail.com>\r\n");

 while(!client.available()) delay(1);

 respCode = client.peek();
 
 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }
 
 client.write("DATA\r\n");

 while(!client.available()) delay(1);

 respCode = client.peek();

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }

// Replace with the recipient address as above  
 client.write("To: Me <user@gmail.com>\r\n");

// Replace with the sender address above
 client.write("From: Me <user@mydomain.com>\r\n");
 
 client.write("Subject: email test\r\n");
 
 client.write("Email test.\r\n");
 
 client.write(".\r\n");

 while(!client.available()) delay(1);

 respCode = client.peek();

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }
 
 client.write("QUIT\r\n");

 while(!client.available()) delay(1);

 respCode = client.peek();

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 if(respCode >= '4')
 {
   efail();
   return 0;  
 }

 client.stop();
 
 Serial.println("disconnected");
 
 return 1;
}

void efail()
{
 byte thisByte = 0;

 client.write("QUIT\r\n");

 while(!client.available()) delay(1);

 while(client.available())
 {  
   thisByte = client.read();    
   Serial.print(thisByte);
 }

 client.stop();
 
 Serial.println("disconnected");
 
}


Edit: I added a change I learned the hard way. It is just before the Ethernet.begin() call.
If you have a ethernet shield with a microSD card slot, you must disable the SD card reader SPI interface (SS is pin 4) or you are going to have trouble someday with a SPI collision between the w5100 and the SD card reader.

EATYone

Thanks for those advices! certainly better than my code (i'm not programmer :smiley-mr-green:)

zoomkat

The below trimmed down code works with my ISP's email server. The 10 sec delay at the end probably could be shortned. As usual YMMV!

Code: [Select]

/*
SEND AN EMAIL WITH ARDUINO
9/23/11
This code was created by modifying the connect example from arduino.cc
and with the help of the YABB forums and their very helpful members.
This code sends an email to any email address and then disconnects.
*/

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Arduino's artificial mac address
byte ip[] = { 192, 168, 1, 102 }; // my ip
byte server[] = { xxx, yyy, zzz, aaa }; // my smtp.xxx.net server ip

Client client(server, 25);

void setup()
{
  Ethernet.begin(mac, ip);
Serial.begin(9600);

Serial.println("connecting...");

if (client.connect()) {
                        Serial.println("connected");
   
                        client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/

                        client.println("MAIL From: me@my-isp.net"); /* identify sender, this should be the same as the smtp server you are using */

                        client.println("RCPT To: you@your-isp.net"); /* identify recipient */

                        client.println("DATA");

                        client.println("To: you@your-isp.net"); /* identify recipient */
                        client.println("Subject: You Have Arduino Mail!!"); /* insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body */
                        client.println("."); /* end email */
                 
                        client.println("QUIT"); /* terminate connection */
                       
                        client.println();
   
} else {
   Serial.println("connection failed");
}
}

void loop()
{
   delay(10000);
   if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
}
}
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up