Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Created by Rui Santos
- Visit: http://randomnerdtutorials.com for more arduino projects
- Arduino with Ethernet Shield
- */
- #include <SPI.h>
- #include <Ethernet.h>
- int pos = 0;
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 }; //physical mac address
- byte ip[] = { 192, 168, 0, 10 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
- byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
- byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
- EthernetServer server(80); //server port
- String readString;
- char smtpserver[] = "smtpcorp.com";
- int port = 2525;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // Create a client connection
- EthernetClient client = server.available();
- if (client) {
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //read char by char HTTP request
- if (readString.length() < 100) {
- //store characters to string
- readString += c;
- //Serial.print(c);
- }
- //if HTTP request has ended
- if (c == '\n') {
- Serial.println(readString); //print to serial monitor for debuging
- client.println("HTTP/1.1 200 OK"); //send new page
- client.println("Content-Type: text/html");
- client.println();
- client.println("<HTML>");
- client.println("<HEAD>");
- client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
- client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
- client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
- client.println("<TITLE>Project X</TITLE>");
- client.println("</HEAD>");
- client.println("<BODY>");
- client.println("<H1>Project X</H1>");
- client.println("<hr />");
- client.println("<br />");
- client.println("<H2>Arduino with Ethernet Shield</H2>");
- client.println("<br />");
- client.println("<a href=\"/?sendemail\"\">Send Email</a>");
- client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
- client.println("<a href=\"/?button2off\"\">Rotate Right</a><br />");
- client.println("<p>Created by Brandon Sisco. Visit http://siscogear.com for more products!</p>");
- client.print("Uptime: ");
- client.print(millis()/1000);
- if (millis()/1000 > 1) client.println(" seconds"); else client.println(" second");
- client.println("<br />");
- client.println("</BODY>");
- client.println("</HTML>");
- delay(1);
- //stopping client
- client.stop();
- //controls the Arduino if you press the buttons
- if (readString.indexOf("?sendemail") >0){
- Serial.println("Send email.");
- if(sendEmail()) Serial.println(F("Email sent"));
- else Serial.println(F("Email failed"));
- }
- if (readString.indexOf("?button1off") >0){
- Serial.println("button 1 off");
- }
- if (readString.indexOf("?button2on") >0){
- Serial.println("button 2 on");
- }
- if (readString.indexOf("?button2off") >0){
- Serial.println("button 2 off");
- }
- //clearing string for next read
- readString="";
- }
- }
- }
- }
- }
- EthernetClient smtpclient;
- byte sendEmail()
- {
- byte thisByte = 0;
- byte respCode;
- if(smtpclient.connect(smtpserver,port) == 1) {
- Serial.println(F("connected"));
- } else {
- Serial.println(F("connection failed"));
- return 0;
- }
- if(!eRcv()) return 0;
- Serial.println(F("Sending hello"));
- // replace 1.2.3.4 with your Arduino's ip
- smtpclient.println("EHLO 192.168.0.10");
- if(!eRcv()) return 0;
- Serial.println(F("Sending auth login"));
- smtpclient.println("auth login");
- if(!eRcv()) return 0;
- Serial.println(F("Sending User"));
- // Change to your base64 encoded user
- smtpclient.println("YnJhbmRvbi5zaXNjb0BnbWFpbC5jb20=");
- if(!eRcv()) return 0;
- Serial.println(F("Sending Password"));
- // change to your base64 encoded password
- smtpclient.println("aDRja3ByMDBm");
- if(!eRcv()) return 0;
- // change to your email address (sender)
- Serial.println(F("Sending From"));
- smtpclient.println("MAIL From: <arduino@siscodev.com>");
- if(!eRcv()) return 0;
- // change to recipient address
- Serial.println(F("Sending To"));
- smtpclient.println("RCPT To: <brandon.sisco@gmail.com>");
- if(!eRcv()) return 0;
- Serial.println(F("Sending DATA"));
- smtpclient.println("DATA");
- if(!eRcv()) return 0;
- Serial.println(F("Sending email"));
- // change to recipient address
- smtpclient.println("To: Brandon Sisco <brandon.sisco@gmail.com>");
- // change to your address
- smtpclient.println("From: arduino <arduino@siscodev.com>");
- smtpclient.println("Subject: Arduino email test\r\n");
- smtpclient.println("This is from my Arduino!");
- smtpclient.println(".");
- if(!eRcv()) return 0;
- Serial.println(F("Sending QUIT"));
- smtpclient.println("QUIT");
- if(!eRcv()) return 0;
- smtpclient.stop();
- Serial.println(F("disconnected"));
- return 1;
- }
- byte eRcv()
- {
- byte respCode;
- byte thisByte;
- int loopCount = 0;
- while(!smtpclient.available()) {
- delay(1);
- loopCount++;
- // if nothing received for 10 seconds, timeout
- if(loopCount > 15000) {
- smtpclient.stop();
- Serial.println(F("\r\nTimeout"));
- return 0;
- }
- }
- respCode = smtpclient.peek();
- while(smtpclient.available())
- {
- thisByte = smtpclient.read();
- Serial.write(thisByte);
- }
- if(respCode >= '4')
- {
- efail();
- return 0;
- }
- return 1;
- }
- void efail()
- {
- byte thisByte = 0;
- int loopCount = 0;
- smtpclient.println(F("QUIT"));
- while(!smtpclient.available()) {
- delay(1);
- loopCount++;
- // if nothing received for 10 seconds, timeout
- if(loopCount > 10000) {
- smtpclient.stop();
- Serial.println(F("\r\nTimeout"));
- return;
- }
- }
- while(smtpclient.available())
- {
- thisByte = smtpclient.read();
- Serial.write(thisByte);
- }
- smtpclient.stop();
- Serial.println(F("disconnected"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement