ESP8266 NodeMCU – Simple Wifi Alarm using PIR Motion Sensor

In this tutorial, we will learn how to use a PIR Motion Sensor (Passive infrared sensor) to detect if someone is inside the room. The ESP8266 will read the state of the sensor constantly. If you need help on how to start with ESP8266 check the tutorial “ESP8266 NodeMCU – Blinking a LED”.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, i will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

PIR Motion Sensor

Figure 1

The PIR Motion Sensor is a passive infrared sensor, which applies to projects that need to detect human or particle movement in a certain range. Since it is powerful and low-cost, it is widely accepted for open-source projects related to Arduino, raspberry pi, etc. This can teach beginners about PIR sensor.

The sensor measures infrared light radiating from objects in its field of view. The HC-SR501 is often equipped with a fresnel lens to increase the field of view, and the result being approximately like figure 2.

Figure 2

Technically, the PIR sensor is able to detect different levels of infrared radiation. Actually, the motion detector is separated into two parts. The output will swing high or low if one half see different IR radiation than the other.

In the Table below, we can see the specifications of the module:

Product Type HC–SR501 Body Sensor Module
Operating Voltage Range 5-20VDC
Quiescent Current <65mA
Level output High 3.3 V /Low 0V
Trigger L not repeated trigger/H repeated trigger
Delay time 5-300S( adjustable) Range (approximately .3Sec -5Min)
Board Dimensions 32mm*24mm
Angle Sensor <110 ° cone angle
Operation Temp. -15-+70 degrees
Lens size sensor Diameter:23mm(Default)

In figure 3, we can see the pinout of the sensor. It is very important to verify the pinout before you connect because if your sensor is from a Chinese manufacturer the pin order could be swapped in relation to one from amazon, for example. Normally, the information is printed on the PCB.

Figure 3

The screw potentiometers are for time and distance ajustements, respectively:

  • Adjust the distance potentiometer clockwise rotation, increased sensing distance (about 7 meters), on the contrary, the sensing distance decreases (about 3 meters).
  • Adjust the delay potentiometer clockwise rotation sensor the delay lengthened (300S), on the contrary, shorten the induction delay (5S).

NOTE: The induction module needs a minute or so to initialize. During initializing time, it will output 0-3 times. One minute later it comes into standby. Keep the surface of the lens from a close lighting source and wind, which will introduce interference.

Blynk Android app

There are a lot of android applications for IoT. We will use blynk APP. Other application for desktop can be used, but we will keep it android for now.

Blynk is a hardware-agnostic IoT platform with white-label mobile apps, private clouds, device management, data analytics, and machine learning. You should check their Website to know more about Blynk.

Figure 5

So to install and configure the app, you must follow the tutorial on their website. However, if you follow the blynk tutorial, you will end up using the blynk.io server. A blynk.io server is a good option because, is safe, continuous and fast. Although blynk.io server is currently providing 2.000 of energy, which limits your Dashboard. To solve this problem, you can use public free blynk servers, from this GitHub. Remember that these servers are unreliable and can have an intermittent shutdown. I will use the second server, but you can use any of them.

2º Server : blynk.iot-cm.com
App Port: 9443
Device Port: 8080
Status : Alive (seen 10 Nov 2019)
Energy : 1,000,000
Blynk Email Sending : unknown
Server location : Thailand (TH)
Admin : Krisana Meesuk Department of Electronic Science, Chiang Mai

To change the blynk.io server for the blynk.iot-cm.com, in the login in the page of the app, when choosing the server, you need to click on the “3 dots” button and copy the server name and App port, as shown in figure 2.

Figure 6

After making the instructions above, you will receive an email with a token. We will use that token in the code for access to the server. To begin, create a new project and name it as you desire. In this tutorial, it will be “Wifi Switch”.

Figure 7

Below the project name, you need to especify the hardware. Choose the ESP8266 option.

Figura 8

Go to the main screen again. Swipe the main screen to the left to open the widgets menu, and insert a Button, two LEDs and a notification. Press the button to open the button settings, and choose the virtual pin V0 and turn on the switch configuration. Press the LED to open the LED settings and choose the virtual pin V1 for the invasion LED and the virtual pin V2 for the other LED. As shown in figure 9.

Figure 9

Last but not least, press the notify to open the notify settings, and configure just like figure 10. You also can customize the behaviour, such as sound, vibration, etc… This widget, allows you to receive a notification every time you order in the code or when the hardware turn on or off. Be aware, that some servers do not permit to send notifications or permit a small number of notifications per day. If it is the case, you have to change to the original Blynk server or another.

Figure 10

For now on, we just have to program our devices and connect them with the blynk app, and we are ready to go.

Circuit

Figure 10

Before connecting everything, remember to verify the pinout. Connect the output of the PIR to GPIO 2 Pin D4, like in figure 10. The pin VU, stands for “Voltage USB”, so you can use the five volts of your USB cable. If not possible, use an external power supply and join the GNDs.

Coding

Copy the main sketch below to your Arduino IDE project and save it. The Blynk library and EPS8266 library must be included by you, I give you the links. Have fun!!

#define BLYNK_PRINT Serial       // used by Blynk to access Serial Monitor for troubleshooting
#include <BlynkSimpleEsp8266.h>  // Blynk ESP8266 wifi and arduino library
#include <ESP8266WiFi.h>


char auth[] = "Token"; 
char ssid[] = "Network";
char pass[] = "Password";

// defines pins numbers
const char sensor = 2;  // Digital pin D4

// defines variables
int Botton_On_Off = 0;
char flag = 0;
char estate_on = 0; 
char estate_off = 0;
char estate_1 = 0;
char estate_2 = 0;

WidgetLED led1(V1);       // Person inside, Blynk LED widget and associated virtual pin 1
WidgetLED led2(V2);       // Person not inside, Blynk LED widget and associated virtual pin 2

BLYNK_WRITE (V0){ 

  Botton_On_Off = param.asInt();

  if(Botton_On_Off){

    if(!estate_on){
      Serial.println("Sistem_on");     
      Blynk.notify("Sistem_on");

      estate_on = 1;
      estate_off = 0;
      flag = 1; 
      estate_1 = 0;
      estate_2 = 0;  
    }
  }else{

    if(!estate_off){
      Serial.println("Sistem_off");       
      Blynk.notify("Sistem_off");

      estate_off = 1;
      estate_on = 0;
      flag = 0;
      estate_1 = 0;
      estate_2 = 0;
    }
  }
}

void setup() {
  
  pinMode(sensor, INPUT);
  Serial.begin(9600); // Starts the serial communication
  //Blynk.begin(auth, ssid, pass);
  //Blynk.begin(auth, ssid, pass, "oasiskit.com", 80);
  Blynk.begin(auth, ssid, pass, IPAddress(128,199,173,118), 8080);

}

void loop() {
  Blynk.run();

  if(flag){ 

    long state = digitalRead(sensor);
    
  if (state == HIGH) {  
      led1.on();
      led2.off();

      if(!estate_1){
        
          Blynk.notify("Persson inside");
          Serial.println("Persson inside");

          estate_1 = 1;
          estate_2 = 0;
      }
  }
  else {
     led1.off();
     led2.on();

    if(!estate_2){

         Blynk.notify("Room Empty");
         Serial.println("Room Empty"); 

         estate_1 = 0;
         estate_2 = 1;
    }
  }
 }
}

1 thought on “ESP8266 NodeMCU – Simple Wifi Alarm using PIR Motion Sensor

Leave a Reply

Your email address will not be published. Required fields are marked *