IFTTT Keypad

The IFTTT Keypad is a project to allow triggering of an IFTTT recipe using a capacitive touch sensor with the Mbed Board. /media/uploads/rpickren3/ifttt-banner.jpg

  • IFTTT is a free web-based service where users can create simple conditional statements to control IoT gadgets, monitor web services, or automate apps. IFTTT is an abbreviation of "If This Then That." This project acts as an IFTTT “trigger.” IFTTT
  • The Maker Channel is an IFTTT plug-in created by hackster.io to allow any internet accessible device to trigger IFTTT recipes. Maker Channel

Overview

In this project, a capacitive touch sensor is used in conjunction with LED’s and a low cost speaker to create a keypad-style input device. The Mbed records keystrokes and compares them to a hardcoded pincode. Once the pincode has been correctly entered, it uses a WiFi microcontroller to send an HTTP request to the Maker Channel endpoint. This endpoint Triggers the IFTTT recipe.

The IFTTT Keypad can be used to trigger any registered IFTTT recipe. Example recipes include disabling a motion detector or sending an SMS notification.

Parts

List of mbed components and peripherals used

Wiring

/media/uploads/rpickren3/keypad.png/media/uploads/rpickren3/huzzuh.png /media/uploads/rpickren3/new.jpg

Final Project

Photo

Proof-of-Concept to Turn off Lutron and Philips Hue Lights:

Code

Main.cpp

Main.cpp

#include "mbed.h"
#include <mpr121.h>
#include <string>
#include "wifi.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

I2C i2c(p9, p10);
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
PwmOut speaker(p21);

void setup_wifi() {
    reset=0; 
    pc.baud(9600); 
    wait(0.5);
    reset=1;
    timeout=2;
    getreply();
    esp.baud(9600);
    ESPconfig();
}

void make_noise(float frequency, float duration) {
    speaker.period(1.0/frequency);
    speaker =0.5; 
    wait(duration);
    speaker=0.0; 
}

char rcv_keystroke() {
    int key_code=0;
    while (key_code==0) {
        int i=0;
        int value=mpr121.read(0x00);
        value +=mpr121.read(0x01)<<8;
        i=0;
        for (i=0; i<12; i++) {
            if (((value>>i)&0x01)==1) key_code=i+1;
        }
        if (key_code>10) key_code = 0;
    }
    char aChar = '0' + (key_code-1);
    return(aChar);
}

int main() {
    led1 = 1;
    led2 = 1;
    led3 = 1;
    led4 = 1;
    setup_wifi();
    led1 = 0;
    led2 = 0;
    led3 = 0;
    led4 = 0;
    string password = "2323";
    string key_code = "----";
    int i = 0;
    while(1) {
        char keystroke = rcv_keystroke(); //blocking
        make_noise(1500,0.1);
        key_code[i] = keystroke;
        if (i==0) led1 = 1;
        if (i==1) led2 = 1;
        if (i==2) led3 = 1;
        if (i==3) led4 = 1;
        i = i + 1;
        if (i==4) {
            if (key_code == password){
                // right
                make_noise(700,0.1);
                make_noise(1000,0.3);
                trigger_ifttt();
            } else {
                // wrong
                make_noise(700,0.1);
                make_noise(300,0.3);
            }
            key_code = "----";
            led1 = 0;
            led2 = 0;
            led3 = 0;
            led4 = 0;
            i = 0;
        }
        wait(0.1);
    }
}

wifi.h

Note: "GET /trigger/foo/with/key/bar" on Line 31 must be changed to match your IFTTT Maker Endpoint.

wifi.h | Largely based off of ESP8266-configuration-mbed-LPC1768

#include "mbed.h"


Serial pc(USBTX, USBRX);
Serial esp(p28, p27); // tx, rx
DigitalOut reset(p26);
Timer t;
 
int  count,ended,timeout;
char buf[2024];
char snd[1024];
 
char ssid[32] = "foo";     // enter WiFi router ssid inside the quotes
char pwd [32] = "bar"; // enter WiFi router password inside the quotes
 
void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();

void trigger_ifttt(){
    strcpy(snd,"conn = net.createConnection(net.TCP, false)\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
    
    strcpy(snd,"conn:on(\"receive\", function(conn, answer) print(answer) end)\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
    
    strcpy(snd,"conn:on(\"connection\", function(cn, answer) cn:send(\"GET /trigger/foo/with/key/bar HTTP/1.1\\r\\n\" .. \"Host: maker.ifttt.com\\r\\n\" .. \"Connection: keep-alive\\r\\n\\r\\n\") end)\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
    
    
    strcpy(snd,"conn:connect(80,\"maker.ifttt.com\")\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
}


 void dev_recv()
{
   // led1 = !led1;
    while(esp.readable()) {
        pc.putc(esp.getc());
    }
}
 
void pc_recv()
{
   // led4 = !led4;
    while(pc.readable()) {
        esp.putc(pc.getc());
    }
}
 
 
// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
void ESPsetbaudrate()
{
    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
    SendCMD();
}
 
//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
void ESPconfig()
{

    wait(1);
    //pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
    strcpy(snd,".\r\n.\r\n");
    SendCMD();
    wait(1);
    //pc.printf("---------- Reset & get Firmware ----------\r\n");
    strcpy(snd,"node.restart()\r\n");
    SendCMD();
    timeout=5;
    getreply();
    //pc.printf(buf);
    wait(1);
    
    strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
    SendCMD();
    timeout=4;
    getreply();
    //pc.printf(buf);
 
    wait(1);
  
    //pc.printf("\n---------- Connecting to AP ----------\r\n");
    //pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
    strcpy(snd, "wifi.sta.config(\"");
    strcat(snd, ssid);
    strcat(snd, "\",\"");
    strcat(snd, pwd);
    strcat(snd, "\")\r\n");
    SendCMD();
    timeout=10;
    getreply();
    //pc.printf(buf); 
}
 
void SendCMD()
{
    esp.printf("%s", snd);
}
 
void getreply()
{
    memset(buf, '\0', sizeof(buf));
    t.start();
    ended=0;
    count=0;
    while(!ended) {
        if(esp.readable()) {
            buf[count] = esp.getc();
            count++;
        }
        if(t.read() > timeout) {
            ended = 1;
            t.stop();
            t.reset();
        }
    }
}


Please log in to post comments.