Road safety is of paramount importance but even then, the accidents are bound to occur due to a variety of reasons like driver error, bad roads, or any stray animal, etc. The time just after the accident is very critical for the people involved as if they can get prompt medical help, the chances of survival will be much higher than if the medical help gets delayed. So a system is necessary which can detect and send an emergency message to the required authorities so that the people involved gets the necessary help.
In this project, we are going to build an IoT based accident detection system with the help of NodeMCU ESP8266 Module and a Vibration Sensor Module SW-420 which will detect the accident and send an Emergency Email to the registered Email address using MATLAB Thingspeak Server and IFTTT service along with any additional emergency alarm system. SW-420 is preferred here over any accelerometer due to its switch like operating system and lower cost.
Components Required
- NodeMCU ESP8266
- SW-420 Vibration Sensor Module
- USB cable for uploading the program
- Jumper Wires
- Breadboard
Circuit Diagram
The circuit diagram for our project here. As shown the SW-420 Module is powered by Vin pin and Its Output is recorded by the D4 pin of NodeMCU. An onboard warning LED is also powered by the D0 pin of the board.
SW-420 Vibration Sensor
The Grove - Vibration Sensor (SW-420) is a high sensitivity non-directional vibration sensor, which can work from 3.3V to 5V. The sensor uses the LM393 comparator to detect the vibration over a threshold point and provide digital data, Logic Low or Logic High, 0, or 1. When the module is stable, the circuit is turned on and the output is high. When the movement or vibration occurs, the circuit will be briefly disconnected and output low. At the same time, you can also adjust the sensitivity according to your own needs. There are two LEDs on the board, one for the Power state and another for the sensor’s output. In this project, we will use 5V to power the module.
Thingspeak
ThingSpeak™ is an IoT analytics service that allows you to aggregate, visualize, and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by your devices to ThingSpeak. With the ability to execute MATLAB® code in ThingSpeak, you can perform online analysis and process data as it comes in. ThingSpeak is often used for prototyping and proof-of-concept IoT systems that require analytics. You can send data from any internet-connected device directly to ThingSpeak using a Rest API or MQTT. With ThingSpeak, you can store and analyze data in the cloud without configuring web servers, and you can create sophisticated event-based email alerts that trigger based on data coming in from your connected devices. You first need to create an account with your email address and then proceed.
To get your data from the sensor to the Thingspeak Server, you need to first create a channel on the server, for this, go to Channels> My Channels> New Channel and fill out the details, it may be changed later so don’t worry. Fill out the field labels with the names of the parameters, you are sending to the thingspeak server.
Now to send the data, you need a unique API key which will later be used in the code to send the data to the server. For that, go to My Channels> (Select the channel you just created)> API Keys.
Now your channel is all set to receive the data from your device.
Programming NodeMCU with Arduino IDE
To program the NodeMCU, you have to connect to your PC with a Micro USB B type cable and open Arduino IDE. To program the NodeMCU, you first need to go to File-> Preferences->Settings.
Enter https://arduino.esp8266.com/stable/package_esp8266com_index.json into the ‘Additional Board Manager URL’ field and click ‘Ok’.
Now go to Tools->Board->Boards Manager and type esp in the search bar, there ESP8266 will be listed, select the latest version of the board and download it.
After installation is complete, go to Tools >Board >and select NodeMCU 1.0(ESP-12E Module). Now you can program NodeMCU with Arduino IDE.
Setting Up the IFTTT alert Email
To get an Email alert if the accident condition occurs, you need to set up the IFTTT alert using Webhooks, IFTTT Emails, Thingspeak React, and ThingHTTP.
Create an IFTTT Applet
IFTTT is a web service that lets you create applets that act in response to another action. You can use the IFTTT Webhooks service to create web requests to trigger an action. The incoming action is an HTTP request to the webserver, and the outgoing action is a notification in the IFTTT app on your device.
1) Create an IFTTT account if you do not already have one. Log in to your account.
2) Create an applet. Select your user icon, and then click Create.
3) Select the input action. Click the word this.
4) Select the Webhooks service. Enter Webhooks in the search field. Select the Webhooks card. After you select Webhooks as the trigger, click the Receive a web request card to continue.
5) Complete the trigger fields. Enter an event name. This example uses AccidentAlert as the event name. Click Create Trigger.
6) Select the resulting action. Click the word that. The trigger word is now the Webhooks icon.
7) Enter notification in the search bar, and click the Email box. Then select Send me an Email.
8) Click Create action to finish the new applet. For this example, the name of the event is AccidentAlert. IFTTT will pass this name of the event where the variable {{EventName}} occurs in the message. You can also pass data about the event that triggered your message using ingredients. For example, including {{OccurredAt}} adds the event time to your notification.
9) Retrieve your Webhooks trigger information. Select My Applets > Services, and search for Webhooks. Select Webhooks, and then click the Documentation button. You see your key and the format for sending a request. Enter the event name. The event name for this example is TooCold.
https://maker.ifttt.com/trigger/{event}/with/key/XXXXXXXXXXXXXXXXXXXXXX
https://maker.ifttt.com/trigger/TooCold/with/key/XXXXXXXXXXXXXXXXXXXXXX
You can test the service using the test button or by pasting the URL into your browser. Note that the IFTTT event trigger is not always instantaneous.
Create a ThingHTTP
To complete the trigger request, create a ThingHTTP. The ThingHTTP app lets you trigger predefined HTTP requests with an API key and a GET request from the web or a device. For this example, use ThingHTTP to trigger Webhooks at IFTTT.
1) Choose Apps > ThingHTTP, and select New ThingHTTP.
2) Edit your ThingHTTP settings.
Name - Name your ThingHTP.
URL - Enter the URL from the Webhooks documentation. The URL for this example has the form https://maker.ifttt.com/triigger/TooCold/with/key/xxxxxxxxxxxxxxxxxxxxxx.
Method - Enter GET.
3) To send custom values in your message from IFTTT, add value1=YOUR_VALUE to the Body field. Change YOUR_VALUE to the value or text you want to send.
4) Save the ThingHTTP.
Create a React to Your Data
Create a React to trigger the ThingHTTP based on your channel data. You must be the author of the channel used to create a react. If you wish to use the data from any other channel, you must first set up a MATLAB analysis to regularly copy the data from the target channel to your channel.
The React app can evaluate your ThingSpeak channel data and trigger other events. Create an instance of the React app that triggers when the house is too cold. Choose Apps > React, and then click New React.
Name - Name your React.
Test Frequency - Select On Data Insertion.
Condition - Select your temperature channel in the If channel list.
Field - Select field 1, set the requirement to is equal to, and set the condition to be equal to 1.
Action - Select ThingHTTP and choose the name of the ThingHTTP you defined previously.
Options - Select Run action each time condition is met.
Now you have done the react part and request to send the email. Now coming to the sensor part, we need to connect the sensor and start sending the data to the Thingspeak server. The trigger Email generated might look like this as shown below.
Arduino Code
Programming the NodeMCU ESP8266 using Arduino IDE is not a complex task as it has only one sensor which has to be interfaced along with a warning LED. As usual, the complete code along with the working video can be found at the end of the page.
We start by adding the header files required. You should add the ESP8266 Wifi.h file from the link given in the code or from here https://github.com/esp8266/Arduino. Download this and copy the library folder to the library folder of your Arduino IDE. For adding Thingspeask Library, go to Sketch> Include Library> Manage Library and search Thingspeak in the search bar.
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
After including the header files, we will define the macros channel ID and Write API Key we got from the Thingspeak Dashboard along with ON and OFF for making the warning LED ON or OFF. After that define the wifi ID and password of the network you wish to connect to.
#define ON 0
#define OFF 1
#define SECRET_CH_ID 0000000
#define SECRET_WRITE_APIKEY "XYZ"
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const char *ssid = "WIFI name";
const char *pass = "Password";
Now we will define the pins connected to the output of the SW-420 sensor module and LED anode. Vibration Sensor output is connected to the pin D4 of NodeMCU and the D0 pin is connected to the LED Anode leg. We have also defined the previous condition and present condition variables that are to be used in the main code to detect whether the vibration is happening or not.
int vibration_Sensor = D4;
int LED = D0;
int present_condition = 0;
int previous_condition = 0;
Now we declare the pins connected to the Sensor Output i.e D4 as INPUT and LED Anode pin as OUTPUT. Also, we attempt to establish a WIFI connection using the ID and Password provided above and start communication with the Thingspeak Platform. It uses HTTP protocol to send and receive data to the Thingspeak Platform.
Serial.begin(115200);
delay(100);
pinMode(vibration_Sensor, INPUT);
pinMode(LED, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("."); // print "......" till the WIFI not connected
}
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client);
In the loop function, the present and previous conditions are compared. If these two are not the same, the LED starts to blink until the both are same. In starting, the two variables hold 1 and the led remains off during the start of the program. When there is some vibration, the present_condition variable becomes 0 and led start to blink. And again when vibrations stop both the variables become 1 and LED stops blinking. Also each time it sends the data to the channel you created on the Thingspeak platform.
previous_condition = present_condition;
present_condition = digitalRead(D4
int httpCode = ThingSpeak.writeField(myChannelNumber, 1, present_condition, myWriteAPIKey);
if (previous_condition != present_condition) {
led_blink();
Serial.println("Accident Detected");
}
else {
digitalWrite(LED, OFF);
}
This is how you program the NodeMCU to use an SW-420 Vibration Sensor Module to send an Email Alert in case of an accident. You can also use push notification if you are using the IFTTT app or can ask your Google Assistant or Alexa Home to sound an emergency alarm using IFTTT services. It's up to you to play with it.
Code
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#define ON 0
#define OFF 1
#define SECRET_CH_ID 0000000 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "XYZ" // replace XYZ with your channel write API Key
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const char *ssid = "AndroidAP"; // Enter your WiFi Name
const char *pass = "12345668"; // Enter your WiFi Password
int vibration_Sensor = D4;
int LED = D0;
int present_condition = 0;
int previous_condition = 0;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(vibration_Sensor, INPUT);
pinMode(LED, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("."); // print "......" till the WIFI not connected
}
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client);
}
void loop() {
previous_condition = present_condition;
present_condition = digitalRead(D4); // Reading digital data from the D4 Pin of the NodeMCU ESP8266.
// Write value to Field 1 of a ThingSpeak Channel
int httpCode = ThingSpeak.writeField(myChannelNumber, 1, present_condition, myWriteAPIKey);
if (previous_condition != present_condition) {
led_blink();
Serial.println("Accident Detected");
} else {
digitalWrite(LED, OFF);
}
}
void led_blink(void) {
digitalWrite(LED, ON);
delay(250);
digitalWrite(LED, OFF);
delay(250);
digitalWrite(LED, ON);
delay(250);
digitalWrite(LED, OFF);
delay(250);
}