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!
(from instructables.com)
We can connect the RJ45 socket to our router and the plug line to usb port (see below).
(from instructables.com)
The most important things to keep in mind are:
- ethernet shield ip settings
- ethernet shield mac address
- 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:
- Temboo service has a library to send email with gmail account (instruction here )
- 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!… 🙂 🙂