Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
đ Ready to explore and create with us? Click [here] and join today!
Vibration Alert System with IFTTTď
This project sets up a vibration detection system using an Arduino board (Uno R4 or R3) with an ESP8266 module and a vibration sensor (SW-420). When a vibration is detected, the system sends an HTTP request to an IFTTT server, potentially triggering various actions such as sending a notification or an email.
To avoid excessive alerts within a short timeframe, the system has been programmed to send these HTTP requests at a minimum interval of 2 minutes (120000 milliseconds). This interval could be adjusted based on the userâs needs.
1. Build the Circuitď
Note
The ESP8266 module requires a high current to provide a stable operating environment, so make sure the 9V battery is plugged in.

2. Configure IFTTTď
IFTTT is a private commercial company founded in 2011 that runs online digital automation platforms which it offers as a service. Their platforms provide a visual interface for making cross-platform if statements to its users, which, as of 2020, numbered 18 million people.

IFTTT stands for âIf This Then That.â Basically, if certain conditions are met, then something else will happen. The âif thisâ part is called a trigger, and the âthen thatâ part is called an action. It joins smart home devices, social media, delivery apps, and more so it can perform automated tasks.

2.1 Sign up IFTTTď
Type âhttps://ifttt.comâ in your browser and click on the âGet startedâ button located at the center of the page. Fill out the form with your information to create an account.

Click âBackâ to exit quickstart, return to the IFTTT homepage, refresh the page and log in again.

2.2 Creating the Appletď
Click âCreateâ to start creating the Applet.

If This trigger
Click âAddâ next to âIf Thisâ to add a trigger.

Search for âwebhookâ and click on âWebhooksâ.

Click on âReceive a web requestâ on the page shown in the following image.

Set the âEvent Nameâ to âvibration_detectedâ.

Then That action
Click on âAddâ next to âThen Thatâ to add a action.

Search for âemailâ and click on âEmailâ.

Click on âSend me a emailâ on the page shown in the following image.

Set the subject and content of the email to be sent when vibration is detected.
As a reference, the subject is set to â[ESP-01] Detected vibration!!!â, and the content is set to âDetected vibration, please confirm the situation promptly! {{OccurredAt}}â. When sending an email, {{OccurredAt}}
will be automatically replaced with the time when the event occurred.

According to the following steps, complete the creation of the Applet.



3. Run the Codeď
Open the
04-Vibration_alert_system.ino
file under the path ofultimate-sensor-kit\iot_project\wifi\04-Vibration_alert_system
, or copy this code into Arduino IDE.You need to enter the
mySSID
andmyPWD
of the WiFi you are using.String mySSID = "your_ssid"; // WiFi SSID String myPWD = "your_password"; // WiFi Password
You also need to modify the
URL
with both the event name you set and your API key.String URL = "/trigger/vibration_detected/with/key/xxxxxxxxxxxxxxxxxx";
Here you can find your unique API KEY that you must keep private. Type in the event name as
vibration_detected
. Your final URL will appear at the bottom of the webpage. Copy this URL.After selecting the correct board and port, click the Upload button.
Open the Serial monitor(set baudrate to 9600) and wait for a prompt such as a successful connection to appear.
4. Code explanationď
The ESP8266 module that comes with the kit is already pre-burned with AT firmware. Therefore, the ESP8266 module can be controlled through AT commands. In this project, we use software serial to enable communication between the Arduino Uno board and the ESP8266 module. The Arduino Uno board sends AT commands to the ESP8266 module for network connection and sending requests. You can refer to ESP8266 AT Instruction Set.
The Uno board reads sensor values and sends AT commands to the ESP8266 module. The ESP8266 module connects to a network and sends requests to IFTTT servers.
Include SoftwareSerial library for serial communication between Arduino and ESP8266
#include <SoftwareSerial.h> SoftwareSerial espSerial(2, 3);
Configure WiFi credentials and IFTTT server details
String mySSID = "your_ssid"; String myPWD = "your_password"; String myHOST = "maker.ifttt.com"; String myPORT = "80"; String URL = "/trigger/xxx/with/key/xxxx";
Define variables for the vibration sensor and alert frequency control
unsigned long lastAlertTime = 0; const unsigned long postingInterval = 120000L; const int sensorPin = 7;
In
setup()
, initialize serial communication, ESP8266 module and connect to WiFivoid setup() { Serial.begin(9600); espSerial.begin(115200); // Initialize the ESP8266 module sendATCommand("AT+RST", 1000, DEBUG); //Reset the ESP8266 module sendATCommand("AT+CWMODE=1", 1000, DEBUG); //Set the ESP mode as station mode sendATCommand("AT+CWJAP=\"" + mySSID + "\",\"" + myPWD + "\"", 3000, DEBUG); //Connect to WiFi network while (!espSerial.find("OK")) { //Wait for connection } }
In
loop()
, detect vibration and send alert if time interval has passedvoid loop() { if (digitalRead(sensorPin)) { if (lastAlertTime == 0 || millis() - lastAlertTime > postingInterval) { Serial.println("Detected vibration!!!"); sendAlert(); //Send an HTTP request to IFTTT server } else { Serial.print("Detected vibration!!! "); Serial.println("Since an email has been sent recently, no warning email will be sent this time to avoid bombarding your inbox."); } } else { if (DEBUG) { Serial.println("Detecting..."); } } delay(500); }
sendAlert() constructs HTTP request and sends it via ESP8266
void sendAlert() { String sendData = "GET " + URL + " HTTP/1.1" + "\r\n"; sendData += "Host: maker.ifttt.com\r\n"; sendATCommand("AT+CIPMUX=0",1000,DEBUG); sendATCommand("AT+CIPSTART=...",3000,DEBUG); sendATCommand("AT+CIPSEND=" + String(sendData.length()),1000,DEBUG); espSerial.println(sendData); }
Handling AT Commands sendATCommand()
This function sends AT commands to the ESP8266 and collects responses.
void sendATCommand(String command, const int timeout, boolean debug) { // Print and send command Serial.print("AT Command ==> "); Serial.print(command); Serial.println(); espSerial.println(command); // Send the AT command // Get the response from the ESP8266 module String response = ""; long int time = millis(); while ((time + timeout) > millis()) { // Wait for the response until the timeout while (espSerial.available()) { char c = espSerial.read(); response += c; } } // Print response if debug mode is on if (debug) { Serial.println(response); Serial.println("--------------------------------------"); }
Reference