Arduino - Door Open - Send Email Notification

We are going to learn how to use Arduino to send a notification to you via email when someone opens your door. We will use Arduino, door sensor, and Ethernet Shield. You can also use any other Ethernet or WiFi shield by making a small modification in the Arduino code.

Arduino door open send email

The tutorial provides the Arduino code for two cases:

Hardware Required

1×Arduino UNO R4 WiFi
1×USB Cable Type-C
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Alternatively if using Ethernet:

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Door Sensor
1×Arduino Ethernet Shield 2
1×Ethernet Cable
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Door Sensor and Sending Email

If you do not know about the door sensor (pinout, how it works, how to program ...) and how to send email from Arduino, learn about them in the following tutorials:

Wiring Diagram

  • If using the Ethernet Shield, stack Ethernet Shield on Arduino Uno or Mega
  • Connect the door sensor to Arduino as below writing diagram
arduino door sensor wiring diagram

This image is created using Fritzing. Click to enlarge image

What we need to do

We need to do to main tasks:

  • Create an Applet on the IFTTT website
  • Write Arduino code that makes an HTTP request to the Applet when the door is opened.

How it works

When the door is opened:

  • Arduino makes an HTTP request to IFTTT Applet we created,
  • The IFTTT Applet will send an email to the email address.

The email address, email subject, and email content are specified when we create the IFTTT Applet.

How To Create an IFTTT Applet

Arduino create IFTTT applet
  • Click the Add button
Arduino adds IFTTT applet
  • Search for Webhooks, and then click the Webhooks icon
Arduino IFTTT Webhooks
  • Click the Receive a web request icon
Arduino IFTTT Webhooks Service
  • Click the Connect button
Arduino
  • Type an event name. You can give any name you want and memorize it to use later. The event name is a part of URL that Arduino will make request to. In this tutorial, we use a name send-email. And then click Create trigger button
Arduino Webhooks create trigger
  • Click the Add button to add the action
Arduino IFTTT create action
  • Search for email, and then click the Email icon
Arduino  Email service
  • Click the Send me an email icon
Arduino send me an email
  • Click the Connnect button
Arduino Connects to email
  • Input your email address and then click the Semd PIN button
Arduino
  • You will receive an email, get the PIN from email and input to Web UI to do verification
  • After that the email subject and content editor appears
Arduino email subject and content editor
  • You can give any subject and content for the email. The below are example
Arduino
  • Subject:
Alert from Arduino
  • Content:
Hey,
Someone opened your door!

Take care!
Arduino Uno
  • Click the Create Action button
  • Click the Continue button
Arduino
  • Click the Finish button
Arduino

Now you succeeded to create an IFTTT Applet for your Arduino. The next step is to get the IFTTT Webhooks key, which is used to authenticate and identify your Arduino.

Arduino IFTTT Webhooks documentation
  • You will see the Webhooks key as below
Arduino IFTTT webhooks key
  • Copy and wire down your Webhooks to use on Arduino code.

Now, We just need to write Arduino code that makes an HTTP request to the below URL:

https://maker.ifttt.com/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX

※ NOTE THAT:

  • In the URL, the send-email is the event name we used:
    • If you already used this name for another Applet, you can give it any other name.
    • If you use another name, please replace the send-email by your event name
  • Webhooks key is generated by IFTTT. Please keep it secret. You can change it by regenerating it on the IFTTT website.

Before writing Arduino code, We can test the IFTTT Applet by doing:

  • Open a web browser
  • Copy the below link:
https://maker.ifttt.com/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX
  • Paste it on the address bar of the web browser
  • Replace XXXXXXXXXXXXXXXXXXXXX with your Webhooks key.
  • Press the enter key on your keyboard. You will see the browser display the message as below image.
Arduino IFTTT applet test
  • And you will receive an email.

Arduino Code for Arduino Uno R4 WiFi

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-open-send-email-notification */ #include <WiFiS3.h> #include <ezButton.h> const char ssid[] = "YOUR_WIFI_SSID"; // change your network SSID (name) const char pass[] = "YOUR_WIFI_PASSWORD"; // change your network password (use for WPA, or use as key for WEP) ezButton doorSensor(13); // create ezButton object that attach to pin 13, which connected to door sensor's pin WiFiClient client; int status = WL_IDLE_STATUS; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key void setup() { Serial.begin(9600); Serial.println("DOOR MONITORING via EMAIL STARTED!"); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true) ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // attempt to connect to WiFi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } // print your board's IP address: Serial.print("IP Address: "); Serial.println(WiFi.localIP()); doorSensor.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { doorSensor.loop(); // MUST call the loop() function first if (doorSensor.isReleased()) { // LOW to HIGH Serial.println("The door is opened!"); Serial.println("Arduino is sending email"); sendEmail(); } } void sendEmail() { // connect to IFTTT server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else { // if not connected: Serial.println("connection failed"); } }

Arduino Code for Arduino and Ethernet Shield

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-open-send-email-notification */ #include <SPI.h> #include <Ethernet.h> #include <ezButton.h> ezButton doorSensor(13); // create ezButton object that attach to pin 13, which connected to door sensor's pin // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key void setup() { Serial.begin(9600); Serial.println("DOOR MONITORING via EMAIL STARTED!"); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address using DHCP"); while (true); } doorSensor.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { doorSensor.loop(); // MUST call the loop() function first if (doorSensor.isReleased()) { // LOW to HIGH Serial.println("The door is opened!"); Serial.println("Arduino is sending email"); sendEmail(); } } void sendEmail() { // connect to IFTTT server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else {// if not connected: Serial.println("connection failed"); } }

Quick Steps

  • If using the Ethernet Shield, stack Ethernet Shield on Arduino
  • Do wiring as above image
  • Install the door sensor on your door.
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • Close and then open the door
  • You will receive an email notification as below
arduino sends email when someone open the door
  • The log on Serial Monitor as below:
COM6
Send
DOOR MONITORING via EMAIL STARTED! The door is opened! Arduino is sending email Connected to server HTTP/1.1 200 OK Date: Mon, 22 Nov 2021 03:49:05 GMT Content-Type: text/html; charset=utf-8 Content-Length: 50 Connection: close X-Powered-By: Sad Unicorns X-Robots-Tag: none X-Top-SecreTTT: XXXXXXXXXXXXXXXXXXXXXXX ETag: W/"32-jD2lq/0l+3wF/XXXXXXXXXXXX" Congratulations! You've fired the send-email event disconnected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES