I want to send an email when my Magnetic switch is HIGH. Magnetic Switch is connected to ESP32. So in which part of the code I should add this?
< int Led = 4; int magnetic = 5; int val; val=digitalRead(magnetic); if (val==HIGH) { digitalWrite(Led, HIGH); Serial.println("Motion detected"); delay(1000); } >
It might be a simple question but I am new in Arduino, and ESP32, so please help me.
I am following codes from this link https://randomnerdtutorials.com/esp32-send-email-smtp-server-arduino-ide/#comment-429759
Hi.
Move your variable definitions before the setup().
Move the other lines of code to the loop().
After detecting motion add the lines to send an email. I recommend creating a function that contains all the code to send the email.
I also recommend increasing the delay time a bit to prevent getting multiple emails.
Here’s an example (don’t forget to modify with your own details: sender email, recipient email and network credentials):
https://pastebin.com/wU73Nvew
I hope this helps.
Regards,
Sara
Thanks a lot, mam. But after running this I am getting error. I added my email and password and checked couple of times and even used a different one.
Start your code here17:57:31.363 -> Connecting.
17:57:31.676 -> WiFi connected.
17:57:31.676 ->
17:57:31.676 -> Preparing to send email
17:57:31.676 ->
17:57:34.249 -> Motion detected
17:57:34.249 -> Connecting to SMTP server...
17:57:38.271 -> Identification...
17:57:38.338 -> Authentication...
17:57:38.372 -> Sign in...
17:57:38.471 -> Error, login password is not valid
17:57:38.471 -> Error sending Email, login password is not valid
17:57:45.988 -> Motion detected
17:57:45.988 -> Connecting to SMTP server...
17:57:49.993 -> Identification...
17:57:50.026 -> Authentication...
17:57:50.059 -> Sign in...
17:57:50.159 -> Error, login password is not valid
17:57:50.159 -> Error sending Email, login password is not valid
18:00:53.765 -> Motion detected
18:00:53.765 -> Connecting to SMTP server...
18:00:57.799 -> Identification...
18:00:57.866 -> Authentication...
18:00:57.899 -> Sign in...
18:00:58.000 -> Error, login password is not valid
18:00:58.000 -> Error sending Email, login password is not valid
18:01:17.997 -> Motion detected
18:01:17.997 -> Connecting to SMTP server...
18:01:22.356 -> Identification...
18:01:22.425 -> Authentication...
18:01:22.460 -> Sign in...
18:01:22.632 -> Error, login password is not valid
18:01:22.632 -> Error sending Email, login password is not valid
18:01:27.617 -> Motion detected
18:01:27.617 -> Connecting to SMTP server...
18:01:31.568 -> Identification...
18:01:31.603 -> Authentication...
18:01:31.638 -> Sign in...
18:01:31.740 -> Error, login password is not valid
18:01:31.740 -> Error sending Email, login password is not valid
Hi.
The error says that password or email is not correct.
What email provider are you using? It may have different SMTP server settings. Did you check that?
Regards,
Sara
Hi again.
If you’re using gmail, you don’t need to change the SMTP server settings on our example.
The error you’re getting says that the password is not valid. The password is for the email sender account, not for the receiver email account.
Without further information I can’t understand what might be wrong :/
Regards,
Sara
Sorry for the late response. I solved my problem. Thanks a lot, Mam. I really appreciate your help.