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.

../_images/04-Wiring_Vibration_alert_system.png

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.

../_images/04-ifttt_intro.png

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.

https://images.contentful.com/mrsnpomeucef/78eXVQ2gXnWkCNCykr7Mdt/8b188790f1fda2ed84ab17afe06cf145/img-welcome-02_x2.jpg

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.

../_images/04-ifttt_signup.png

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

../_images/04-ifttt_signup_2.png

2.2 Creating the Applet

Click “Create” to start creating the Applet.

../_images/04-ifttt_create_applet_1_shadow.png

If This trigger

Click “Add” next to “If This” to add a trigger.

../_images/04-ifttt_create_applet_2_shadow.png

Search for “webhook” and click on “Webhooks”.

../_images/04-ifttt_create_applet_3_shadow.png

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

../_images/04-ifttt_create_applet_4_shadow.png

Set the “Event Name” to “vibration_detected”.

../_images/04-ifttt_create_applet_5_shadow.png

Then That action

Click on “Add” next to “Then That” to add a action.

../_images/04-ifttt_create_applet_6_shadow.png

Search for “email” and click on “Email”.

../_images/04-ifttt_create_applet_7_shadow.png

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

../_images/04-ifttt_create_applet_8_shadow.png

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.

../_images/04-ifttt_create_applet_9_shadow.png

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

../_images/04-ifttt_create_applet_10_shadow.png ../_images/04-ifttt_create_applet_11_shadow.png ../_images/04-ifttt_create_applet_12_shadow.png

3. Run the Code

  1. Open the 04-Vibration_alert_system.ino file under the path of ultimate-sensor-kit\iot_project\wifi\04-Vibration_alert_system, or copy this code into Arduino IDE.

  2. You need to enter the mySSID and myPWD of the WiFi you are using.

    String mySSID = "your_ssid";     // WiFi SSID
    String myPWD = "your_password";  // WiFi Password
    
  3. 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";
    
    ../_images/04-ifttt_apikey_1_shadow.png ../_images/04-ifttt_apikey_2_shadow.png

    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.

    ../_images/04-ifttt_apikey_3_shadow.png ../_images/04-ifttt_apikey_4_shadow.png
  4. After selecting the correct board and port, click the Upload button.

  5. Open the Serial monitor(set baudrate to 9600) and wait for a prompt such as a successful connection to appear.

    ../_images/04-ready_shadow.png

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.

  1. Include SoftwareSerial library for serial communication between Arduino and ESP8266

    #include <SoftwareSerial.h>
    SoftwareSerial espSerial(2, 3);
    
  2. 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";
    
  3. Define variables for the vibration sensor and alert frequency control

    unsigned long lastAlertTime = 0;
    const unsigned long postingInterval = 120000L;
    const int sensorPin = 7;
    
  4. In setup(), initialize serial communication, ESP8266 module and connect to WiFi

    void 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
      }
    }
    
  5. In loop(), detect vibration and send alert if time interval has passed

    void 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);
    }
    
  6. 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);
    
    }
    
  7. 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