Building a DIY Home Alarm System with Arduino and WiFi

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.

Contact the Author

Please sign in to contact this author

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  #include  #include  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 = ""; //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;
  http.begin(client, url); int httpCode = http.GET(); if (httpCode > 0) {
    String payload = http.getString();
  } else {
    Serial.println("Error on HTTP request");

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.

Related Products

+86 18059204998