A simple burglar Alarm: Ethernet shield connection

The estimated reading time for this post is 307 seconds

Hello again. I received ad home the Ethernet shield and it’s time to study it a bit!
What I want to understand is how to send an email to my email address .

The connection to the Arduino UNO is simple , a picture can explain better!

FZ05SN7H05NT26I.MEDIUM(from instructables.com)

We can connect the RJ45 socket to our router and the plug line to usb port (see below).

FDP0VOXH05NHCWO.MEDIUM(from instructables.com)

The most important things to keep in mind are:

  1. ethernet shield ip settings
  2. ethernet shield mac address
  3. SMTP settings to send email

My router is in DHCP so it dynamically set the IP address.
No problems for the MAC address: it can be set as you need.

There are two ways to send email with Ethernet Shield:

  1. Temboo service has a library to send email with gmail account (instruction here )
  2. SMTP2GO service to send up to 10 email with its SMTP free service (here you can register )

I choose SMTP2GO cause I discovered Temboo too late, when I finished to deploy with SMTP2GO.
If someone use Temboo let me know your opinion.

I used Nicolaj Joergensen code,it’s very clear and well explained.

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

// Arduino network information
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
EthernetClient client;
char smtpServer[] = "smtpcorp.com";
void setup()
{
  Serial.begin(9600);  // for debug mode
  setupComm();
}
void loop()
{
  email("hallo");
  delay(1000);
}
//  ethernet shield connection init
void setupComm()
{
  Serial.println("Trying to connect");
  if (!Ethernet.begin(mac)){
    Serial.println("Failed to DHCP");
    // verifyng connection
    while(true);
  }
  delay(10000);
  // IP address is:
  Serial.print("IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
}
// now I send email
bool email(char* text)
{
  bool success = false;
  Serial.println("Sending email...");
  if (client.connect(smtpServer, 2525)){            //2525 is SMTP Server port
    Serial.println("connected");
    delay(100);
    client.println("EHLO arduino");
    for(int i=0; i<999; ++i){
      if(client.read() > 0)
        break;
    }
    Serial.println("responded");
    client.println("AUTH LOGIN");                     //see "http://base64-encoder-online.waraxe.us/"
    client.println("xxxxxxxxxx");           		 //Type your username  and encode it
    client.println("yyyyyyyyyy");        			//Type your password  and encode it</p>
    // Put your "from" email address here
    client.println("MAIL FROM:<dumm@gmail.com>"); //Seems you can write what you want here...
    for(int i=0; i<999; ++i){
      if(client.read() > 0)
        break;
    }
    client.println("RCPT TO:<mail@mail.com>");
    for(int i=0; i<999; ++i){
      if(client.read() > 0)
        break;
    }
    client.println("DATA");
    for(int i=0; i<999; ++i){
      if(client.read() > 0)
        break;
    }
    //Sender
    client.println("from: mail@mail.com"); //Sender address
    client.println("to: mail@mail.com");  //Receiver address
    client.println("SUBJECT: From your arduino");
    client.println("");
    client.println(text);
    client.println(".");
    client.println("QUIT");
    for (int i = 0; i<999; ++i){
      if(i > 998){
        Serial.println("error: No response");
      }
      if(client.read() > 0)
        break;
    }
    success = true;
    client.println();
    Serial.println("end");
  }
  else {
    Serial.println("Failed");
    client.println("QUIT"); //Disconnection
  }
  client.stop();
  return success;
}

Code explanation:

line  5: MAC Address setting: as told before you can choose the numerbs you want!
line  7:  SMTP server address (is smtpcorp.com for the SMTP2GO service)
line 15: email function: we send an email with “ciao” as text message.
line 22: ethernet shield initialization
line 30-33: here we display the IP address assigned in DHCP by my router. It’s really help know Ethernet shield address expecially if you want to use our shield as webserver.
line 42: SMTP command to send email.
line 51: 64 base encode for SMTP2GO username
line 52: 64 base encode for SMTP2GO password.

This link can be useful for encoding in 64 base: http://base64-encoder-online.waraxe.us/
For example CICCIO username will be Q0lDQ0lP in 64 base.

I uploaded the sketch on Arduino UNO and i received my first email!
At line 16 I set a delay, every 1 second Arduino will try to send an email!
Be careful cause SMTP2GO service provides up to 10 daily emails in the free edition.

It’s all for today with ethernet shield: i have all I want.

The next steps is to send a custom email when one sensor will detect an intrusion: I want to know witch sensor and at what time.

See you soon!

Ps. Meanwhile I received Arduino Nano!… 🙂 🙂

22 thoughts to “A simple burglar Alarm: Ethernet shield connection”

  1. “At line 18 I set a delay, every 1 second Arduino will try to send an email!”

    It’s line 16.

  2. Hi,

    first of all you have to register for a temboo account.
    Once you have your credential go here: https://temboo.com/library/Library/Google/Gmail/SendEmail/

    You have to choose:
    1. your shiled (arduino for example)
    2. your internet shield
    3. do not stream sensor data

    Then you have to fill the form (gmail username and password, etc, etc)

    At this point you have to click on RUN and your code is ready to copy and paste (remember also the HEADER FILE).

    I hope this help.

    Enjoy!

    Giuseppe

  3. thank you. The email code for SMTP2go worked beautiful the first time I tried it. I wasted several hours modifying code I had found elsewhere – learned through the process, but this code approached it entirely differently and worked. Thank you. I will have to check Temboo. I already had SMTP2go account so will use that for now.

  4. Hello!

    Thank You for this very clear example!
    I would like to know: How can I send an integer, or a String class with this.

  5. i’m failed in serial monitor said responded but there’s no mail sent and nothing happens in my SMTP2GO account. is there any configuration for my gmail?

  6. I know that gmail now have a check on secure or not applications. Unsecure application cannot send email . You can check in your google account if there is a setting like that. Try to uncheck this option and try again. Let me know. thank you

  7. I use google’s smtp “smtp.gmail.com” and TLS port 587,in console it says responded but didn’t receive any mail
    also I use google’s application password to connect…still no success. Can anyone help?

    1. I’n not sure but I think that now with an google account you need to authorise the external application to use Gmail. You can configure the authorization inside Gmail.

      1. Hello, I rather think the Arduino Ethernet library does NOT support TLS directly and Gmail requires domain mail authentification securtity to avoid spaming ; you HAVE to use SMTP2go as registered domain to pass email to Gmail.
        Rgds,

  8. Hello again, I do not understand the purpose of loop at line 78 :
    for (int i = 0; i 998){
    Serial.println(“error: No response”);
    }
    Seems i goes from 0 to 998 (<999) and IF i is OVER 998 THEN…., which never happens ?
    Can you please expalin ?
    thanks,

  9. output is repeated again & again as given below:

    Trying to connect
    IP address: 192.168.1.21.
    Sending email…
    connected
    responded
    end
    Sending email…
    connected
    responded
    end

    can any one help?