Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

2.15 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.

COMPONENT INTRODUCTION

PURCHASE LINK

ESP32 WROOM 32E

BUY

ESP32 Camera Extension

-

Several Jump Wires

BUY

PIR Motion Sensor Module

BUY

1. Circuit Assembly¶

../_images/iot_9_blynk_bb.png

2. Blynk Configuration¶

2.1 Initializing Blynk

  1. Go to the BLYNK page and select Sign Up FREE or Enterprise Solution.

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

    ../_images/09_blynk_sign_in.png
  3. Check your email for a prompt, and click on the Create Password link in the email to set your password.

    ../_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
  5. After completing the Blynk Tour, a window will pop up where you can choose to Explore Blueprints or click Quick Start to quickly connect your device. However, in this case, we will select “Have a look around first”.

    ../_images/09_blynk_skip.png

2.2 Template Creation

  1. Start by creating a template in Blynk. Follow the steps to set up the Intrusion Alert System template.

    ../_images/09_create_template_1_shadow.png
  2. Name your template, choose ESP32 as the Hardware, select WiFi as the Connection Type, and then click Done.

    ../_images/09_create_template_2_shadow.png
  3. Enter the template, where you’ll be prompted with next steps. Click Configure template to upload a cover image, enhance the description, and more. Follow the remaining three steps to complete the setup.

    ../_images/09_blynk_temp_steps.png

2.3 Set Up Datastreams

  1. Open the newly created template and go to the datastream setup page.

    ../_images/09_blynk_new_datastream.png
  2. Click New Datastream, and in the popup, select Virtual Pin.

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

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

    ../_images/09_datastream_1_shadow.png

2.4 Set up the Web Dashboard

  1. Drag and drop both the Switch widget and the Label widget onto the Web Dashboard.

    ../_images/09_web_dashboard_1_shadow.png
  2. Hover over a widget to see three icons. Use the Settings icon to configure the widget’s properties.

    ../_images/09_blynk_dashboard_set.png
  3. Configure the Switch widget to link with the AwayMode(V0) datastream, setting the ONLABEL and OFFLABEL to display “away home” and “at home”, respectively.

    ../_images/09_web_dashboard_2_shadow.png
  4. In the Label widget settings, link it to the Current Status(V1) datastream.

    ../_images/09_web_dashboard_3_shadow.png

2.5 Setting Up an Event

  1. Click Events & Notifications and then Create Event.

    ../_images/09_blynk_event_add.png
  2. Name the event and specify its code. Choose Warning for TYPE and provide a short description for the notification email. Adjust notification frequency as desired.

    Note

    Ensure the EVENT CODE is set as intrusion_detected. Any changes here require corresponding code adjustments.

    ../_images/09_event_1_shadow.png
  3. Go to the Notifications section to enable notifications and configure email settings.

    ../_images/09_event_2_shadow.png
  4. Under Settings, define how frequently an event triggers notifications and set intervals according to your preference. Remember to click Create to save your settings.

    ../_images/09_event_3_shadow.png

2.6 Saving the Template

  1. Remember to save your changes to the template.

    ../_images/09_save_template_shadow.png

2.7 Making a Device

  1. It’s time to create a new device from the template.

    ../_images/09_blynk_device_new.png
  2. Select From template to begin.

    ../_images/09_blynk_device_template.png
  3. Choose the Intrusion Alert System template and click Create.

    ../_images/09_blynk_device_template2.png
  4. Note down the Template ID, Device Name, and AuthToken for your ESP32 integration.

    ../_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. Download this code or copy this code to the Arduino IDE directly.

  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