In this Project we will explain how to detect when a door or window has been opened and, how to notify a user via push notification. Such a system might notify an owner when someone enters his home.

The objective of this post is to present a simple circuit to detect the opening of a door or window. Further we show how to use the ESP8266 NodeMcu along with Pushbullet service, to send a push notification to a smartphone.

Materials Needed

For the sake of simplicity, this project is demonstrated in its most basic form in such that features and materials are kept at a minimum.  Following materials are needed for this project:

 

About Magnetic Reed Switch and Push Notifications

Magnetic Reed Switch

A reed switch is an electromagnetic switch used to control the flow of electricity in a circuit. They are made from two or more ferrous reeds encased within a small glass tube-like envelope, which become magnetized and move together or separate when a magnetic field (such as a permanent magnet) is moved towards the switch.

The switch available in our store consists of two parts: The permanent magnet and the reed switch itself. In this project the permanent magnet part will be attached to the door, while the switch with the circuitry is attached on the door frame as shown below.

Image: Arrangement of reed switch and magnet on a door

 

Push Notifications

A push notification is a message that pops up on a mobile device. App publishers can send them at any time; users don't have to be in the app or using their devices to receive them. They can do a lot of things; for example, they can show the latest sports scores, get a user to take an action, such as downloading a coupon, or let a user know about an event, such as a flash sale.

Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app. Each mobile platform has support for push notifications — iOS, Android, Fire OS, Windows and BlackBerry all have their own services.

Examples of IoT applications that requires push notifications:

  • Temperature & Humidity monitoring
  • Flood / water level monitoring
  • Natural methane gas monitoring
  • Baby wake-up noise alert
  • Door/window opening sensor
  • Smoke detector
  • Pet Feeder empty bowl notification

 

Some of the actors in sending a push notification include:

  1. Operating system push notification service (OSPNS). Each mobile operating system (OS), including iOS, Android, Fire OS, Windows, and BlackBerry, has its own service.
  2. App publisher. The app publisher enables their app with an OSPNS. Then, the publisher uploads the app to the app store.
  3. Client app. This is an OS-specific app, installed on a user's device. It receives incoming notifications.

 

Notification Service Setup

In our example we will use Pushbullet (which is an internet service which for SMS sending, notification management and file sending between your mobile devices and pc) together with Pushingbox (a cloud that can send notification, emails, tweets based on API calls in real time).

 

The overall mechanism can be as shown in the image below.

Image: Overview to the mechanism of a push notification from an ESP8266 to a smartphone

 

The cloud notification system has two components:

  • PushingBox that is used by the ESP8266 to trigger the notification
  • PushBullet that is the system that sends the notification to the connected devices

 

 

1) Setup Pushbullet

  • Create a free account at Pushbullet and activate it.
  • Go apps market / Google play and search for PushBullet and setup the application on your mobile device using the same account you’ve just created. After that you should see your mobile smartphone in the Devices menu.
  • Go to Settings menu and create a new Access Token by pressing Create Access Token button. Write down this Access token. You will need it in the next step.

2) Setup Pushingbox

  • Go to PushingBox, create a new account for free and login.
  • Go to Dashboard and click on My Services – > Add a service. Select PushBullet service from the list.
  • Give it a name, and paste your pushbullet Access token which you got from Pushbullet in step 1).
  • After that go to My Scenarios and add a new scenario. You should now be able to add a new Action to your scenario.

    Image: PushingBox scenario setup

  • The text between the $message_parameter$ signs will be your HTTP parameter, you will see in the code later. You will be able to replace each parameter like that with your data like temperature or humidity.
  • Save the action and go back to your virtual scenarios list. You should be able to see your new scenario and copy the DeviceId

 

Wiring

The magnetic reed switch is open ("off state") when the magnet part is adjacent to the switch (i.e. door is closed). One end of the switch is connected to the 3V3 (3.3V) pin of the ESP8266 while other end is connected to pin D2 (digital input). At the same time, pin D2  is connected to GND of the micro controller through a 10k resistor as a pull-down.

Whenever the door is closed, the switch is open, so that Pin D2 is pulled up to 3.3V (HIGH). Once the door opens, the switch closes ("on state") and pin D2 is pulled to ground (LOW). In this state, current of 0.33mA is flowing from he 3.3V pin though the 10k resistor to ground.

The ESP8266 NodeMcu can be powered through it's voltage input pin (Vin, range: 4.5 - 12V), through it's micro USB connector (5V) or directly through pin 3V3 (3.3V).

