Hello all,
I am having issue while trying to send an email notification while using and esp32 and a deb-ounce switch.
The first part of the sketch(void setup) will work fine, sending me my email with the message showing up that my esp32 have just booted up as I am starting up the board, but the switch won't trigger the 2nd email to go out, also I am receiving random emails about every minute or so about something triggering the motion( never touches the switch)...
I did try different input for my switch, still the same, I also physically removed the switch, ran a jumper wire directly from the input that I was using to the 3.3V so it stay high, still receiving random message from the void loop...
I also remove completely the portion that should send the message from the void loop, leaving only the part that will turn on the LED when i press the switch, that LED turn on every single time that I press the switch and then turn off as expected,
Thank You for any help
Sergio
#include <Arduino.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
#define WIFI_SSID "*******"
#define WIFI_PASSWORD "*******"
#define SMTP_server "smtp.gmail.com"
#define SMTP_Port 465
#define sender_email "*******@gmail.com"
#define sender_password "gvij fvik adad mbqx"
#define Recipient_email "*******@gmail.com"
#define LED 18
#define BUTTON 4
int button_state = 0;
SMTPSession smtp;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(LED, HIGH);
Serial.begin(9600);
Serial.println();
Serial.print("Connecting...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(200);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
smtp.debug(1);
ESP_Mail_Session session;
session.server.host_name = SMTP_server ;
session.server.port = SMTP_Port;
session.login.email = sender_email;
session.login.password = sender_password;
session.login.user_domain = "";
/* Declare the message class */
SMTP_Message message;
message.sender.name = "ESP32";
message.sender.email = sender_email;
message.subject = "ESP32 BOOT ALERT";
message.addRecipient("MYR2D2S",Recipient_email);
//Send HTML message
String htmlMsg = "<div style=\"color:#FF0000;\"><h1>YOUR MAIN ESP32WROOM SYSTEM HAVE JUST REBOOTED</h1><p>Sent by your ESP32WroomDevKit1</p></div>";
message.html.content = htmlMsg.c_str();
message.html.content = htmlMsg.c_str();
message.text.charSet = "us-ascii";
message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
/*
//Send simple text message
String textMsg = "Hello Microcontrollerslab! This is a simple text sent from ESP board";
message.text.content = textMsg.c_str();
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; */
if (!smtp.connect(&session))
return;
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
}
void loop()
{
button_state = digitalRead(BUTTON);
if (button_state == LOW)
digitalWrite(LED, LOW);
ESP_Mail_Session session;
session.server.host_name = SMTP_server ;
session.server.port = SMTP_Port;
session.login.email = sender_email;
session.login.password = sender_password;
session.login.user_domain = "";
/* Declare the message class */
SMTP_Message message;
message.sender.name = "ESP32";
message.sender.email = sender_email;
message.subject = "ESP32 MOTION ALERT";
message.addRecipient("MYR2D2S",Recipient_email);
//Send HTML message
String htmlMsg = "<div style=\"color:#FF0000;\"><h1>MOTION DETECTED IN THE MAIN LIVING ROOM AREA</h1><p>Sent by your ESP32WroomDevKit1</p></div>";
message.html.content = htmlMsg.c_str();
message.html.content = htmlMsg.c_str();
message.text.charSet = "us-ascii";
message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
/*
//Send simple text message
String textMsg = "Hello Microcontrollerslab! This is a simple text sent from ESP board";
message.text.content = textMsg.c_str();
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; */
if (!smtp.connect(&session))
return;
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
else
digitalWrite(LED, HIGH);
}