8.9 Blynk-based Intrusion Notification System

This project demonstrate a simple home intrusion detection system using a PIR motion sensor (HC-SR501). When the system is set to “Away” mode through the Blynk app, the PIR sensor monitors for motion. Any detected movement triggers a notification on the Blynk app, alerting the user of potential intrusion.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

ESP32 Starter Kit

320+

ESP32 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

ESP32 WROOM 32E

BUY

ESP32 Camera Extension

-

Jumper Wires

BUY

PIR Motion Sensor Module

BUY

1. Circuit Assembly

../../_images/iot_9_blynk_bb.png

2. Blynk Configuration

2.1 Initializing Blynk

  1. Navigate to the BLYNK and select START FREE.

    ../../_images/09_blynk_access.png
  2. Enter your email to initiate the registration process.

    ../../_images/09_blynk_sign_in.png
  3. Confirm your registration through your email.

    ../../_images/09_blynk_password.png
  4. After confirmation, Blynk Tour will appear. It is recommended to select “Skip”. If Quick Start also appears, consider skipping it as well.

    ../../_images/09_blynk_tour.png

2.2 Template Creation

  1. First, create a template in Blynk. Follow the subsequent instructions to create the Intrusion Alert System template.

    ../../_images/09_create_template_1_shadow.png
  2. Assign a name to the template, select ESP32 Hardware, and select Connection Type as WiFi, then select Done.

    ../../_images/09_create_template_2_shadow.png

2.3 Datastream Generation

Open the template you just set up, let’s create two datastreams.

  1. Click New Datastream.

    ../../_images/09_blynk_new_datastream.png
  2. In the popup, choose Virtual Pin.

    ../../_images/09_blynk_datastream_virtual.png
  3. Name the Virtual Pin V0 as AwayMode. Set the DATA TYPE as Integer with MIN and MAX values as 0 and 1.

    ../../_images/09_create_template_shadow.png
  4. Similarly, create another Virtual Pin datastream. Name it Current Status and set the DATA TYPE to String.

    ../../_images/09_datastream_1_shadow.png

2.4 Setting Up an Event

Next, we’ll set up an event that sends an email notification if an intrusion is detected.

  1. Click Add New Event.

    ../../_images/09_blynk_event_add.png
  2. Define the event’s name and its specific code. For TYPE, choose Warning and write a short description for the email to be sent when the event happens. You can also adjust how often you get notified.

    Note

    Make sure the EVENT CODE is set as intrusion_detected. This is predefined in the code, so any changes would mean you need to adjust the code as well.

    ../../_images/09_event_1_shadow.png
  3. Go to the Notifications section to turn on notifications and set up email details.

    ../../_images/09_event_2_shadow.png

2.5 Fine-Tuning the Web Dashboard

Making sure the Web Dashboard interacts perfectly with the Intrusion Alert System is vital.

  1. Simply drag and place both the Switch widget and the Label widget onto the Web Dashboard.

    ../../_images/09_web_dashboard_1_shadow.png
  2. When you hover over a widget, three icons will appear. Use the settings icon to adjust the widget’s properties.

    ../../_images/09_blynk_dashboard_set.png
  3. In the Switch widget settings, select Datastream as AwayMode(V0). Set ONLABEL and OFFLABEL to display “away” and “home”, respectively.

    ../../_images/09_web_dashboard_2_shadow.png
  4. In the Label widget settings, select Datastream as Current Status(V1).

    ../../_images/09_web_dashboard_3_shadow.png

2.6 Saving the Template

Lastly, don’t forget to save your template.

../../_images/09_save_template_shadow.png

2.7 Making a Device

  1. It’s time to create a new device.

    ../../_images/09_blynk_device_new.png
  2. Click on From template to start with a new setup.

    ../../_images/09_blynk_device_template.png
  3. Then, pick the Intrusion Alert System template and click on Create.

    ../../_images/09_blynk_device_template2.png
  4. Here, you’ll see the Template ID, Device Name, and AuthToken. You need to copy these into your code so the ESP32 can work with Blynk.

    ../../_images/09_blynk_device_code.png

