Building a DIY Home Alarm System with Arduino and WiFi

Learn how to build your own home alarm system using an Arduino, a motion sensor, and a WiFi module. This step-by-step guide will show you how to set up the hardware and write the code to send notifications to your phone or email when the alarm is triggered. Keep your home safe and secure with this simple DIY project!

Building a DIY Home Alarm System with Arduino and WiFi
Building a DIY Home Alarm System with Arduino and WiFi

Here's a brief explanation of the code:

  • We first include the necessary libraries for Wi-Fi and HTTPClient.
  • We then define our Wi-Fi network name and password, as well as the URL of our server.
  • We define the pin to which our motion sensor is connected and set it to input mode.
  • We then set up the Wi-Fi connection and wait until we are connected to the network.
  • In the loop function, we check the state of the motion sensor. If it detects motion and the alarm is not already triggered, we send a notification to the server and set the alarmTriggered variable to true. If the motion sensor detects no motion and the alarm is triggered, we set alarmTriggered to false.
  • The sendNotification function takes a message parameter, constructs a URL with the message as a parameter, and sends an HTTP GET request to the server. The server can then process this request and send a notification to the appropriate parties.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "your-ssid"; //replace with your Wi-Fi network name
const char* password = "your-password"; //replace with your Wi-Fi network password

const char* serverName = "http://yourserver.com/"; //replace with your server URL

const int motionSensorPin = D1; //motion sensor connected to pin D1
bool alarmTriggered = false;

void setup() {
  Serial.begin(115200);
  pinMode(motionSensorPin, INPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi!");
}

void loop() {
  if (digitalRead(motionSensorPin) == HIGH && !alarmTriggered) {
    Serial.println("Motion detected! Alarm triggered!");
    sendNotification("Motion detected in your home!");
    alarmTriggered = true;
  }
  else if (digitalRead(motionSensorPin) == LOW && alarmTriggered) {
    Serial.println("Motion stopped. Alarm deactivated.");
    alarmTriggered = false;
  }
  delay(100);
}

void sendNotification(String message) {
  WiFiClient client;
  HTTPClient http;
  String url = serverName + "?message=" + message;
  Serial.println(url);
  http.begin(client, url);
  int httpCode = http.GET();
  if (httpCode > 0) {
    String payload = http.getString();
    Serial.println(payload);
  }
  else {
    Serial.println("Error on HTTP request");
  }
  http.end();
}

Note that this is just a basic example code and you may need to modify it to fit your specific use case. Also, make sure to properly secure your Wi-Fi network and server to prevent unauthorized access.

Our website is for sale! The domain and website has been running for more than 3 years, but it's time to part with it. There is no price, so I consider any offer. Contact us if you are interested in buying in the feedback form, we will discuss options, price and details for transferring the site. (script, database and everything else for the site to work).