This is the sketch I'm trying to execute :
#include <SPI.h>
#include <Ethernet.h>
// Use comments to enable or disable this define for debug messages
#define DEBUG_POP
// Use comments to enable or disable the deleting of the mail
//#define ENABLE_DELETE_POP
// The mac address must be an unique number
// A mac generator is used:
// http://www.miniwebtool.com/mac-address-generator/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xFD, 0x78 };
// change network settings to yours
IPAddress ip( 192, 168, 10, 50 );
IPAddress gateway( 192, 168, 10, 210 );
IPAddress subnet( 255, 255, 255, 0 );
// Set the server POP3 address, the port, the user and password.
// The POP3 mail server is something like this:
// mail.yourdomain.com, pop.yourdomain.com, pop3.yourdomain.com
// Using PROGMEM for these causes a fail when trying to connect and log in.
const char pop_server[] = "pop.gmail.com";
const int pop_port = 110;
const char pop_user[] = "username";
const char pop_pass[] = "xxxx";
// The number of milliseconds timeout for parseInt() and find().
// The response time for the Server can still be 10 seconds.
#define POP_TIMEOUT 10
#define POP_TIMEOUT_DEFAULT 1000
EthernetClient client;
void setup()
{
Serial.begin( 9600);
Serial.println(F( "\nArduino POP3 email reader"));
pinMode( 13, OUTPUT); // the system led is used for testing
// When the Ethernet Shield is used, there is also a SD card connected
// to the SPI bus. Disable the SD card with chip select at pin 4.
pinMode( 4, OUTPUT);
digitalWrite( 4, LOW);
// Start Ethernet. Use only the 'mac' parameter for DHCP
// Use 'mac' and 'ip' parameters for static IP address.
Ethernet.begin( mac, ip);
Ethernet.begin( mac, ip, gateway, gateway, subnet);
// if( Ethernet.begin( mac) == 0)
// {
// Serial.println("Failed to configure Ethernet using DHCP.");
// // no point in carrying on, so do nothing forevermore:
// while(1);
// }
//
// print your local IP address.
Serial.println(F( "Ethernet started."));
Serial.print(F( "Local IP = "));
Serial.println(Ethernet.localIP());
Serial.println(F( "Press 'c' to check mail."));
}
void loop()
{
// Create a buffer to receive the commands in (that is the Subject of the mail).
char buffer[32];
byte inChar = Serial.read();
if(inChar == 'c')
{
// The getEmail gets the text of the mail Subject into the buffer.
// The valid number of received characters are returned.
// If the return value is < 0, it is an error.
int n = getEmail( buffer, sizeof(buffer));
if( n<0)
{
Serial.print(F("Email POP3 failed, error = "));
Serial.println( n);
}
else
{
if( n == 0)
{
Serial.println(F("Ready, nothing to do."));
}
else
{
// 'n' is > 0, a command received.
Serial.print(F("Email checked, Command = \""));
Serial.print( buffer);
Serial.println(F("\""));
// Check the commands.
//
// At this moment, a single command 'L' is used to set system led on or off.
// L=1 (set led on)
// L=0 (set led off)
if( buffer[0] == 'L' && buffer[1] == '=')
{
digitalWrite( 13, buffer[2] == '0' ? LOW : HIGH);
}
}
}
}
}
// getEmail
// --------
// Find an email on a mail server, using POP3.
// The Subject should start with "ARDUINO " and the text
// after that is copied into pBuf.
//
// The data in pBuf is only valid if the return value is not an error
// (an error is return value less than zero).
//
int getEmail( char *pBuf, int nBufSize)
{
// nBytes is the number of bytes that is returned by getEmail.
int nBytes = 0;
// Connect to server
// client.connect returns '1' if okay, or negative number if error.
// SUCCESS 1
// 0 (error, unknown timeout, perhaps an error in the library)
// TIMED_OUT -1
// INVALID_SERVER -2
// TRUNCATED -3
// INVALID_RESPONSE -4
// -5 (there is no mail server at that IP address and port)
// The string for the server must be a normal string in sram, no PROGMEM allowed.
int nError = client.connect( pop_server, pop_port);
// During testing, a value of zero was sometimes returned.
// This is not according to the documentation and it is an error.
// Therefor the non-error value '0' is turned into a negative number to
// indicate an error.
if( nError == 0)
return( -200);
// Only a value of 1 is okay.
if( nError != 1)
return( nError);
#ifdef DEBUG_POP
Serial.println(F("connected"));
#endif