arduino and esp8266

Voice Connection based Security system with ESP8266 and Arduino for IoT in Home Automation

Table of Contents

INTRODUCTION

There is increasing demand of intelligent and automated system which drives the development of cost effective IoT based solution to improve security. A very exciting project with this field would be an IoT based security system powered by Arduino and ESP8266 which sends an email alert and plays a voice message when it sees any movement. This project furthers the capability of the ESP8266 Wi‐Fi module, PIR sensor, and ISD1820 voice module by combining them to create a reliable remotely controlled security system. In this post I want to explore how this project is built and what are its components.

Objective of the Project

The purpose of this security system is to detect intruders, send out an automated email notification to an email address of choice, and play an ISD1820 pre recorded warning message. The internet can be used to activate or deactivate the system. The work in this project involves studying how ESP8266, PIR sensor and ISD1820 make things work, and use simple electronic components to build the fully operational version of IoT security setup.

Components Required

To build this project, you’ll need the following components:

    • ESP8266 Wi-Fi Module

    • Passive Infrared sensor or PIR sensor.

    • ISD1820 Voice Module

    • LM317, LM7805 etc Voltage Regulators

    • Transistors: BC547 (2 Nos)

    • Resistors: 1K, 200Ω, 330Ω

    • Capacitors: 10µf and 0.1µf

    • 12V Adapter or 9V Battery

    • Breadboard and jumper wires

Key Modules in the Project

1. ESP8266 Wi-Fi Module

ESP8266 is a cheap and powerful Wi-Fi microcontroller, it’s widely used for IoT project. If you are a new comer to ESP8266, the term can go hand in hand with Arduino, where you can upload and program ESP8266 to do different things using Arduino IDE. The ESP8266 in its NodeMCU version provides a simple platform for connecting to the web, control GPIO for electronics and interfacing with multiple sensors and modules. In this project, it functions as the brain of the security system, working with all connected modules and internet to send email alarms.

If you want to start with ESP8266 programming with Arduino IDE then do start reading tutorials on Arduino and ESP8266 integration.

2. PIR Sensor

PIR sensor is must to detect motion. Within the range it can detect infrared radiation emitted from humans and animals. The output is triggered by the heat signature captured by a pyroelectric crystal in the PIR sensor as motion is detected. Also, the sensor has two potentiometers which you can use to adjust its sensitivity and delay time. The module also has a trigger selection pin to choose between “H” mode and “I” mode:

H Mode: If the detected object leaves the area of detection, the output pin of the sensor stays high.

I Mode: When the object moves out of the sensor’s range the output pin goes low.

To maintain consistency with the PIR sensor’s output, H mode is chosen for this project so the PIR outputs a high signal when it detects any motion, even if the object remains in the detection area for a substantial amount of time.

3. ISD1820 Voice Module

A simple voice recording module, the ISD1820 can record audio clips up to 10 seconds. An in-built microphone and speaker is featured in it. It includes three buttons for recording and playback:

REC button: To record an audio clip.

PLAYE button: To have the recorded audio play once.

PLAYL button: To listen to the recorded audio as long as an audio recording button is pressed.

For projects based on microcontroller, the ISD1820’s PLAYE pin can be controlled with a GPIO pin for autonomous playback. For this project, we use GPIO 0 of ESP8266 to trigger the playback of the warning message when the PIR sensor detects any motion.

Arduino and ESP8266 setup

Hardware setup and Circuit Design

The circuit is built using ESP8266 module, PIR sensor, ISD1820 voice module and a 12V adapter provides power to the whole circuit. There is also two voltage regulators and LM317 for 3.3V and an LM7805 for 5V. These are necessary to power ESP8266 and other consumables. Here’s a breakdown of the circuit design:

Power Regulation: The 200Ω, 330Ω resistor combination is connected to configure the LM317 regulator to output 3.3V and LM7805 regulator output 5V to ISD1820 and PIR sensors. The regulators are powered from a 12V adapter.

PIR and Voice Module Control:

The PIR sensor and ISD1820 module are powered ON and OFF via a BC547 transistor and a current limiting resistor at GPIO 2 of the ESP8266.

The input pin connected from the PIR sensor’s Dout pin using a BC547 transistor is the GPIO 0. Should motion be detected, it will read the GPIO 0 high and send an email alert via the ESP8266.

Transistor Switches: The ESP8266 pin outputs are limited, so we use transistors to drive the modules. Also, there are switches to disconnect the modules temporarily from power up to prevent entering a reset loop.

How to build an Email API with IFTTT

The project makes use of the IFTTT (If This Then That) service in order to send email notifications that can be automated through predefined triggers. Here’s how to create an API using IFTTT:

Log in to your IFTTT account.

Search the “Maker Webhooks” service to connect to Webhooks.

Start with Gmail and let’s connect and grant the permissions.

Make an Applet, set the action (This) to Webhooks. Pass in an appropriate event name (e.g., “intrusion_detected”).

That action is set to Gmail. Craft your custom email subject and body text that lets the user know that there was an intrusion.

Take the API url under Webhooks documentation and copy it. We will use this URL in the Arduino code to trigger an email.

The Arduino Code and System functionnality

Using the PIR sensor as the input, it is monitored by ESP8266 programmed with Arduino IDE. If motion is detected, it will trigger the ISD1820 module to say whatever it recorded and make an HTTP GET request to the IFTTT API URL to send an email notification. Here’s a high-level outline of the code functionality:

Sensor and modules initializations.

Using the ESP8266 connect to a Wi-Fi.

Run a PIR sensor’s output, and play a recorded message when motion is detected.

To get an email alert, can trigger a HTTP GET request to IFTTT URL.

Advantages and Use Cases

This IoT-based security system offers several benefits:

Remote monitoring: Using the ESP8266, the system can be remotely armed and disarmed over the internet.

Personalized alerts: The ISD1820 can record your custom warning messages.

Affordable and scalable: The system is cost effective and can be further rendered with additional sensors and notifications.

This paper presents an IoT based Security System using Arduino and ESP8266.

The ability to remotely control and monitor your home alarm system using a simple web interface using an IoT security system controlled with Arduino and the ESP8266 module. The ESP8266 microcontroller is leveraged to operate in Access Point (AP) and Station (STA) modes within the project. The driving interface is a web page that users can visit to ARM or DISARM the system using the ESP8266 which acts as the main controller and ‘makes the connection’.

In this article, we will follow through the project and talked about some of the major asses of how to configure Arduino and ESP8266, how to design the user interface in the form of HTML, and how we can handle actions from the user interface considering the alarm states as well. And we’ll discuss the code, and explain each part, so you can understand it, and modify it.

Step 1: On the ESP8266 side, I configure ESP8266 as AP and STA.

For the project, the ESP8266 works as Access Point and Station. This allows you to set up the module itself to form a local Wi-Fi network to communicate with connected devices while also being able to connect to external networks for cloud based integration.char* ssid = “BPAS home”; // Put your WiFi SSID here.e ESP8266 module, PIR sensor, and ISD1820 voice module, all powered by a 12V adapter. The setup also includes two voltage regulators—an LM317 for 3.3V and an LM7805 for 5V. These are essential for powering the ESP8266 and other components. Here’s a breakdown of the circuit design:

Power Regulation: The LM317 regulator is configured to output 3.3V using a combination of 200Ω and 330Ω resistors, and the LM7805 regulator provides 5V output for the ISD1820 and PIR sensor. A 12V adapter powers the regulators.

PIR and Voice Module Control:

GPIO 2 of the ESP8266 is used to control the power to the PIR sensor and ISD1820 module through a BC547 transistor and a current-limiting resistor.

GPIO 0 serves as the input pin connected to the PIR sensor’s Dout pin through a BC547 transistor. When motion is detected, GPIO 0 reads a high signal, triggering an email alert via the ESP8266.

Transistor Switches: The output current of the ESP8266 pins is limited, so transistors are used to drive the modules. Additionally, switches are added to disconnect the modules temporarily during power-up to avoid entering a reset loop.

Building an Email API with IFTTT

To send email notifications, the project leverages the IFTTT (If This Then That) service, which can automate tasks based on predefined triggers. Here’s how to create an API using IFTTT:

Log in to your IFTTT account.

Connect to Webhooks by searching for the “Maker Webhooks” service.

Connect to Gmail and provide the necessary permissions.

Create a new Applet and set the trigger (This) to Webhooks. Use a relevant event name (e.g., “intrusion_detected”).

Set the action (That) to Gmail. Create a custom email subject and body text indicating an intrusion.

Copy the API URL provided under Webhooks documentation. This URL will be used in the Arduino code to trigger an email.

Arduino Code and System Operation

The ESP8266, programmed using the Arduino IDE, monitors the output from the PIR sensor. When motion is detected, it triggers the ISD1820 module to play the recorded voice and sends an HTTP GET request to the IFTTT API URL to dispatch an email notification. Here’s a high-level outline of the code functionality:

Initialize sensors and modules.

Connect to Wi-Fi using the ESP8266.

Monitor PIR sensor output and play the recorded message when motion is detected.

Trigger an HTTP GET request to the IFTTT URL to send an email alert.

Advantages and Use Cases

This IoT-based security system offers several benefits:

Remote monitoring: The system can be armed and disarmed remotely over the internet using the ESP8266.

Personalized alerts: You can record custom warning messages using the ISD1820.

Affordable and scalable: The system is cost-effective and can be expanded to include additional sensors and notifications.

Implementing an IoT-Based Security System Using Arduino and ESP8266

Creating an IoT-based security system using Arduino and ESP8266 enables remote monitoring and control over your home’s alarm system through a simple web interface. The project leverages the ESP8266 microcontroller, which is configured to operate in both Access Point (AP) and Station (STA) modes. The ESP8266 acts as the main controller and establishes a Wi-Fi connection, allowing users to access a web page to ARM or DISARM the system.

In this article, we’ll explore the project, focusing on the key aspects of configuring Arduino and ESP8266, designing a user interface using HTML, and implementing actions based on alarm states. We will discuss the code and explain each segment to provide clarity and enable modifications.

Step 1: Configuring the ESP8266 as AP and STA

In the project, the ESP8266 acts as both an Access Point and Station. This enables the module to establish a local Wi-Fi network for communication with connected devices, while also being able to connect to external networks for cloud-based integration.

Setting Up Wi-Fi Credentials

The following lines of code are crucial for configuring the Wi-Fi connection:

const char* ssid = “BPAS home”;  // Enter your Wi-Fi SSID here

cout <<“Password is : “<< const char* password = “cracksen”; //Enter your password here ;

Change the ssd and password to a value of your choice (to match your router’s credentials). This allows for an ESP8266 to join your network.

To get the ESP8266’s IP Address.

The following code segment should be used to find the ESP8266’s IP address which has been assigned to it. This helps in determining the URL for accessing the webpage interface:

the Serial.print(“Connected to “);

Serial.println(ssid);

Serial.print(“IP address: “);

It’s all quite legal, although the Arduino specifically states the Serial.println(WiFi.localIP()); line above may be slow to respond, as it must wait for the WiFi module to answer.

The module will show the IP address that it got from the router through accessing the Arduino IDE’s Serial Monitor. To access alarm control page from browser, this IP address will be used.

Step 2: The Webpage using HTML

The sim solution is next, and consists of designing a simple webpage using HTML as the user interface to control the alarm system. You can make the webpage your own way. Below is an example of a basic HTML structure:

mainPage += “<h1 align=\”center\”>IoT-based Security System</h1>”;

mainPage += “<h2 align=\”center\”>-CircuitDigest</h2>”;

mainPage += “<h1 align=\”center\”><p>Alarm Status: “;

mainPage += “<a href=\”switch2On\”><button>ARM</button></a>&nbsp;”;

mainPage += “<a href=\”switch2Off\”><button>DISARM</button></a></p>”;

feedback = “<h3 align=\”center\”>Click on ARM to enable the security system</h3>”;

This HTML code creates a webpage with buttons to ARM or DISARM the system. You can add more features or customize the appearance by editing this segment.

