I have almost no experience with arduino or programming it.
So if anyone can help me with my task.
I like to connect a motion detection sensor to arduino, to send me an email when motion is deteced. But I like to get an email only after 20.00 hours to 6.00 and in saturday and sunday for whole day. So I don't get tons of spam when working hours and mayn people moving at that area.
Can I do that with arduino nano? What to use? Is there already written program for that?
I want just to connect all of it on powers supply and wifi network to make it work.
I am interested in doing something similar to this as well. I did a project in which I get alert emails if different things happen. I used a wemos d1 mini board, they have the mini r2 which looks and has pins just like the arduino uno. That is what I would use. It is coded and uploaded via the arduino ide.
what about code? did you do it by yourself or you have downloade it?
A combination of both. I used several examples I found online, but nothing you find is going to do exactly what you want. I can send you my code, but you are going to have to edit it to do what you want.
Are you going to use a pir sensor to detect movement? What kind? Is that the only thing you want it to do (detect movement and send email if the time is between a certain hour)?
I don’t mind sending you code, but it would be easier to send the right thing if I knew exactly what you’re trying to do.
yes please share code.
I will maybe add also a light sensor if that would not be so much trouble.
I will use regular PIR sensor for alarm. What I am thinking of to add lora to pir sensor to send and to arduino to receive.
Code is pasted at the bottom. However, you are going to have to find the libraries. I will try to include them, but I don't know if it will let me. This is not exactly what you are looking for. It is going to have to be modified, but it includes a way for the wemos board to connect to your wifi (change to your particular settings). It includes a time server so that you can set the time you do or don't want to receive emails. And it includes a way to have emails automatically sent out when you want.
A lot of it you will not need.
#include <ESP_Mail_Client.h>
#include <ESP_Mail_FS.h>
#include <SDK_Version_Common.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "" //add email address you want to use to send motion detected warning
#define AUTHOR_PASSWORD ""// add email password
#define RECIPIENT_EMAIL "" // add email you want to receive warnging message
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = ""; //ssid of what the wemos will need to connect to access the internet
const char* password = ""; password to access point/router
const long utcOffsetInSeconds = -14400;//this sets the time zone Use this to set the time zone
String header;
String output5State = "off"; //These are for the button presses on the web server interface
String output4State = "off"; //These are for the button presses on the web server interface
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
WiFiServer server(80);
IPAddress ip(192, 168, 1, 131); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long previousTime = 0;
unsigned long Hour;
unsigned long Minute;
const long timeoutTime = 2000;
volatile boolean TestEmail = false;
/* The SMTP Session object used for Email sending */
SMTPSession smtp;
/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status);
void setup() {
Serial.begin(115200);
Serial.println("Starting, 5 minute wait before compressor will come on");
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
//WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to WiFi network
Serial.println();
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
// show WiFi status in serial monitor
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ArduinoOTA.begin();
timeClient.begin();
}
void loop() {
ArduinoOTA.handle(); // This is for OTA uploading sketch
timeClient.update(); // This is to get time from NTP server
Hour = (timeClient.getHours()); // Not sure this is necessary, but allows me to adjust time format to non military
Minute = (timeClient.getMinutes()); //Not sure this is necessary, but allows me to adjust time format to have 0 before single digit
if (Hour > 12){ // This changes from military time to regular
Hour = (Hour - 12);
}
currentMillis = millis();
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
currentMillis = millis();
previousTime = currentMillis;
while (client.connected() && currentMillis - previousTime <= timeoutTime) { // loop while the client's connected
currentMillis = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /chickencoopdoor/opened") >= 0) { // spaces in "chicencoopdoor" messes up button
Serial.println("Chicken Coop Door opened");
output5State = "on";
//digitalWrite(output5, HIGH); // this actually turns the relay on or off
} else if (header.indexOf("GET /chickencoopdoor/closed") >= 0) {
Serial.println("GPIO 5 off");
output5State = "off";
//digitalWrite(output5, LOW); // this actually turns the relay on or off
} else if (header.indexOf("GET /door/open") >= 0) {
Serial.println("GPIO 4 on");
output4State = "on";
//digitalWrite(output4, HIGH); // this actually turns the relay on or off
} else if (header.indexOf("GET /door/closed") >= 0) {
Serial.println("GPIO 4 off");
output4State = "off";
//digitalWrite(output4, LOW); // this actually turns the relay on or off
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>Test Server</h1>");
client.println("<p>Dehumidifier is on " );
client.print("<p>time is: ");
client.print(Hour);
client.print(":");
if (Minute < 10){
client.print("0"); // this puts 0 before the minute
}
client.print(timeClient.getMinutes());
client.print("<p>compressor temperature is ");
//can put variables here
client.print("<p>In the last hour, the compressor has been on ");
client.println("minutes out of the last ");
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>GPIO 5 - State " + output5State + "</p>");
// If the output5State is off, it displays the ON button
if (output5State == "off") {
client.println("<p><a href=\"/chickencoopdoor/opened\"><button class=\"button\">Opened</button></a></p>"); //no spaces chickencoopdoor
} else {
client.println("<p><a href=\"/chickencoopdoor/closed\"><button class=\"button button2\">Closed</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 4
client.println("<p>GPIO 4 - State " + output4State + "</p>");
// If the output4State is off, it displays the ON button
if (output4State == "off") {
client.println("<p><a href=\"/door/open\"><button class=\"button\">Open</button></a></p>");
} else {
client.println("<p><a href=\"/door/closed\"><button class=\"button button2\">Closed</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
if (Hour == 8 and Minute == 50 and TestEmail == false) { // This sets the time for when or if you want a test email sent
Testemail1();
}
}
/* 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");
}
}
void Testemail1()
{
smtp.debug(1);
smtp.callback(smtpCallback);
ESP_Mail_Session session;
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 = "";
SMTP_Message message;
message.sender.name = "5 0 clock";
message.sender.email = AUTHOR_EMAIL;
message.subject = "Email Test";
message.addRecipient("Test", RECIPIENT_EMAIL);
String htmlMsg = "Did you get this email?\n The time is "; // how to add words to email text
htmlMsg += String(Hour); // how to add a variable to the email text
htmlMsg += String(":");
if (Minute < 10){
htmlMsg +=String("0");
}
htmlMsg += String(timeClient.getMinutes()); // this could be: htmlMsg += String(Minute); instead
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;
if (!smtp.connect(&session))
return;
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
TestEmail = true;
}
thank you for code I will test it.
which arduino will work with this code?
Can I use arduino nano and wifi module with this?
I used a wemos d1 mini. I am sure you can use a nano with wifi, but there will probably be changes that are needed within the code
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.