Skip to content

WEMOS Wireless DIY door alarm sensor with Home Assistant article that contains the code, libraries, diagrams, 3D print files and more information promised in the step-by-step youtube video below

License

Notifications You must be signed in to change notification settings

SmartThingsDIY/wemos-door-alarm-sensor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DIY Door Alarm Sensor With Home Assistant...

Or any MQTT based system

About

This article accompanies the "DIY Door Alarm Sensor" YouTube series. It contains the code, libraries, diagrams, 3D print files and more information that I promised in the videos: DIY Door Alarm Sensor

BUY VS BUILD

You can always go ahead and buy a ready-to-use a solution like this Ring Alarm Contact Sensor but these devices usually would only work with their respective apps, and some of them would require you to pay extra in order to receive push notifications, or enable cloud usage. Now, this article and its video provide a step-by-step guide and links to everything you need to build a similar device that accomplishes the same thing.

In addition to detecting when a door or window are open and sending notifications. This device:

  • When motion is detected (door opened), only then, the connection to WiFi is made, again, to save energy
  • Has a removable LiPo battery for easy recharging
  • Sends notifications (and any type of data) to an MQTT topic, enlarging the possibilities of what you can do with the data

In my case, I will be sending the notifications to my Home Assistant setup in order to trigger a pre-defined automation that:

  • Pushes actionable notifications to our phone devices through Home Assistant companion apps
  • Converts notification alarm to a text (Alexa TTS capabilities) and play it through the Amazon Echo devices around the house
  • Turns on the front door lights if it's after sunset and turns them off 2 minutes later

The beauty of this is I can decide to trigger any type of notifications in the future such as playing an alarm sound or sending SMS notifications...

So without further ado, let's start with a list of all hardware components, Apps and Libraries I've used in this project:

⚡️ COMPONENTS AND SUPPLIES

🖥 APPS

📦 Libraries

Hardware Overview: HC-SR04 Ultrasonic Sensor

The Ultrasonic Module HC-SR04 can measure the distance to an object by using echo. It can measure the distance between the sensor and the object by the time from the sound wave of a specific frequency to the time when the sound wave is heard to bounce back

This small module is perfect for all kind of projects that need distance measurements, e.g. avoiding obstacles. It provides 2cm-500cm non-contact measurement function with a ranging hight accuracy that can reach to 3mm

I will be placing this module on my door where the distance to the wall is fixed while the door is closed, but when opened, the distance between the door and the wall obviously increases. This increase in distance will be detected by the HC-SR04 module which will trigger the alarm

HC-SR04 working principle: The module automatically sends eight 40KHz square wave to detect whether a signal is returned, if an object is detected, Echo pin will be high level, and based on the different distance, it will take the different duration of high level So we can calculated the distance easily:

  • The distance = ((Duration of high level) * (Sonic :340m/s)) / 2
  • Distance L = 1/2 × T × C

Hardware Overview: WEMOS R1 D2

I've decided to use the WEMOS R1 D2 microchip for this project because it has a small size that saves space and can easily fit in a printable 3D case, but also because of a few reasons:

  • It has an Analog pin
  • It has WiFi capabilities
  • It supports OTA online: Updating code Over-The-Air
  • 4MB of memory which more than enough for our needs
  • And because it's compatible with Arduino IDE, making the programming easy

Hardware Overview: LiPo Battery

A Lithium Polymer battery, or more correctly Lithium-ion Polymer battery (abbreviated as LiPo, LIP, Li-poly, lithium-poly and others), is a rechargeable battery of lithium-ion technology using a polymer electrolyte instead of a liquid electrolyte. High conductivity semisolid (gel) polymers form this electrolyte. These batteries provide higher specific energy than other lithium battery types and are used in applications where weight is a critical feature, such as mobile devices, radio-controlled aircraft and some electric vehicles.

I had purchased this set of 5 batteries with a charger from Amazon for under $15 and been using them for a previous project without a problem, so that shall be my go to battery for this project as well.

3D PRINTED CASE

No one likes wires hanging around, and so I went ahead looking for a 3D case I can use for this project and luckily found this one on Thingiverse so that's what I'll be using

THE WIRING

The HC-SR04 Ultrasonic Module has 4 pins: Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Wemos Mini respectively. The trig and echo pins to any Digital I/O pin on the Wemos, in our example we use D5 for Trig and D6 for Echo. Please follow the diagram bellow:

Wiring source files are included under wiring folder

THE CODE

The code within main.cpp file is well documented, but I'll try to explain the concepts and ideas behind the code in this section. But first of all, copy the file secrets_copy.h to secrets.h and edit its content with your details: WiFi credentials, Home Assistant details...

The sketch begins with the creation of a few objects we'll need along the way: WiFiClient that we use to connect to Wifi and PubSubClient that we use to send data through MQTT

WiFiClient espClient;
PubSubClient client(espClient);

Then we declare a few variables like the pins used to send the wave and measure the time it take to bounce back, as long as the number of tries we aim to do while connecting to WiFi because we want to avoid draining the battery trying to connect to WiFi indefinitely.

#define NB_TRYWIFI 20 // WiFi connection retries

#define sensorEchoPin D5
#define sensorTrigPin D6

The setup() function make sure the WiFi is disconnected when the board first boots up, and that's because WiFi consumes a lot of energy, so we want to make sure it's only activated when required:

void setup()
{
    Serial.begin(9600);

    disconnectWiFi(); // no need to switch WiFi on unless we need it

    pinMode(sensorTrigPin, OUTPUT);
    pinMode(sensorEchoPin, INPUT);
}

The function that reads the distance is straightforward:

long readSensor()
{
    digitalWrite(sensorTrigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(sensorTrigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(sensorTrigPin, LOW);

    duration = pulseIn(sensorEchoPin, HIGH);

    return duration / 58.2; // The echo time is converted into cm
}

Make sure you have installed an MQTT broker in your HomeAssistant setup beforehand. You can start here: https://www.home-assistant.io/docs/mqtt/broker#run-your-own

Finally

All contribution to this project is appreciated

About

WEMOS Wireless DIY door alarm sensor with Home Assistant article that contains the code, libraries, diagrams, 3D print files and more information promised in the step-by-step youtube video below

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published