Image: Schematics of the reed switch wiring with the ESP8266 NodeMcu .A power jack can be connected to an external power supply such as a battery or adapter.

In this example we power the ESP8266 through its Vin pin that is connected to a power jack. Thus, an adapter or  battery pack that supplies a voltage of 4.5-12V can be connected.

 

Image: Wiring of the setup on a breadboard. The reed switch (in this example: light green) changes its state when the door is opened.

 

Connect the components as follows:

  1. D2 of NodeMcu connected to 10kOhm resistor and one end of the reed switch.
  2. Other end 10kOhm resistor connected to common ground, that is connected to GND of NodeMcu and powerjack (-)
  3.  Other end of reed switch is connected to 3v3 pin of NodeMcu
  4. Anode connected to VCC with 50 Ohm resistor between
  5.  Vin of NodeMcu connected to powerjack (+)
  6. Power jack can be connected to an external power source that supplies 4.5-12V (i.e. battery or adapter)

 

 

 

 

 

ESP8266 Code

The last step is implementing the sketch that checks the door status from the reed switch sensor and triggers the notification. Replace the dots with your Wi-Fi credentials and your deviceId from pushingBox account information.

 

ESP8266_SendMsgToPushServer.ino  - used to make the ESP8266 send a HTTP request with a payload in the request URL to the Pushing Box server

//ESP8266 Arduino Tutorial - Push notification messages example


#include <ESP8266WiFi.h>

// PushingBox scenario DeviceId code and API
String deviceId = "*************";
const char* logServer = "api.pushingbox.com";

const char* ssid = "**********"; //replace with your WiFi credentials
const char* password = "***********"; //replace with deviceid from pushingbox account


int reed_switch = 2;  //pin D4 is used to check the door status 
                      // High: door open, Low: door closed
boolean previous_doorStatus=0;

void setup() {
  Serial.begin(9600);
pinMode(reed_switch, INPUT);
previous_doorStatus = digitalRead(reed_switch );  

}

void sendNotification(String message){

  Serial.println("- connecting to Home Router SID: " + String(ssid));
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println();
  Serial.println("- succesfully connected");
  Serial.println("- starting client");
  
  WiFiClient client;

  Serial.println("- connecting to pushing server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    
    String postStr = "devid=";
    postStr += String(deviceId);
    postStr += "&message_parameter=";
    postStr += String(message);
    postStr += "\r\n\r\n";
    
    Serial.println("- sending data...");
    
    client.print("POST /pushingbox HTTP/1.1\n");
    client.print("Host: api.pushingbox.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
}

void loop() {
 // read the input pin:
  boolean door_status = digitalRead(reed_switch );

//makes sure that push notification only on door open
if(door_status != previous_doorStatus && !door_status ){
 // Sending a notification to your mobile phone 
 // function takes the message as a parameter 
sendNotification("Door has been opened");

}
previous_doorStatus=door_status;
delay(5000);
}

 

When door is open, D2 is HIGH and it's LOW when the door is closed. Every 5 seconds, pin D2 (door status) is checked and compared with the previous state. If there is a status change from LOW to HIGH (door has just been opened), the ESP8266 will send a HTTP request to the Pushing Box server which will trigger a notification through the Pushbullet app on the users phone.

 

Test the Setup

After uploading the ESP8266_SendMsgToPushServer.ino sketch to the board, the ESP8266 NodeMcu will check the door status every 5 seconds. Before testing the notification part, we recommend to check the circuit only first. It is important to ensure that the magnetic switch along with the wiring and the ESP8266 work properly and detect door openings.  This can be done by modifying the code. Instead of sending a push notification on door open, the build-in LED of the NodeMcu can be turned on for a second, to indicate that a door status change is detected properly.

So, in case no notifications are received, you can be sure that the error is not due to the circuit, the switch or the ESP8266. Probable sources of error could be:

  • access token copied incorrectly
  • no internet connectivity on the phone or ESP8266
  • ESP8266 crashed

If everything works fine, you should get a push notification on your phone with 5 to 10 seconds delay.

 

Wrap-Up

We connected the ESP8266 NodeMcu with an reed switch to sense when a door is opened or closed. The NodeMcu is connected to the internet via Wifi. We use Pushbullet and Pushing Box services (free) to send a notification when the door has been opened to the enduser's android phone. It will appear as a push notification.
This is the most basic configuration and there are plenty other ways to add functionalities. For instance the door open event could trigger an additional alarm buzzer or turn on a light switch confuse intruders.