adrianF
Published

Internet Button // TI LaunchPad & IFTTT

Bend the internet to your will with the @TXInstruments LaunchPad-powered @IFTTT-enabled button!

BeginnerFull instructions provided4,321
Internet Button // TI LaunchPad & IFTTT

Things used in this project

Story

Read more

Code

Energia Code Example

C/C++
This is the main sketch for making the appropriate RESTful API call to the IFTTT Maker Channel. The TI LaunchPad attaches an interrupt to one of the on-board push buttons. When the button is pressed, the LaunchPad does an HTTP Post to the IFTTT cloud to trigger an event. This event then triggers a push notification through the Boxcar 2 channel in IFTTT.
/*
  Repeating Wifi Web Client

 This sketch connects to a a web server and makes a request

 Circuit:
 * WiFi shield attached to pins SPI pins and pin 7

 created 23 April 2012
 modified 31 May 2012
 by Tom Igoe
 modified 13 Jan 2014
 by Federico Vanzati
 modified 6 July 2014
 by Noah Luskey
 modified 12 Oct 2015
 by Adrian Fernandez (IFTTT trigger)

 This code is in the public domain.

 Circuit:
 * CC3200 WiFi LaunchPad or CC3100 WiFi BoosterPack
   with TM4C or MSP430 LaunchPad
 */

#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>

// your network name also called SSID
char ssid[] = "YOUR_SSID";
// your network password
char password[] = "YOUR_PASSWORD";

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(50,62,217,1);  // numeric IP for Google (no DNS)
char server[] = "maker.ifttt.com";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

int trigger = 0;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);

  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
  
  
  /* Enable internal pullup. 
   * Without the pin will float and the example will not work */
  pinMode(PUSH2, INPUT_PULLUP);
  attachInterrupt(PUSH2, sendRequest, FALLING); // Interrupt is fired whenever button is pressed
  
  Serial.println("Push the button!");
}

void loop() {
  String IFTTT_KEY = "YOUR_IFTTT_KEY";
  String IFTTT_EVENT = "button_pressed"; // IFTTT Maker Event Name here
  if(trigger == 1){
    ifttt_trigger(IFTTT_KEY, IFTTT_EVENT); 
    Serial.println("Push the button!");
    trigger = 0;
  }

}

//******************************************************************************************
// This method makes a HTTP connection to the server & does a GET request for user name
// Simply pass in the badge ID to query as a String.
// Returns string:
//   - Returns "<NAME>"if badge ID is registered
//   - Returns "NULL" if badge ID is not registered
//   - Returns "FAIL" if REST API failed
//******************************************************************************************
String ifttt_trigger(String KEY, String EVENT) {
  String name = "";
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    
    // This is optional. You can send additional data to IFTTT along with your HTTP POST that triggers the action
    String PostData = "{\"value1\" : \"testValue\", \"value2\" : \"Hello\", \"value3\" : \"World!\" }";
    
    Serial.println("connected to server... Getting name...");
    // send the HTTP PUT request:
    String request = "POST /trigger/";
    request += EVENT;
    request += "/with/key/";
    request += KEY;
    request += " HTTP/1.1";
    
    Serial.println(request);    
    client.println(request);
    client.println("Host: maker.ifttt.com");
    client.println("User-Agent: Energia/1.1");
    client.println("Connection: close");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    client.println();

  }
  else {
    // if you couldn't make a connection:
    Serial.println("Connection failed");
    return "FAIL"; // rest API failed...
  }
  
  // Capture response from the server. (10 second timeout)
  long timeOut = 4000;
  long lastTime = millis();
  
  while((millis()-lastTime) < timeOut){  // Wait for incoming response from server
    while (client.available()) {         // Characters incoming from the server
      char c = client.read();            // Read characters
      Serial.write(c);
    }
    
  }
  Serial.println();
  Serial.println("Request Complete!");
  //return name; // Return the complete name received from server
  return "SUCCESS";
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void sendRequest(){
  if(trigger == 0){
    trigger = 1;
  }
}

Credits

adrianF

adrianF

2 projects • 21 followers

Comments