Step 3: Programming Actions for ARM and DISARM

When users click on the ARM or DISARM buttons, specific actions are executed based on the URL path. The following code snippets demonstrate the implementation:

server.on(“/switch2On”, [](){

    feedback = “<h3 align=\”center\”>Alarm is up and running</h3>”;

    currentPage = mainPage + feedback;

    server.send(200, “text/html”, currentPage);

    currentPage = “”;

    digitalWrite(GPIO_2, HIGH); // Turn on PIR and Voice module

    power_module = true;

    delay(1000);

});

server.on(“/switch2Off”, [](){

    feedback = “<h3 align=\”center\”>Alarm is down</h3>”;

    currentPage = mainPage + feedback;

    server.send(200, “text/html”, currentPage);

    currentPage = “”;

    digitalWrite(GPIO_2, LOW); // Turn off PIR and Voice Module

    power_module = false;

    delay(1000);

});

In this section, when the ARM button is clicked, it triggers the /switch2On URL, which executes the following actions:

  • Updates the webpage feedback message to indicate that the alarm is active.
  • Sets GPIO 2 to HIGH, which turns on connected modules such as the PIR sensor and Voice Module.
  • Waits for a second to ensure the changes are applied.

Conversely, the /switch2Off URL turns off the GPIO pin and updates the webpage to indicate that the alarm is now disabled.

Integrating with IFTTT

A crucial part of the project is integrating the security system with IFTTT (If This Then That). This allows sending notifications or performing specific actions based on alarm triggers. You’ll need an IFTTT webhook URL, which you can obtain from the IFTTT website.

String url = “/trigger/123/with/key/mDsoOV_EERS3xRfrh3_UQBhbcx0qlRHns-z2qXXXXX”; // Use your own IFTTT URL

This line sends a request to the IFTTT webhook when the alarm is triggered, enabling email notifications or SMS alerts based on your settings.

Step 4: Uploading the Code to ESP8266

It is quite easy, as we can use many ways such as using the Arduino IDE, or using the Python in MicroPython.

After all the hardware ready and the code done uploading to the ESP8266 via the Arduino IDE and FTDI board. If you find the upload is problematic then reset the module and try again. Once the file is uploaded open the Serial Monitor to confirm that the ESP8266 is connected to Wi-Fi and displaying its IP address.

Testing and working of the security system

Once you’ve successfully uploaded the program, you can plug the ESP8266 onto a perf board and power it up. Check the connection status and obtain you ip address using serial monitor. Once you’ve done that, go to the webpage using the IP address and you will see the ARM and DISARM buttons.

Enabling the system lets the PIR sensor learn in around 50-60 seconds. After those it is ready to detect motion. Once the intruder enters the sensor’s range an audio alert is triggered and an email is sent to your registered ID through IFTTT with the time and date stamp of the event.

Conclusion and Summary of Project

This Arduino and ESP8266 project allows us to remotely control a security system via the most efficient, simple and affordable manner i.e without requiring an expensive, extra, small machine. With the flexibility of Arduino and ESP8266 connectivity features, combining these two can give you a smart home solution that’s cost effective but easily customizable. By using basic HTML-based webpage interface, it is easy to remote control the robot and integration with services like IFTTT allows its functionality to be improved with alerts and notifications.

If you have any questions or feedback about this project do share it in the comments below, I’ll be glad to help! Also, make sure to check out the video below to see how this project actually works completed. Happy building!


/*Program for : IOT based Security system system with Voice warning

BY ElectroGlobal  */

 

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <WiFiClientSecure.h>

#include <ESP8266WebServer.h>

#include <ESP8266mDNS.h>


MDNSResponder mdns;


boolean trigger_sms = false;

boolean power_module = false;



const char* ssid = "BPAS home";  //Enter you Wifi SSID here

const char* password = "cracksen"; //Enter your password here


 


ESP8266WebServer server(80);


String mainPage = ""; //The default page

String feedback = ""; //Gives staus of the switch

String currentPage = ""; //Combines main and feedback page


int GPIO_0 = 0; //Pin defanition

int GPIO_2 = 2; //Pin defanition


void setup(void){

  //HTML code for webpage//

  mainPage += "<h1 align=\"center\">IOT based security System</h1><h2 align=\"center\">-CircuitDigest</h2><h1 align=\"center\"><p>Alarm Status: <a href=\"switch2On\"><button>ARM</button></a>&nbsp;<a href=\"switch2Off\"><button>DISARM</button>  </a></p>";

  feedback = "<h3 align=\"center\">Click on ARM to enable the security system</h3>";

  //End of HTML code//

  currentPage = mainPage+feedback;

  

  // preparing GPIOs

  pinMode(GPIO_0, INPUT); //feeded by PIR sensor

  pinMode(GPIO_2, OUTPUT); //used to turn on PIR and Voice module

  digitalWrite(GPIO_2, LOW);

  

  delay(1000);

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  Serial.println("");


  // Wait for connection

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  // config static IP



  Serial.println("");

  Serial.print("Connected to ");

  Serial.println(ssid);

  Serial.print("IP address: ");

  Serial.println(WiFi.localIP()); //Serial monitor will give the IP addrss of your ESP module

  

  if (mdns.begin("esp8266", WiFi.localIP())) {

    Serial.println("MDNS responder started");

  }

  

  server.on("/", [](){

    currentPage = mainPage+feedback;

    server.send(200, "text/html", currentPage);

    currentPage = "";

  });



  

  server.on("/switch2On", [](){

    feedback = "<h3 align=\"center\">Alarm is up and running </h3>"; //HTML code modification

    currentPage=mainPage+feedback;

    server.send(200, "text/html", currentPage);

    currentPage="";

    digitalWrite(GPIO_2, HIGH); //Turn on PIR and Voice module

    power_module=true;

    delay(1000);

  });

  server.on("/switch2Off", [](){

    feedback = "<h3 align=\"center\">Alarm is down</h3>"; //HTML code modification 

    currentPage=mainPage+feedback;

    server.send(200, "text/html", currentPage);

    currentPage="";

    digitalWrite(GPIO_2, LOW); //Turn off PIT and Voice Module

    power_module=false;

    delay(1000); 

  });



  server.begin();

  Serial.println("IOT security system is up and running");

}


//Function to send an E-mail//

void send_Email()

{

const char* host = "maker.ifttt.com"; //The host of API URL will be the same for all

const int httpsPort = 443;


   WiFiClientSecure client;

  Serial.print("connecting to ");

  Serial.println(host);

  if (!client.connect(host, httpsPort)) {

    Serial.println("connection failed");

    return;

  }


 


  String url = "/trigger/123/with/key/mDsoOV_EERS3xRfrh3_UQBhbcx0qlRHns-z2qXXXXX"; //Must change it to your API URL

  Serial.print("requesting URL: ");

  Serial.println(url);


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +

               "Host: " + host + "\r\n" +

               "User-Agent: BuildFailureDetectorESP8266\r\n" +

               "Connection: close\r\n\r\n");


  Serial.println("request sent");

  while (client.connected()) {

    String line = client.readStringUntil('\n');

    if (line == "\r") {

      Serial.println("headers received");

      break;

    }

  }

  String line = client.readStringUntil('\n');


  Serial.println("reply was:");

  Serial.println("==========");

  Serial.println(line);

  Serial.println("==========");

  Serial.println("closing connection");

}

//Function to send an E-mail


 

void loop(void){

  server.handleClient();


  if (digitalRead(GPIO_0==HIGH) && power_module==true)

  trigger_sms=true;

  else

  trigger_sms=false;

  

  if (trigger_sms==true)

  {

    send_Email(); //If the PIR module is powered and if a person is detected send a E-mail

    trigger_sms=false;

    delay(2000);

  }

  

}
ESP8266 Based Air Pollution Detector
Arduino Ide | Building a Wireless Bluetooth Controlled Robot Car with Arduino ide : A Complete Instruction

Leave a Reply

WhatsApp
My Cart
Wishlist
Recently Viewed
Categories