TO SEND MAIL TO ARDUINO

i would like mail to arduino...how can i do? :-/

Well, you could do something like this:

Create a free mail account (like Gmail or something) and make Arduino check the POP3-Server every x minutes (or seconds). If new mail arrives, read it, act accordingly, delete it. Done.

You'll need an Ethernet Shield to do so (or any other possibility of connecting to the internet/network). Sample code can be found on the net, just search Google or check the Arduino Playground.

Have fun!

This is my code…arduino in this program connects to server gmail…now i don’t know to send mail to arduino…how can i do? how to configure it for download mail?

#include <Ethernet.h>
int ledPIN=9;
char string[165];
int i=0;

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 10, 0, 0, 5};
byte server={74, 125, 43, 109};

Client client(server,993);

void setup()
{
pinMode(ledPIN,OUTPUT);
delay(1000);
Ethernet.begin(mac,ip);
Serial.begin(9600);
delay(1000);
Serial.println(“connecting…”);

if(client.connect()){

Serial.println(“connected”);
client.println(“Utente:username@gmail.com”);
client.println(“Password:psw”);
client.println();
}
}

void loop()
{
if (client.available()){
char c=client.read();
Serial.print(c);
string*=c;*

  • i++;*
    }

You could do the same as any email-client (e.g. Thunderbird, Outlook, ...) and send the appropriate POP3-commands to the mail server (or the appropriate commands for whatever protocol that server uses).

Info about POP3: http://en.wikipedia.org/wiki/Post_Office_Protocol

If it is legal where you live, you could capture (e.g. using Wireshark) what your email client sends to the mail server when you make it fetch your unread emails.

This is what I would do (I'm not using working code here):

  • Login to the mail server using the USER and PASS command
  • Retrieve count of unread mails using the LIST command
  • In range 1..[result of LIST], use the RETRx command (x being an integer in range) to retrieve mail x
  • Use the DELEx command
  • Logout using the QUIT command

That is - basically - how POP3 works.

Good luck!

Greets - Steps

well, i relaized this code… in this code i would like the led turning on only if are present messages in the mail box…i used a string for to store the answers sent by server.
The server answer me with following string: Code:+OK #n bytes

where n is number of messages that are present in the my mail box.
if n=1 then i would like that led turning on, but in the my code has encountered an error:

#include <Ethernet.h>


int ledPIN=9;
int statusPIN=0;
int startTime=0;
char String[20];
int i=0;

 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 5};
byte server[]={85,33,2,56};


Client client(server,110);

void setup()
{
pinMode(ledPIN,OUTPUT);
delay(1000);
Ethernet.begin(mac,ip);
Serial.begin(9600);

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

}


void loop()
{
  
 statusPIN=LOW;
 if(client.connect()){
 
 
 
  Serial.println("connected");
 
  
 
  client.println("USER username@alice.it");
  client.println("PASS psw");
 
 
  client.println("LIST 1");
   
   
  
  client.println("QUIT");

  client.println();
 
 
 }
 
   if (client.available()){
     
   for(int i=0; i<20; i++){
   char String[20]=client.read();
   Serial.println(String[i]);
   }
   
  
   if(String[3]=1){
   statusPIN=HIGH;      
   digitalWrite(ledPIN,statusPIN);
   }
}


 }

error detected is :“array must be initialized with a brace enclosed initializer.”

Hi!

You should take another look at the Arduino language, I guess.

First thing I saw (besides missing indentation) is this line:

if(String[3]=1){

To compare two values, use the == operator, not =.

By the single equals sign, you assign the value 1 to String3. Try to change that and verify the code again.

If the error remains (or changes), just try to comment out one line after another to find the line that contains the error.

As I'm writing this, I think that you should simply delete the global variable "String". You are initializing it down in the if(client.available())-part, right?

And maybe you want to replace your client-reading by something like this:

if(client.available())
{
    char c = client.read();
    Serial.print(c);
}

Hope I could help you a bit (just woke up, so sorry for any weird words/grammar).

Greets - Steps

i would like the led turning on only if are present messages in the mail box...how can i do? :-/

Assuming that "newMails" is a boolean variable, you could do something like that:

if(newMails)
{
    digitalWrite(ledPinNumber, HIGH);
}
else
{
    digitalWrite(ledPinNumber, LOW);
}

(You want to that from inside of the loop() method, of course)

Of course, you'll need to check whether or not new mails have arrived (you seem to be quite close to that) and assign true or false to "newMails" accordingly.

Greets - Steps

i have got a big problem…the server answer me in any case…

[include <Ethernet.h>


int ledPIN=9;
int statusPIN=0;
int startTime=0;
char String[20]="0";

 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 5};
byte server[]={85,33,2,56};


Client client(server,110);

void setup()
{
pinMode(ledPIN,OUTPUT);
delay(1000);
Ethernet.begin(mac,ip);
Serial.begin(9600);

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

}


void loop()
{
  
 statusPIN=LOW;
 if(client.connect()){
 
 
 
  Serial.println("connected");
 
  
 
  client.println("USER username@alice.it");
  client.println("PASS psw");
 
 
  client.println("LIST 1");
   
   
  
  client.println("QUIT");

  client.println();
 
 
 }
 
   if (client.available()){
   char c=client.read();
   Serial.print(c);
   
   
  
   
   statusPIN=HIGH;      
   digitalWrite(ledPIN,statusPIN);
   }
}


/code]

if are present one mail the server answer me with:
+OK Microsoft Exchange Server 2003 POP3 server version  ready.
+OK
+OK User successfully logged on
+OK 1 8789
+OK  Microsoft Exchange Server 2003 POP3 server version  signing off.

while if not are mail :

+OK Microsoft Exchange Server 2003 POP3 server version  ready.
+OK User successfully logged on
-ERR The specified messege si out of range

+OK  Microsoft Exchange Server 2003 POP3 server version  signing off
-ERR Protocol Error

If you read the article http://en.wikipedia.org/wiki/Post_Office_Protocol you'll recognize that you should use

client.println("LIST");

instead of

client.println("LIST 1");

(In case that MS' server does follow default POP3)

Maybe that will solve your problem?

Maybe you misunderstand the server's reply:

+OK Microsoft Exchange Server 2003 POP3 server version  ready.
+OK
+OK User successfully logged on
+OK 1 8789
+OK  Microsoft Exchange Server 2003 POP3 server version  signing off.

Line 1: Connection established Line 2: OK Line 3: Successfully logged on Line 4: There's 1 message (8789 bytes) Line 5: Logout

That means: You'll have to send RETR 1 to the server to retrieve the first (and in this case only) email. In case there are 2 or more mails, you'll have to retrieve 2, 3, ..., n.

Good luck

Greets - Steps

the string "+OK 1 8789" , i must to store it because arduino can controls if n=1 or not....?

I'm not sure whether or not I'm getting you right here.

If you mean that this is the interesting string in the server's reply and you should act according to it's contents, you're right. You can do something like this:

Take a look at the string. The first 4 bytes (characters) can be skipped ("+OK ", including the trailing space after OK). Now you'll have to check, how many bytes there are before the next space (in case there are more than 9 mails, so simply using 1 byte won't work). You could do that in a loop. After you encountered the next space, the characters between byte 4 and x are the bytes containing the amount of mails on the server.

 (NOT WORKING CODE)
Define an offset (where to start) of value 4 (as the fifth byte is the first you care about)
Define a buffer char array (length 5 should be enough)
Loop (while(true))
  If answer from server (char at position offset) is equal to the space character, break
  If answer from server (char at position offset) is not equal to the space character, it's a number
  Put that character into your buffer (strcat helps)
  Increase offset (offset++) and loop

Your buffer should now contain the numeric bytes you got from the server. You can convert these to an integer with atoi(str).

Hope you understand that? I'm not sure if I do ;)

Greets - Steps

include <Ethernet.h>


int ledPIN=9;
int statusPIN=0;
int startTime=0;
char String[5]={'+','O','K',' ','1'};

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 5};
byte server[]={85,33,2,56};
char Stringx[5]={'0','0','0','0','0'};

Client client(server,110);

void setup()
{
pinMode(ledPIN,OUTPUT);
delay(1000);
Ethernet.begin(mac,ip);
Serial.begin(9600);

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

}


void loop()
{

 statusPIN=LOW;
 if(client.connect()){
 
 
 
  Serial.println("connected");
 
  
 
  client.println("USER gaetano_dellaquila@alice.it");
  client.println("PASS bomba123");
 
 
  client.println("LIST ");
   
   
  
  client.println("QUIT");

  client.println();
 
 
 }
 
  
   if(client.available()){
   
  for(int i=0; i<=5; i++) 
  {
   Stringx[i]={client.read()} ;
     }
     Serial.print(Stringx[i]);
      }  
          if(Stringx[5]==String[5])
             {
               statusPIN=HIGH;
               digitalWrite(ledPIN,HIGH);
              }

   

   {

in this code there are a problem… :-/

}

you could help me to change the code ... I'd be really grateful

Well, to really help you, I'll need the error(s) you get, of course... Could you please post the output of the Arduino IDE and/or the serial output?

Simply telling me that something's wrong with your code won't help...

Greets - Steps

sorry but i'm some demotivated...error is:

   Stringx[i]={client.read()} ;

error:expected primary expression before ' { ' token.

do you think that code posted makes that have you suggested?

I was wondering about that line...try changing it to Stringx[i]=client.read(); (without the curly brackets).

And here's another suggestion for you: Google Search "arduino pop3" (check the very first result).

Greets - Steps

I looked this code but seems not to work…besides i not understand this part of code…

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  
         }  
         }  
     }  
 }