Guest User

Untitled

a guest
Nov 16th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. //These are the libraries, they are the main code behind our functions
  2. #include <ESP8266Ping.h> //Handles the ping of the hostname for us.
  3. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  4. #include <DNSServer.h> //Spins up a DNS Server, this is required if you are not connected to WiFi
  5. #include <ESP8266WebServer.h> //Spins up a WEB Server, this is required if you are not connected to WiFi
  6. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  7. #include <base64.h> //Used by the email generation
  8. #include <WiFiClientSecure.h> //Used by the email generation
  9. #include <Ticker.h> //Ticker Library
  10.  
  11. Ticker blinker;
  12. #define LED 2 //On board LED
  13.  
  14. //This is the host that we want to ping, this is used to test internet is working.
  15. const char* remote_host = "www.google.com";
  16.  
  17. //These are the constants and variables required for sending an email
  18. const int SMTP_PORT = 465;
  19. const char* SMTP_SERVER = "smtp.gmail.com";
  20. String error_message;
  21. String ServerResponse;
  22. String Senders_Login = "crescendoalerts@gmail.com";
  23. String Senders_Password = "Crescend0UK";
  24. String From;
  25. //################################################
  26. String To, Subject, Message, Login_base64, Passwrd_base64;
  27. WiFiClientSecure client;
  28. //End of the email information
  29.  
  30. //gets called when WiFiManager enters configuration mode
  31. void configModeCallback (WiFiManager *myWiFiManager) {
  32. Serial.println("Entered config mode");
  33. Serial.println(WiFi.softAPIP());
  34. //if you used auto generated SSID, print it
  35. Serial.println(myWiFiManager->getConfigPortalSSID());
  36. }
  37.  
  38. void setup() {
  39. // put your setup code here, to run once:
  40. Serial.begin(115200);
  41.  
  42. pinMode(LED,OUTPUT);
  43.  
  44. //This will initialise the ticker which will make the internal LED blink every second
  45. //blinker.attach(1, changeState); //Use <strong>attach_ms</strong> if you need time in ms
  46.  
  47.  
  48. //WiFiManager
  49. //Local intialization. Once its business is done, there is no need to keep it around
  50. WiFiManager wifiManager;
  51. //reset settings - for testing
  52. //wifiManager.resetSettings();
  53.  
  54. //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  55. wifiManager.setAPCallback(configModeCallback);
  56.  
  57. //fetches ssid and pass and tries to connect
  58. //if it does not connect it starts an access point with the specified name
  59. //here "AutoConnectAP"
  60. //and goes into a blocking loop awaiting configuration
  61. if (!wifiManager.autoConnect()) {
  62. Serial.println("failed to connect and hit timeout");
  63. //reset and try again, or maybe put it to deep sleep
  64. ESP.reset();
  65. delay(1000);
  66. }
  67. //if you get here you have connected to the WiFi
  68. Serial.println("WiFi is now connected");
  69. changeState(); //This turns the LED off as it is on by default, we can then turn it on when there is activity
  70. }
  71.  
  72. void loop() {
  73. Serial.print("Pinging host ");
  74. Serial.println(remote_host);
  75. changeState(); //Turn the LED on to show activity
  76. if(Ping.ping(remote_host))
  77. {
  78. Serial.println("Success!!");
  79. changeState(); //Turn the LED off to show inactivity
  80. }
  81. else
  82. {
  83. Serial.println("Error :(");
  84. Serial.println("Attempting to send email alert...");
  85. From = "crescendo_alerts@gmail.com";
  86. SendMail("simon.bendall@live.co.uk", "Smart Home Alarm - Internet Failure", "Smart Home Alarm - The connection to the internet has been lost.");
  87. if (error_message != "") Serial.println(error_message);
  88. changeState(); //Turn the LED off to show inactivity
  89. }
  90. //We dont want to flood the network constantly so we are going to wait for 1 minute between tries
  91. //We might need to change this so it doesnt actually use the "DELAY" parameter as this shouldnt be used at runtime.
  92. delay(60000);
  93. }
  94.  
  95. void changeState() //switches the internal LED on or off depending on its current state
  96. {
  97. digitalWrite(LED, !(digitalRead(LED))); //Invert Current State of LED
  98. }
  99.  
  100. void SendMail(String To, String Subject, String Message) {
  101. if (!client.connect(SMTP_SERVER, SMTP_PORT)) {
  102. error_message = "SMTP responded that it could not connect to the mail server";
  103. return;
  104. }
  105. if (ErrorWhileWaitingForSMTP_Response("220", 500)) {
  106. error_message = "SMTP responded with a Connection Error";
  107. return;
  108. }
  109. client.println("HELO server");
  110. if (ErrorWhileWaitingForSMTP_Response("250", 500)) {
  111. error_message = "SMTP responded with an Identification error";
  112. return;
  113. }
  114. client.println("AUTH LOGIN");
  115. WaitSMTPResponse(ServerResponse, 500);
  116. client.println(base64::encode(Senders_Login));
  117. WaitSMTPResponse(ServerResponse, 500);
  118. client.println(base64::encode(Senders_Password));;
  119. if (ErrorWhileWaitingForSMTP_Response("235", 500)) {
  120. error_message = "SMTP responded with an Authorisation error";
  121. return;
  122. }
  123. String mailFrom = "MAIL FROM: <" + String(From) + '>';
  124. client.println(mailFrom);
  125. WaitSMTPResponse(ServerResponse, 500);
  126. String recipient = "RCPT TO: <" + To + '>';
  127. client.println(recipient);
  128. WaitSMTPResponse(ServerResponse, 500);
  129. client.println("DATA");
  130. if (ErrorWhileWaitingForSMTP_Response("354", 500)) {
  131. error_message = "SMTP DATA error";
  132. return;
  133. }
  134. client.println("From: <" + String(From) + '>');
  135. client.println("To: <" + String(To) + '>');
  136. client.print("Subject: ");
  137. client.println(String(Subject));
  138. client.println("Mime-Version: 1.0");
  139. client.println("Content-Type: text/html; charset=\"UTF-8\"");
  140. client.println("Content-Transfer-Encoding: 7bit");
  141. client.println();
  142. String body = "<!DOCTYPE html><html lang=\"en\">" + Message + "</html>";
  143. client.println(body);
  144. client.println(".");
  145. if (ErrorWhileWaitingForSMTP_Response("250", 1000)) {
  146. error_message = "SMTP responded with a Message error";
  147. return;
  148. }
  149. client.println("QUIT");
  150. if (ErrorWhileWaitingForSMTP_Response("221", 1000)) {
  151. error_message = "SMTP responded with a QUIT error";
  152. return;
  153. }
  154. client.stop();
  155. Serial.println("Message Sent");
  156. }
  157.  
  158. bool ErrorWhileWaitingForSMTP_Response(String Error_Code, int TimeOut) {
  159. int timer = millis();
  160. while (!client.available()) {
  161. if (millis() > (timer + TimeOut)) {
  162. error_message = "SMTP responsed that a Timeout occurred";
  163. return true;
  164. }
  165. }
  166. ServerResponse = client.readStringUntil('\n');
  167. if (ServerResponse.indexOf(Error_Code) == -1) return true;
  168. return false;
  169. }
  170.  
  171. bool WaitSMTPResponse(String Error_Code, int TimeOut) {
  172. int timer = millis();
  173. while (!client.available()) {
  174. if (millis() > (timer + TimeOut)) {
  175. error_message = "SMTP responded that a Timeout occurred";
  176. return false;
  177. }
  178. }
  179. ServerResponse = client.readStringUntil('\n');
  180. if (ServerResponse.indexOf(Error_Code) == -1) return false;
  181. return true;
  182. }
Add Comment
Please, Sign In to add comment