Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //These are the libraries, they are the main code behind our functions
- #include <ESP8266Ping.h> //Handles the ping of the hostname for us.
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <DNSServer.h> //Spins up a DNS Server, this is required if you are not connected to WiFi
- #include <ESP8266WebServer.h> //Spins up a WEB Server, this is required if you are not connected to WiFi
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #include <base64.h> //Used by the email generation
- #include <WiFiClientSecure.h> //Used by the email generation
- #include <Ticker.h> //Ticker Library
- Ticker blinker;
- #define LED 2 //On board LED
- //This is the host that we want to ping, this is used to test internet is working.
- const char* remote_host = "www.google.com";
- //These are the constants and variables required for sending an email
- const int SMTP_PORT = 465;
- const char* SMTP_SERVER = "smtp.gmail.com";
- String error_message;
- String ServerResponse;
- String Senders_Login = "crescendoalerts@gmail.com";
- String Senders_Password = "Crescend0UK";
- String From;
- //################################################
- String To, Subject, Message, Login_base64, Passwrd_base64;
- WiFiClientSecure client;
- //End of the email information
- //gets called when WiFiManager enters configuration mode
- void configModeCallback (WiFiManager *myWiFiManager) {
- Serial.println("Entered config mode");
- Serial.println(WiFi.softAPIP());
- //if you used auto generated SSID, print it
- Serial.println(myWiFiManager->getConfigPortalSSID());
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- pinMode(LED,OUTPUT);
- //This will initialise the ticker which will make the internal LED blink every second
- //blinker.attach(1, changeState); //Use <strong>attach_ms</strong> if you need time in ms
- //WiFiManager
- //Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wifiManager;
- //reset settings - for testing
- //wifiManager.resetSettings();
- //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
- wifiManager.setAPCallback(configModeCallback);
- //fetches ssid and pass and tries to connect
- //if it does not connect it starts an access point with the specified name
- //here "AutoConnectAP"
- //and goes into a blocking loop awaiting configuration
- if (!wifiManager.autoConnect()) {
- Serial.println("failed to connect and hit timeout");
- //reset and try again, or maybe put it to deep sleep
- ESP.reset();
- delay(1000);
- }
- //if you get here you have connected to the WiFi
- Serial.println("WiFi is now connected");
- changeState(); //This turns the LED off as it is on by default, we can then turn it on when there is activity
- }
- void loop() {
- Serial.print("Pinging host ");
- Serial.println(remote_host);
- changeState(); //Turn the LED on to show activity
- if(Ping.ping(remote_host))
- {
- Serial.println("Success!!");
- changeState(); //Turn the LED off to show inactivity
- }
- else
- {
- Serial.println("Error :(");
- Serial.println("Attempting to send email alert...");
- From = "crescendo_alerts@gmail.com";
- SendMail("simon.bendall@live.co.uk", "Smart Home Alarm - Internet Failure", "Smart Home Alarm - The connection to the internet has been lost.");
- if (error_message != "") Serial.println(error_message);
- changeState(); //Turn the LED off to show inactivity
- }
- //We dont want to flood the network constantly so we are going to wait for 1 minute between tries
- //We might need to change this so it doesnt actually use the "DELAY" parameter as this shouldnt be used at runtime.
- delay(60000);
- }
- void changeState() //switches the internal LED on or off depending on its current state
- {
- digitalWrite(LED, !(digitalRead(LED))); //Invert Current State of LED
- }
- void SendMail(String To, String Subject, String Message) {
- if (!client.connect(SMTP_SERVER, SMTP_PORT)) {
- error_message = "SMTP responded that it could not connect to the mail server";
- return;
- }
- if (ErrorWhileWaitingForSMTP_Response("220", 500)) {
- error_message = "SMTP responded with a Connection Error";
- return;
- }
- client.println("HELO server");
- if (ErrorWhileWaitingForSMTP_Response("250", 500)) {
- error_message = "SMTP responded with an Identification error";
- return;
- }
- client.println("AUTH LOGIN");
- WaitSMTPResponse(ServerResponse, 500);
- client.println(base64::encode(Senders_Login));
- WaitSMTPResponse(ServerResponse, 500);
- client.println(base64::encode(Senders_Password));;
- if (ErrorWhileWaitingForSMTP_Response("235", 500)) {
- error_message = "SMTP responded with an Authorisation error";
- return;
- }
- String mailFrom = "MAIL FROM: <" + String(From) + '>';
- client.println(mailFrom);
- WaitSMTPResponse(ServerResponse, 500);
- String recipient = "RCPT TO: <" + To + '>';
- client.println(recipient);
- WaitSMTPResponse(ServerResponse, 500);
- client.println("DATA");
- if (ErrorWhileWaitingForSMTP_Response("354", 500)) {
- error_message = "SMTP DATA error";
- return;
- }
- client.println("From: <" + String(From) + '>');
- client.println("To: <" + String(To) + '>');
- client.print("Subject: ");
- client.println(String(Subject));
- client.println("Mime-Version: 1.0");
- client.println("Content-Type: text/html; charset=\"UTF-8\"");
- client.println("Content-Transfer-Encoding: 7bit");
- client.println();
- String body = "<!DOCTYPE html><html lang=\"en\">" + Message + "</html>";
- client.println(body);
- client.println(".");
- if (ErrorWhileWaitingForSMTP_Response("250", 1000)) {
- error_message = "SMTP responded with a Message error";
- return;
- }
- client.println("QUIT");
- if (ErrorWhileWaitingForSMTP_Response("221", 1000)) {
- error_message = "SMTP responded with a QUIT error";
- return;
- }
- client.stop();
- Serial.println("Message Sent");
- }
- bool ErrorWhileWaitingForSMTP_Response(String Error_Code, int TimeOut) {
- int timer = millis();
- while (!client.available()) {
- if (millis() > (timer + TimeOut)) {
- error_message = "SMTP responsed that a Timeout occurred";
- return true;
- }
- }
- ServerResponse = client.readStringUntil('\n');
- if (ServerResponse.indexOf(Error_Code) == -1) return true;
- return false;
- }
- bool WaitSMTPResponse(String Error_Code, int TimeOut) {
- int timer = millis();
- while (!client.available()) {
- if (millis() > (timer + TimeOut)) {
- error_message = "SMTP responded that a Timeout occurred";
- return false;
- }
- }
- ServerResponse = client.readStringUntil('\n');
- if (ServerResponse.indexOf(Error_Code) == -1) return false;
- return true;
- }
Add Comment
Please, Sign In to add comment