Advertisement
Guest User

Untitled

a guest
Aug 8th, 2015
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. /*
  2. Created by Rui Santos
  3. Visit: http://randomnerdtutorials.com for more arduino projects
  4.  
  5. Arduino with Ethernet Shield
  6. */
  7.  
  8. #include <SPI.h>
  9. #include <Ethernet.h>
  10. int pos = 0;
  11. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 }; //physical mac address
  12. byte ip[] = { 192, 168, 0, 10 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
  13. byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
  14. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  15. EthernetServer server(80); //server port
  16. String readString;
  17.  
  18. char smtpserver[] = "smtpcorp.com";
  19. int port = 2525;
  20.  
  21. void setup() {
  22. // Open serial communications and wait for port to open:
  23. Serial.begin(115200);
  24. while (!Serial) {
  25. ; // wait for serial port to connect. Needed for Leonardo only
  26. }
  27. // start the Ethernet connection and the server:
  28. Ethernet.begin(mac, ip, gateway, subnet);
  29. server.begin();
  30. Serial.print("server is at ");
  31. Serial.println(Ethernet.localIP());
  32. }
  33.  
  34.  
  35. void loop() {
  36. // Create a client connection
  37. EthernetClient client = server.available();
  38. if (client) {
  39. while (client.connected()) {
  40. if (client.available()) {
  41. char c = client.read();
  42.  
  43. //read char by char HTTP request
  44. if (readString.length() < 100) {
  45. //store characters to string
  46. readString += c;
  47. //Serial.print(c);
  48. }
  49.  
  50. //if HTTP request has ended
  51. if (c == '\n') {
  52. Serial.println(readString); //print to serial monitor for debuging
  53.  
  54. client.println("HTTP/1.1 200 OK"); //send new page
  55. client.println("Content-Type: text/html");
  56. client.println();
  57. client.println("<HTML>");
  58. client.println("<HEAD>");
  59. client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  60. client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  61. client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
  62. client.println("<TITLE>Project X</TITLE>");
  63. client.println("</HEAD>");
  64. client.println("<BODY>");
  65. client.println("<H1>Project X</H1>");
  66. client.println("<hr />");
  67. client.println("<br />");
  68. client.println("<H2>Arduino with Ethernet Shield</H2>");
  69. client.println("<br />");
  70. client.println("<a href=\"/?sendemail\"\">Send Email</a>");
  71. client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");
  72. client.println("<br />");
  73. client.println("<br />");
  74. client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
  75. client.println("<a href=\"/?button2off\"\">Rotate Right</a><br />");
  76. client.println("<p>Created by Brandon Sisco. Visit http://siscogear.com for more products!</p>");
  77. client.print("Uptime: ");
  78. client.print(millis()/1000);
  79. if (millis()/1000 > 1) client.println(" seconds"); else client.println(" second");
  80. client.println("<br />");
  81. client.println("</BODY>");
  82. client.println("</HTML>");
  83.  
  84. delay(1);
  85. //stopping client
  86. client.stop();
  87. //controls the Arduino if you press the buttons
  88. if (readString.indexOf("?sendemail") >0){
  89. Serial.println("Send email.");
  90. if(sendEmail()) Serial.println(F("Email sent"));
  91. else Serial.println(F("Email failed"));
  92. }
  93. if (readString.indexOf("?button1off") >0){
  94. Serial.println("button 1 off");
  95. }
  96. if (readString.indexOf("?button2on") >0){
  97. Serial.println("button 2 on");
  98. }
  99. if (readString.indexOf("?button2off") >0){
  100. Serial.println("button 2 off");
  101. }
  102. //clearing string for next read
  103. readString="";
  104. }
  105. }
  106. }
  107. }
  108. }
  109.  
  110. EthernetClient smtpclient;
  111.  
  112. byte sendEmail()
  113. {
  114. byte thisByte = 0;
  115. byte respCode;
  116.  
  117. if(smtpclient.connect(smtpserver,port) == 1) {
  118. Serial.println(F("connected"));
  119. } else {
  120. Serial.println(F("connection failed"));
  121. return 0;
  122. }
  123.  
  124. if(!eRcv()) return 0;
  125.  
  126. Serial.println(F("Sending hello"));
  127. // replace 1.2.3.4 with your Arduino's ip
  128. smtpclient.println("EHLO 192.168.0.10");
  129. if(!eRcv()) return 0;
  130.  
  131. Serial.println(F("Sending auth login"));
  132. smtpclient.println("auth login");
  133. if(!eRcv()) return 0;
  134.  
  135. Serial.println(F("Sending User"));
  136. // Change to your base64 encoded user
  137. smtpclient.println("YnJhbmRvbi5zaXNjb0BnbWFpbC5jb20=");
  138.  
  139. if(!eRcv()) return 0;
  140.  
  141. Serial.println(F("Sending Password"));
  142. // change to your base64 encoded password
  143. smtpclient.println("aDRja3ByMDBm");
  144.  
  145. if(!eRcv()) return 0;
  146.  
  147. // change to your email address (sender)
  148. Serial.println(F("Sending From"));
  149. smtpclient.println("MAIL From: <arduino@siscodev.com>");
  150. if(!eRcv()) return 0;
  151.  
  152. // change to recipient address
  153. Serial.println(F("Sending To"));
  154. smtpclient.println("RCPT To: <brandon.sisco@gmail.com>");
  155. if(!eRcv()) return 0;
  156.  
  157. Serial.println(F("Sending DATA"));
  158. smtpclient.println("DATA");
  159. if(!eRcv()) return 0;
  160.  
  161. Serial.println(F("Sending email"));
  162.  
  163. // change to recipient address
  164. smtpclient.println("To: Brandon Sisco <brandon.sisco@gmail.com>");
  165.  
  166. // change to your address
  167. smtpclient.println("From: arduino <arduino@siscodev.com>");
  168.  
  169. smtpclient.println("Subject: Arduino email test\r\n");
  170.  
  171. smtpclient.println("This is from my Arduino!");
  172.  
  173. smtpclient.println(".");
  174.  
  175. if(!eRcv()) return 0;
  176.  
  177. Serial.println(F("Sending QUIT"));
  178. smtpclient.println("QUIT");
  179. if(!eRcv()) return 0;
  180.  
  181. smtpclient.stop();
  182.  
  183. Serial.println(F("disconnected"));
  184.  
  185. return 1;
  186. }
  187.  
  188. byte eRcv()
  189. {
  190. byte respCode;
  191. byte thisByte;
  192. int loopCount = 0;
  193.  
  194. while(!smtpclient.available()) {
  195. delay(1);
  196. loopCount++;
  197.  
  198. // if nothing received for 10 seconds, timeout
  199. if(loopCount > 15000) {
  200. smtpclient.stop();
  201. Serial.println(F("\r\nTimeout"));
  202. return 0;
  203. }
  204. }
  205.  
  206. respCode = smtpclient.peek();
  207.  
  208. while(smtpclient.available())
  209. {
  210. thisByte = smtpclient.read();
  211. Serial.write(thisByte);
  212. }
  213.  
  214. if(respCode >= '4')
  215. {
  216. efail();
  217. return 0;
  218. }
  219.  
  220. return 1;
  221. }
  222.  
  223.  
  224. void efail()
  225. {
  226. byte thisByte = 0;
  227. int loopCount = 0;
  228.  
  229. smtpclient.println(F("QUIT"));
  230.  
  231. while(!smtpclient.available()) {
  232. delay(1);
  233. loopCount++;
  234.  
  235. // if nothing received for 10 seconds, timeout
  236. if(loopCount > 10000) {
  237. smtpclient.stop();
  238. Serial.println(F("\r\nTimeout"));
  239. return;
  240. }
  241. }
  242.  
  243. while(smtpclient.available())
  244. {
  245. thisByte = smtpclient.read();
  246. Serial.write(thisByte);
  247. }
  248.  
  249. smtpclient.stop();
  250.  
  251. Serial.println(F("disconnected"));
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement