A Panic Button is used to send an emergency signal to the police or nearby people in an emergency situation like cardiac arrest or any serious health issue where urgent help is needed or the situation where you are attacked by someone or thieves break into house etc. In India, government made it mandatory to have panic button in mobile phone, which when pressed, will call the police with the current location of the person in danger.
So here in this project we will make a physical panic button using ESP8266-01 Wi-Fi module which will send an alert email to predefined Email IDs when pressed.
IoT Based Panic Button Circuit Diagram
Circuit diagram for this IoT panic button is given below:
Components Required
- ESP8266-01
- PUSH Buttons
- Puff Board
- Soldering wire
- FTDI Serial Adapter Module
- Connecting wires
ESP8266-01 Wi-Fi module
ESP8266-01 is a Wi-Fi module and has a onboard microcontroller manufactured by Espressif. This module help microcontrollers to get connect to the Wi-Fi Network using AT commands through UART communication. There is a range of Wi-Fi modules from ESP-01 to ESP-12 by Espressif Systems, but ESP-01 is very low cost and easily available in the market. It has 8 pins -Two UART Pins (RX and TX), two GPIO pins, one Reset and one CH_PD and two for 3.3 V and Ground.
Programming ESP-01
ESP8266-01 can be programmed in many ways but in this project we will be programming it with FTDI Serial Adapter Module. You can also use USB to TTL converter to program it. ESP8266 is not breadboard friendly so here I have soldered all the components on a perf board as per above circuit diagram. This board can be used to program ESP8266 in future projects. Check the detailed tutorial on how to program ESP-01 using FTDI module.
Here a Reset button with pull up resistor is used to reset the Wi-Fi module and a jumper switch is used to switch the module in programmable mode. During programming mode, GPIO0 PIN (Flash pin) is made grounded with the help of Jumper cap, and when left open it can be used as GPIO pin. One push button is used as panic button which will be pressed to send an alert mail to some person in case of emergency.
Configure SMTP2GO to send Email
SMTP (Simple Mail Transfer Protocol) is a platform used to send and receive large number of emails from remote locations automatically. Due to its fast and reliable service it is mostly used by developers and marketers to save their time in sending emails in a secured way. Its servers and data centers are all around the world which helps it to select the nearest server and hence provides fastest connection in sending and receiving emails. It can be used in IOT projects to send emails automatically when a particular task is occurred. In this project we will be using SMTP2GO to send emails alerts when fire is detected by the flame sensor.
Setting up SMTP2Go:
Go to https://www.smtp2go.com/ and click on Try SMTP2GO Free.
Fill the details with your name, email id and password and click on Submit. Then it will redirect to a page where it will ask you to activate SMTP2GO.
Go to your mail box and click on the mail received by SMTP2GO. Click on Activate Account.
Now enter the username which is your email id and password. A new page will open with your username. Save these username and password in a notepad file which will be needed later in Arduino IDE code. Now click on Settings and then Click on Users. A new page will open with your user name and SMTP server details. Save the SMTP server and SMTP Port as these will be used in Arduino code to connect with SMTP server.
Encoding to Base64 Value
Before sending username and password to SMTPTOGO server, we need to encode them into Base64. To encode them we will used the https://www.base64encode.org/.
Here just enter the username and password you want to encode into base64 and Click on Encode to generate the encoded values. Copy and save the encoded values.
For instance, is your user name is “sender@xyz.com” then enter this username in the given text area and then click on encode. The encoded base64 value is “cGFzc3dvcmQ=”. Similarly do it for password.
We previously used SMTP2GO service to send email on fire detection.
Working of Emergency Panic Button
Connections for this ESP8266 based Panic Button are already shown in the circuit diagram above. Here The RX pin of FTDI is connected to TX pin of ESP-01 and TX pin of FTDI is connected to RX pin of ESP-01. Since the output voltage we get from FTDI is around 4 volts therefore it can’t be used to power the ESP-01 as it will damage it. So we will use a 3.3V DC from a breadboard power supply which takes input power from a adapter and provides a dual output of 5 volts and 3.3 volts. This bread board power supply module is created by following this tutorial on CircuitDigest.
Make sure to connect the grounds of FTDI and ESP-01 to the ground of power supply module. One push button is connected at GPIO2 pin which will used to send a mail in case of emergency. One end of button is connected to 3.3 power supply using a resistor and one end is connected to the ground.
Now copy the complete code given in the end of the tutorial and paste in the Arduino IDE. Replace the Wi-Fi credentials with your SSID and password. Change the username and password of SMTP2GO to Base 64 value as shown above. Connect the GPIO0 of flash pin to ground for programming mode and connect the FTDI with PC using USB port. Click on upload and press the reset button 2-3 times. After successfully uploading the code, disconnect the GPIO0 pin from the ground and press Reset button once and open the serial monitor to see the status messages. After connecting to Wi-Fi, press the panic button and you will receive the mail after few seconds.
Code Explanation
Complete code with a working video is given at the end of this tutorial, here we are explaining the complete program to understand the working of the project.
Start with including the ESP Library
#include <ESP8266WiFi.h>
Define variables for SSID and password of Wi-Fi network. Since we need to connect to SMTP server, a variable is declared for the address of the server.
const char* ssid = "Enter your ssid"; const char* password = "enter your password"; char server[] = "mail.smtp2go.com"; const int button = 2; WiFiClient Client; //define wifi client as client
Connecting with Wi-Fi
Serial.print("Connecting To: "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi Connected."); Serial.print("IP address: "); Serial.println(WiFi.localIP());
The “pressed” variable stores the digital input from the panic button. If someone pressed the button then it sends an email by calling the function sendEmail().
int pressed = digitalRead(button); Serial.println(pressed); if(pressed == 0) { sendEmail(); Serial.print("Mail sent to:"); Serial.println(" The recipient"); Serial.println(""); }
Client.connect() function which takes SMTP Server and SMTP Port to connect to SMTP server. It gives “1” if the connection establishes successfully and acknowledge the server with EHLO command.
if (Client.connect(server, 2525) == 1) // connect to smtp server with port address 2525 { Serial.println(F("connected to server")); } else { Serial.println(F("connection failed")); return 0; } Client.println("EHLO www.example.com");
Send the encoded user name and password to the server. The user name and the password from the SMTP2GO which we pasted in notepad are encoded to base64 value. The procedure to encode into base64 value is shown above.
Client.println("AUTH LOGIN"); Serial.println(F("Sending User")); Client.println("c2VuZGVyQHh5ei5jb20= "); //base64, ASCII encoded SMTP Username Serial.println(F("Sending Password")); Client.println("cGFzc3dvcmQ="); //base64, ASCII encoded SMTP Password
After successful authentication, here the complete email is formed with fields like “To”, “From”, “Subject”, “Body”.
Client.println(F("To: receiver@xyz.com ")); Client.println(F("From: sender@xyz.com ")); Client.println(F("Subject: Panic Button - Alert !!!\r\n")); Client.println(F("This is a alert message from your grandfather.")); Client.println(F("He is not well. Please take him to hospital immediately"));
After sending the data QUIT command is sent to complete the email.
Serial.println(F("Sending QUIT")); Client.println(F("QUIT")); if (!emailResp()) return 0; Client.stop();
This is how a DIY IoT Panic button can be made easily using ESP-01 module.
Check the Demonstration video below.
#include <ESP8266WiFi.h>
#define WLAN_SSID "CircuitLoop"
#define WLAN_PASS "circuitdigest101"
char server[] = "mail.smtp2go.com";
WiFiClient Client;
const int button = 2;
void setup() {
pinMode(button,INPUT);
Serial.begin(9600);
delay(10);
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
int pressed = digitalRead(button);
Serial.println(pressed);
if(pressed == 0)
{
sendEmail();
Serial.print("Mail sent to:");
Serial.println(" The recipient");
Serial.println("");
}
}
byte sendEmail()
{
if (Client.connect(server, 2525) == 1) // connect to smtp server with port address 2525
{
Serial.println(F("connected to server"));
}
else
{
Serial.println(F("connection failed"));
return 0;
}
if (!emailResp()) // if connection failed return now
return 0;
//
Serial.println(F("Sending EHLO"));
Client.println("EHLO www.example.com"); // Send command EHLO previosly it was HELO********************
if (!emailResp())
return 0;
Serial.println(F("Sending auth login"));
Client.println("AUTH LOGIN");
if (!emailResp())
return 0;
//
Serial.println(F("Sending User"));
Client.println("c2VuZGVyQHh5ei5jb20="); //base64, ASCII encoded SMTP Username
if (!emailResp())
return 0;
Serial.println(F("Sending Password"));
Client.println("cGFzc3dvcmQ="); //base64, ASCII encoded SMTP Password
if (!emailResp())
return 0;
Serial.println(F("Sending From"));
Client.println(F("MAIL From: sender@xyz.com"));
if (!emailResp())
return 0;
// change to recipient address
Serial.println(F("Sending To"));
Client.println(F("RCPT To: receiver@xyz.com"));
if (!emailResp())
return 0;
Serial.println(F("Sending DATA"));
Client.println(F("DATA"));
if (!emailResp())
return 0;
Serial.println(F("Sending email"));
Client.println(F("To: receiver@xyz.com "));
Client.println(F("From: sender@xyz.com "));
Client.println(F("Subject: Panic Button - Alert !!!\r\n"));
Client.println(F("This is a alert message from your grandfather."));
Client.println(F("He is not well. Please take him to hospital immediately"));
//Client.println(F("Third line of the test e-mail."));
//
Client.println(F("."));
if (!emailResp())
return 0;
//
Serial.println(F("Sending QUIT"));
Client.println(F("QUIT"));
if (!emailResp())
return 0;
//
Client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte emailResp()
{
byte responseCode;
byte readByte;
int loopCount = 0;
while (!Client.available())
{
delay(1);
loopCount++;
// Wait for 20 seconds and if nothing is received, stop.
if (loopCount > 20000)
{
Client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
responseCode = Client.peek();
while (Client.available())
{
readByte = Client.read();
Serial.write(readByte);
}
if (responseCode >= '4')
{
// efail();
return 0;
}
return 1;
}