Categories
TUTORIALS

HOW TO MAKE A PANIC BUTTON USING ARDUINO AND ifttt

Overview

Nowadays people rely on cell phones, so emergency landlines in public spaces are hard to find. For situations when a connection is needed, it can be a tough decision which communication technology to deploy. The popular GSM (Global Systems for Mobile Communication) boards are becoming harder to integrate as 2G and 3G are becoming obsolete. LoRa (Long Range) promises distance, but its small packets are more appropriate for small bits of data. What is a DIY lover to do? An Arduino with WiFi capability and the free IFTTT (If This Then That) service might just be the heroes you need. In this tutorial we’ll use a typical Arduino panic button setup, but use IFTTT to connect to the internet and send a notification, email, SMS ,VOIP call or phone call.

Parts

For this tutorial it is assumed that you have your Arduino with WiFi already checked and working. If you want to follow along with this build, here’s what you’ll need:

  • 1- Arduino MKR1000 (or another Arduino with WiFi capability)
  • 1-LED
  • 1-button
  • 1-10K resistor
  • 1-220 ohm resistor
  • 5-hookup wires
  • 1-breadboard

Circuit

  • 5V –> + rail
  • Ground –> – rail
  • Pin 7 –> Button (10K resistor between Pin 7 and -rail)
  • Button –> +rail
  • LED –> -rail
  • LED –> 220 ohm resistor
  • 220 ohm resistor –> Pin 5

IFTTT (If This Then That) App

IFTTT is a service for IoT (Internet of Things) devices. Think of it as a matchmaker of apps and devices that can connect through WiFi. What if Amazon Alexa could talk to your Hue lights? Want to blink your Hue lights when the International Space Station flies by? Want a weekly email of koala GIFS from Giphy? What about having your Fitbit send your activity to a Google spreadsheet? There are many practical (and fun) shortcuts you can perform with this app. Let’s get things started!

  1. Go to the app store to download IFTTT for your cell or on the web.
  2. Take a quick browse through the applets that are already available.
  3. If you are on the IOS app click “Get More” on the bottom of the screen.
  4. Select the “+” sign at the top of the screen where it says “Make your own Applets from…”. Now follow along in this slideshow as we set up “This” by touching the “+” sign. Make sure to read the details carefully and finish by pressing “Create trigger”.

Now it’s time to follow the screen show to set up the “That”, so begin by selecting the “+” sign again. This time you will be choosing to create a notification, so IFTTT may ask you for information about your phone. Make sure to hit “finish” on the last screen. Notice that when the applet is complete it will show a toggle that says “connected”. You can always choose to turn this off when you don’t want it to run.

Congrats—you’ve completed making your first applet! It will allow the button to send a notification to your phone. For the “That” part of the applet, you can also choose other forms of communication like email, SMS, VOIP phone or regular phone (even a landline can work).

Now we just need to get a key from IFTTT to use for our code, so follow this slideshow. On the app press “Get More” and then press “Get More” again. At the end you have to click on “Documentation” and the next screen will have the key (it will look like a jumble of letters). Make sure to take a screenshot or write it down.

Arduino Code

There are three important lines in the code which will need your information between the ” ” symbols.

  • SSID – your WiFi name
  • password – your password for your WiFi
  • resource – (IMPORTANT) this is the link needed to use your applet, so it must exactly match your trigger event name and key. You probably used button_pushed, but if not, you need to change it.
/* This program creates a panic button using an Arduino MKR1000 and IFTTT service. When button is pushed LED flashes and a notification, SMS, VOIP call or phonecall is initiated. It uses code from Robert 7320 for the LED and button www.instructables.com/id/5-Simple-Button-and-Led-Projects-with-Arduino/
Additional code from Rui Santos for his version of an ESP8266 panic button www.randomnerdtutorials.com/esp8266-wi-fi-button-diy-amazon-dash-button-clone/  
*/
#include <WiFi101.h>
#include <WiFiUdp.h>
#include <SPI.h>