3. Code Execution

  1. Before running the code, make sure to install the Blynk library from the Library Manager on the Arduino IDE.

    ../../_images/09_blynk_add_library.png
  2. Open the iot_9_intrusion_alert_system.ino file, which is located in the esp32-starter-kit-main\c\codes\iot_9_intrusion_alert_system directory. You can also copy its content into the Arduino IDE.

  3. Replace the placeholders for BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME, and BLYNK_AUTH_TOKEN with your own unique IDs.

    #define BLYNK_TEMPLATE_ID "TMPxxxxxxx"
    #define BLYNK_TEMPLATE_NAME "Intrusion Alert System"
    #define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxx"
    
  4. You also need to enter your WiFi network’s ssid and password.

    char ssid[] = "your_ssid";
    char pass[] = "your_password";
    
  5. Choose the correct board (ESP32 Dev Module) and port, then click the Upload button.

  6. Open the Serial monitor (set baud rate to 115200) and wait for a successful connection message.

    ../../_images/09_blynk_upload_code.png
  7. After a successful connection, activating the switch in Blynk will start the PIR module’s surveillance. When motion is detected (state of 1), it will say, “Somebody here!” and send an alert to your email.

    ../../_images/09_blynk_code_alarm.png

4. Code explanation

  1. Configuration & Libraries

    Here, you set up the Blynk constants and credentials. You also include the necessary libraries for the ESP32 and Blynk.

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial
    
    #define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
    #define BLYNK_TEMPLATE_NAME "Intrusion Alert System"
    #define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>
    
  2. WiFi Setup

    Enter your WiFi credentials.

    char ssid[] = "your_ssid";
    char pass[] = "your_password";
    
  3. PIR Sensor Configuration

    Set the pin where the PIR sensor is connected and initialize the state variables.

    const int sensorPin = 14;
    int state = 0;
    int awayHomeMode = 0;
    BlynkTimer timer;
    
  4. setup() Function

    This function initializes the PIR sensor as an input, sets up serial communication, connects to WiFi, and configures Blynk.

    • We use timer.setInterval(1000L, myTimerEvent) to set the timer interval in setup(), here we set to execute the myTimerEvent() function every 1000ms. You can modify the first parameter of timer.setInterval(1000L, myTimerEvent) to change the interval between myTimerEvent executions.


    void setup() {
    
        pinMode(sensorPin, INPUT);  // Set PIR sensor pin as input
        Serial.begin(115200);           // Start serial communication at 115200 baud rate for debugging
    
        // Configure Blynk and connect to WiFi
        Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
    
        timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
    }
    
  5. loop() Function

    The loop function continuously runs Blynk and the Blynk timer functions.

    void loop() {
       Blynk.run();
       timer.run();
    }
    
  6. Blynk App Interaction

    These functions are called when the device connects to Blynk and when there’s a change in the state of the virtual pin V0 on the Blynk app.

    • Every time the device connects to the Blynk server, or reconnects due to poor network conditions, the BLYNK_CONNECTED() function is called. The Blynk.syncVirtual() command request a single Virtual Pin value. The specified Virtual Pin will perform BLYNK_WRITE() call.

    • Whenever the value of a virtual pin on the BLYNK server changes, it will trigger BLYNK_WRITE().


    // This function is called every time the device is connected to the Blynk.Cloud
    BLYNK_CONNECTED() {
        Blynk.syncVirtual(V0);
    }
    
    // This function is called every time the Virtual Pin 0 state changes
    BLYNK_WRITE(V0) {
        awayHomeMode = param.asInt();
        // additional logic
    }
    
  7. Data Handling

    Every second, the myTimerEvent() function calls sendData(). If the away mode is enabled on Blynk, it checks the PIR sensor and sends a notification to Blynk if motion is detected.

    • We use Blynk.virtualWrite(V1, "Somebody in your house! Please check!"); to change the text of a label.

    • Use Blynk.logEvent("intrusion_detected"); to log event to Blynk.


    void myTimerEvent() {
       sendData();
    }
    
    void sendData() {
       if (awayHomeMode == 1) {
          state = digitalRead(sensorPin);  // Read the state of the PIR sensor
    
          Serial.print("state:");
          Serial.println(state);
    
          // If the sensor detects movement, send an alert to the Blynk app
          if (state == HIGH) {
            Serial.println("Somebody here!");
            Blynk.virtualWrite(V1, "Somebody in your house! Please check!");
            Blynk.logEvent("intrusion_detected");
          }
       }
    }
    

Reference