/*
Rui Santos
Complete project details at:
- ESP32: https://RandomNerdTutorials.com/esp32-send-email-smtp-server-arduino-ide/
- ESP8266: https://RandomNerdTutorials.com/esp8266-nodemcu-send-email-smtp-server-arduino/
For ESP8266 devices that don't have external SRAM/PSRAM chip installed, choose the MMU option 3, 16KB cache + 48KB IRAM and 2nd Heap (shared).
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Example adapted from: https://github.com/mobizt/ESP-Mail-Client
*/
// To send Emails using Gmail on port 465 (SSL), you need to create an app password: https://support.google.com/accounts/answer/185833
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP_Mail_Client.h>
//#define WIFI_SSID "Mi MIX 2S"
//#define WIFI_PASSWORD "987654321"
#define WIFI_SSID "Gosha_Antonina"
#define WIFI_PASSWORD "0505973144"
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
/* The sign in credentials */
#define AUTHOR_EMAIL "почта@gmail.com"
#define AUTHOR_PASSWORD "пароль"
/* Recipient's email*/
#define RECIPIENT_EMAIL "почта@gmail.com"
/* The SMTP Session object used for Email sending */
SMTPSession smtp;
/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status);
int BAT= A0; // Аналоговый вход А0 для измерения напряжения батареи
float RatioFactor=5.54; // Коэффициент соотношения резисторов
bool trigger_Send = true;
float Tvoltage_Threshold_above = 12.6;
float Tvoltage_Threshold_below = 12.4;
int value = LOW;
float Tvoltage=0.0;
float Vvalue=0.0,Rvalue=0.0;
unsigned long send_time; // Переменная для хранения времени отправки сообщения(через сколько следующее)
//String textMsg; //--> Variable to hold all data that will be sent to email
//ESPTimeHelper ETH; //--> ESPTimeHelper declaration. This is used to get time data from the server.
unsigned long startTime;
void setup(){
Serial.begin(115200);
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
////////Подключаемся в течении 10 секунд, а если нет то нет. По-моим экспериментам хватает. В примере стояло даже 5 секунд, но к телефону не успевало подключаться.
while (WiFi.status() != WL_CONNECTED && (millis() - startTime) <= 10000) // try for 10 seconds
{
delay(500);
Serial.print(".");
}
Serial.print("Connecting to AP");
Serial.println("");
//////////////Не подключился??? Иди спать, потом попробуешь сначала.
if (WiFi.status() != WL_CONNECTED)
{ Serial.println("I'm awake, but I'm going into deep sleep mode for hour without WIFI");
//ESP.deepSleep(6000e6);
ESP.deepSleep(ESP.deepSleepMax());
}
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------Get time data from server
// ETH.TZ = 2; //--> GMT+2 . See the GMT list here: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
// Daylight Saving Time. If your location does not have Daylight Saving Time, just enter the value 0.
// ETH.DST_MN = 0; //--> See the DST list here: https://en.wikipedia.org/wiki/Daylight_saving_time_by_country
// Serial.println("Getting time data from server. Please wait...");
// if (!ETH.setClock(ETH.TZ, ETH.DST_MN)){
// Serial.println("Can't set clock...");
// return;
// }
// Serial.println("Successfully Get time data.");
//----------------------------------------
}
void loop(){
for(unsigned int i=0;i<10;i++){
Vvalue=Vvalue+analogRead(BAT); // Считываем значение с АЦП
delay(5);
}
Vvalue=(float)Vvalue/10.0; // Находим среднее значение из 10 измерений
Rvalue=(float)(Vvalue/1024.0)*3.3; //Конвертируем значение
Tvoltage=Rvalue*RatioFactor; //Находим исходное напряжение, умножив его на коэффициент
Serial.print(Vvalue);
Serial.println("V Analog input");
Serial.print(Tvoltage);
Serial.println("V battery input");
Serial.print("Напряжение - ");
Serial.print(Tvoltage);
Serial.println("V");
if(Tvoltage <= 12.8) {
///////
/** Enable the debug via Serial port
* none debug or 0
* basic debug or 1
*/
smtp.debug(1);
/* Set the callback function to get the sending results */
smtp.callback(smtpCallback);
/* Declare the session config data */
ESP_Mail_Session session;
/* Set the session config */
session.server.host_name = SMTP_HOST;
session.server.port = SMTP_PORT;
session.login.email = AUTHOR_EMAIL;
session.login.password = AUTHOR_PASSWORD;
session.login.user_domain = "";
/* Declare the message class */
SMTP_Message message;
/* Set the message headers */
message.sender.name = "ESP";
message.sender.email = AUTHOR_EMAIL;
message.subject = "ESP Battery voltage";
message.addRecipient("Gosha", RECIPIENT_EMAIL);
// Сообщение письма
String textMsg;
textMsg = "Напряжение батареи = " + String(Tvoltage)+"V";
message.text.content = textMsg.c_str();
message.text.charSet = "utf-8";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_high;
message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;
if (!smtp.connect(&session)) return; // Подключение к серверу с конфигурацией сеанса
//////
if(millis() - send_time > 60000) { // 1 минута = 60000
if (!MailClient.sendMail(&smtp, &message)) Serial.println("Ошибка отправки сообщения, " + smtp.errorReason());
send_time = millis();
}
/* Connect to server with the session config */
if (!smtp.connect(&session))
return;
/* Start sending Email and close the session */
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
////// Закончили всё, идем спать.
}
Serial.println("I'm awake, but I'm going into deep sleep mode for hour");
//ESP.deepSleep(6000e6);
ESP.deepSleep(ESP.deepSleepMax());
}
/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status){
/* Print the current status */
Serial.println(status.info());
/* Print the sending result */
if (status.success()){
Serial.println("----------------");
ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
Serial.println("----------------\n");
struct tm dt;
for (size_t i = 0; i < smtp.sendingResult.size(); i++){
/* Get the result item */
SMTP_Result result = smtp.sendingResult.getItem(i);
time_t ts = (time_t)result.timestamp;
localtime_r(&ts, &dt);
ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
ESP_MAIL_PRINTF("Subject: %s\n", result.subject);
}
Serial.println("----------------\n");
}
}