const char* ssid = "xxxxxxxx";   // Your network SSID (name)
const char* password = "xxxxxxxxx";  // Your network password (use for WPA, or use as key for WEP)
// Unique IFTTT URL resource
const char* resource = "/trigger/button_pressed/with/key/YourKeyHere";   //Important- your key goes here
int keyIndex = 0;   // your network key Index number (needed only for WEP)
int LED = 5;  //declaring pin for LED
int BUTTON = 7;  //declaring pin for button
// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

void setup(){
Serial.begin(115200);  //set rate of serial monitor
initWifi();
//makeIFTTTRequest();

pinMode(LED,OUTPUT);  //set LED pin as output
pinMode(BUTTON,INPUT);  //set button pin as input
}
// this loop looks for a button push and if happens 
//blinks LED and runs IFTTT request loop (otherwise LED off)
void loop(){
if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
digitalWrite(LED,HIGH);
delay (1000);
digitalWrite(LED,LOW);
delay (1000);
makeIFTTTRequest();

}else{

digitalWrite(LED,LOW);

}
}
// this loop helps for debugging Wifi connection 
//and initiates Wifi

void initWifi() {
  Serial.print("Connecting to: "); 
  Serial.print(ssid);
  WiFi.begin(ssid, password);  

  int timeout = 10 * 4; // 10 seconds
  while(WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if(WiFi.status() != WL_CONNECTED) {
     Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: "); 
  Serial.print(millis());
  Serial.print(", IP address: "); 
  Serial.println(WiFi.localIP());
}

// this loops makes an HTTP request to the IFTTT web service
// and also allows the serial monitor to help with debugging
void makeIFTTTRequest() {
  Serial.print("Connecting to "); 
  Serial.print(server);
  
  WiFiClient client;
  int retries = 5;
  while(!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if(!!!client.connected()) {
     Serial.println("Failed to connect, going back to sleep");
  }
  
  Serial.print("Request resource: "); 
  Serial.println(resource);
  client.print(String("GET ") + resource + 
                  " HTTP/1.1\r\n" +
                  "Host: " + server + "\r\n" + 
                  "Connection: close\r\n\r\n");
                  
  int timeout = 5 * 10; // 5 seconds             
  while(!!!client.available() && (timeout-- > 0)){
    delay(100);
  }
  if(!!!client.available()) {
     Serial.println("No response, going back to sleep");
  }
  while(client.available()){
    Serial.write(client.read());
  }
  
  Serial.println("\nclosing connection");
  client.stop();

}

Final Notes

As you can see, VOIP can be an interesting choice for a panic button. IFTTT will allow you to do several options from the same code as long as you create multiple applets like VOIP, email, phone etc. This project uses the Arduino MKR1000 but many people also use Arduinos with ESP8266. Be aware that the 8266 uses a different library, so code will vary. However, the use of IFTT in the pertinent lines of code will be similar, and you can see that in the example from Santos referenced at the top of my code. Have fun experimenting with the many ways IFTTT can link your maker projects!

2 replies on “HOW TO MAKE A PANIC BUTTON USING ARDUINO AND ifttt”

Thank for the great tutorial! Setting up ifttt was easy but getting the Arduino to talk to ifttt was not, until I figured out the cert issue.

Just an FYI that you may want to add, maker.ifttt.com will redirect you to the HTTPS port and fail if you do not have the certificate loaded on the board. You can do that through the WiFi101 firmware/certificate updater in the Arduino IDE. At the bottom just add the URL maker.ifttt.com:443 and update cert. You will need the correct sketch uploaded but google can help with that or look here https://www.arduino.cc/en/Tutorial/FirmwareUpdater

[…] QUICK TIP: Panic ButtonWith the IFTTT app, you can create a panic button that you can use in case of emergencies. You can set it up to turn on the siren, set-off the alarm, call for help, or any action that can help you keep safe in case of a break-in. Panic buttons can also serve for medical safety. The panic button can be purchased online, or you can DIY. […]

Leave a Reply

Your email address will not be published. Required fields are marked *