#include #include #define SMTP_HOST "smtp.dreamhost.com" #define SMTP_PORT 465 #define AUTHOR_EMAIL "automation@mydomain.ca" #define AUTHOR_PASSWORD "***3bHb" #define RECIPIENT_EMAIL "recipient@gmail.com" #define redLed 14 //D5 #define greenLed 12 //D6 #define acSense A0 #define rstBtn 16 //D0 #define pwrBton 13 //D7 bool senseOn = 1; bool rstBtnStatus = 1; bool pwrBtnStatus = 1; int acSenseStatus = 0; bool ledState; int justCount = 0; unsigned long previousTimeLed = 0; const unsigned long greenLedFlash = 1000; unsigned long previousTimeCount = 0; const unsigned long acLoopCount = 5000; SMTPSession smtp; void smtpCallback(SMTP_Status status); void setup() { pinMode(redLed, OUTPUT); pinMode(greenLed, OUTPUT); pinMode(acSense, INPUT); pinMode(rstBtn, INPUT); pinMode(pwrBton, INPUT); Serial.begin(115200); WiFiManager wm; wm.setAPCallback(configModeCallback); bool res; res = wm.autoConnect("fallback","12345678"); if(!res) { //Serial.println("Failed to connect"); digitalWrite(redLed, HIGH); digitalWrite(greenLed, HIGH); senseOn = 0; } else { //Serial.println("connected..."); digitalWrite(redLed, LOW); digitalWrite(greenLed, HIGH); } //--------ESP_MAIL_CLIENT-----(working when code is here)------------- } void loop() { if(senseOn){ unsigned long currentTime = millis(); //----------- Flash green led -------------- if(currentTime - previousTimeLed >= greenLedFlash){ ledState = !ledState; digitalWrite(greenLed, ledState); previousTimeLed = currentTime; } //----------- Reset button pushed ---------- rstBtnStatus = digitalRead(rstBtn); if(!rstBtnStatus){ WiFi.disconnect(); ESP.restart(); } //----------- AC Detected ------------------ acSenseStatus = analogRead(acSenseStatus); if(acSenseStatus > 200){ digitalWrite(redLed, HIGH); if(currentTime - previousTimeCount >= acLoopCount){ justCount++; Serial.println(justCount); previousTimeCount = currentTime; if(justCount > 3){ sendEmail(); digitalWrite(redLed, LOW); digitalWrite(greenLed, HIGH); senseOn = 0; } } } else{ digitalWrite(redLed, LOW); justCount = 0; } } } void sendEmail(){ //--------ESP_MAIL_CLIENT------not working when code is here------------ MailClient.networkReconnect(true); smtp.callback(smtpCallback); Session_Config config; config.server.host_name = SMTP_HOST; config.server.port = SMTP_PORT; config.login.email = AUTHOR_EMAIL; config.login.password = AUTHOR_PASSWORD; config.login.user_domain = ""; config.time.ntp_server = F("pool.ntp.org,time.nist.gov"); config.time.gmt_offset = -4; config.time.day_light_offset = 0; SMTP_Message message; message.sender.name = F("Electrical Panel"); message.sender.email = AUTHOR_EMAIL; message.subject = F("Information from JAB"); message.addRecipient(F("Owner"), RECIPIENT_EMAIL); String textMsg = "It looks like your power is back...!"; message.text.content = textMsg.c_str(); message.text.charSet = "us-ascii"; message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low; message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay; if (!smtp.connect(&config)){ ESP_MAIL_PRINTF("Connection error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); return; } if (!smtp.isLoggedIn()){ Serial.println("\nNot yet logged in."); } else{ if (smtp.isAuthenticated()) Serial.println("\nSuccessfully logged in."); else Serial.println("\nConnected with no Auth."); } /* Start sending Email and close the session */ if (!MailClient.sendMail(&smtp, &message)) ESP_MAIL_PRINTF("Error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); } void configModeCallback (WiFiManager *myWiFiManager) { Serial.println("Entered config mode"); Serial.println(WiFi.softAPIP()); Serial.println(myWiFiManager->getConfigPortalSSID()); digitalWrite(redLed, HIGH); digitalWrite(greenLed, LOW); } void smtpCallback(SMTP_Status status){ /* Print the current status */ Serial.println(status.info()); /* Print the sending result */ if (status.success()){ // ESP_MAIL_PRINTF used in the examples is for format printing via debug Serial port // that works for all supported Arduino platform SDKs e.g. AVR, SAMD, ESP32 and ESP8266. // In ESP8266 and ESP32, you can use Serial.printf directly. Serial.println("----------------"); ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount()); ESP_MAIL_PRINTF("Message sent failed: %d\n", status.failedCount()); Serial.println("----------------\n"); for (size_t i = 0; i < smtp.sendingResult.size(); i++) { /* Get the result item */ SMTP_Result result = smtp.sendingResult.getItem(i); // In case, ESP32, ESP8266 and SAMD device, the timestamp get from result.timestamp should be valid if // your device time was synched with NTP server. // Other devices may show invalid timestamp as the device time was not set i.e. it will show Jan 1, 1970. // You can call smtp.setSystemTime(xxx) to set device time manually. Where xxx is timestamp (seconds since Jan 1, 1970) ESP_MAIL_PRINTF("Message No: %d\n", i + 1); ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed"); ESP_MAIL_PRINTF("Date/Time: %s\n", MailClient.Time.getDateTimeString(result.timestamp, "%B %d, %Y %H:%M:%S").c_str()); ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str()); ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str()); } Serial.println("----------------\n"); // You need to clear sending result as the memory usage will grow up. smtp.sendingResult.clear(); } }