Simple ESP32-based alarm with ESPHome firmware

Another example of an elementary device based on an ESP32 microcontroller is a home alarm system. The device consists of only three elements: a microcontroller, a motion sensor, and a buzzer. It can be installed in a drawer, closet, and even at the house's front door. If the sensor detects motion, the mobile app will alert you via push notification.

Assembling the device

The following elements will be required for assembling a home alarm system:

  • ESP32 microcontroller,
  • motion sensor,
  • buzzer.

Additionally, you will need wires to connect these elements. We also used a breadboard to facilitate device assembly and testing. In general, however, a breadboard is an optional component. Without it, the device becomes more compact and it is more convenient to store it in a desk drawer or closet.

Here's how a ready-made alarm looks like on a breadboard:

A ready-made alarm on a breadboard

A ready-made alarm on a breadboard

A ready-made alarm on a breadboard

Writing firmware for alarm system

We wrote the firmware for the device using the ESPHome tool. Here is the complete firmware code that you can use to build a similar device:

switch:
    - platform: gpio
    id: buzzer
    name: buzzer
    pin:
        number: GPIO22
        inverted: true
    - platform: gpio
    id: state
    name: state
    pin:
        number: GPIO2
binary_sensor:
- platform: gpio
    pin: GPIO18
    name: "PIR Sensor"
    device_class: motion
    on_press:
    then:
        - mqtt.notify:
                    payload: |-
                    root["logLevel"] = "info";
                    root["message"] = "Motion Detected";
        - if:
            condition:
            switch.is_on: state
            then:
            switch.turn_on: buzzer

    on_release:
    then:
        - mqtt.notify:
                    payload: |-
                    root["logLevel"] = "info";
                    root["message"] = "Horizont is clear";
        - if:
            condition:
            switch.is_on: state
            then:
            switch.turn_off: buzzer


    filters:
    - delayed_on: 100ms

The device is based on the 2Smart Cloud platform, which supports ESPHome as a tool for creating professional IoT products. We just pasted this code into the editor window in the vendor's board. After that, we downloaded the test firmware, flashed the device, and configured the interface of the mobile application for alarm control.

These features are free if you connect no more than 20 devices to the platform. The mobile app is also a free feature of 2Smart Cloud.

2Smart Cloud vendor's board

How the alarm system works

By default, the device detects motion using a sensor and sends notifications to the mobile application about each occurrence.

2Smart Cloud mibile app

The Motion Sensor widget on the app screen also indicates that motion has been detected.

2Smart Cloud mibile app

2Smart Cloud mibile app

Additionally, an observing mode is provided; if it is turned on, the alarm is activated. As soon as the device detects motion, the buzzer starts beeping, and a notification is sent to the mobile application.

The buzzer can be turned off. For this, we have also added a separate widget.

2Smart Cloud supports integration with voice assistants in a mobile phone. Therefore, we can activate the alarm with the phrase: “OK Google, turn on the guard.” And if you use an iPhone, you can set up a shortcut that will automatically turn on the alarm when you leave home and turn it off when you return.

© All Rights Reserved            updated 2023-10-12 16:07:29