Instantly share code, notes, and snippets.
Last active
October 16, 2015 13:49
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save pinski1/6f4b9ed7b402cbd3c391 to your computer and use it in GitHub Desktop.
An Amazon Dash-like button tied into IFTTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple IoT button using the If This Then That(IFTTT) Maker Channel, an | |
* ESP8266-01 and lit arcade button. | |
* | |
* For connections advice: | |
* http://iot-playground.com/2-uncategorised/38-esp8266-and-arduino-ide-blink-example | |
*/ | |
#include <ESP8266WiFi.h> | |
/** Network Definitions */ | |
const char* ssid = "yourssid"; | |
const char* password = "yourpassword"; | |
const int timeout = 5; | |
/** IFTTT Definitions */ | |
const char* ifttt_url = "maker.ifttt.com"; | |
const int ifttt_port = 80; | |
const char* ifttt_key = "YOUR IFTTT_KEY"; | |
const char* ifttt_event = "button"; | |
/** Pin Definitions */ | |
const int LED = 2; | |
const int button = 0; | |
/** Function Definitions */ | |
void shutdown(unsigned int error) { | |
// to cover shutting down | |
} | |
void setup(void) { | |
pinMode (button, OUTPUT); | |
digitalWrite(button, LOW); // hold on | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, HIGH); | |
Serial.begin(115200); | |
delay(10); | |
Serial.println("\r\n\r\nButton pressed!"); | |
Serial.print("Connecting to WiFi"); | |
WiFi.begin(ssid, password); | |
int i; | |
for(i = 0; i < timeout; i++) | |
{ | |
delay(500); | |
if(WiFi.status() != WL_CONNECTED) break; | |
else Serial.print('.'); | |
} | |
Serial.println(); | |
if(i == timeout) | |
{ | |
Serial.println("Connection to WiFi failed!"); | |
digitalWrite(button, HIGH); // release | |
while(1); | |
} | |
Serial.println("Triggering IFTTT event..."); | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(ifttt_url, ifttt_port)) { | |
Serial.println("Connection to IFTTT failed!"); | |
digitalWrite(button, HIGH); // release | |
while(1); | |
} | |
String url = "/trigger/" + String(ifttt_event) + "/with/key/" + String(ifttt_key); | |
// Set some values for the JSON data | |
IPAddress ip = WiFi.localIP(); | |
unsigned char mac[WL_MAC_ADDR_LENGTH]; | |
WiFi.macAddress(mac); | |
String value_1 = String(mac[0], HEX) + ':' + String(mac[1], HEX) + ':' + String(mac[2], HEX) + ':' + String(mac[3], HEX) + ':' + String(mac[4], HEX) + ':' + String(mac[5], HEX); | |
String value_2 = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); | |
String value_3 = String(WiFi.gatewayIP()); // drop? | |
// Build JSON data string | |
String data = ""; | |
data = data + "\n" + "{\"value1\":\""+ value_1 +"\",\"value2\":\""+ value_2 +"\",\"value3\":\""+ value_3 + "\"}" | |
// Sent HTTP POST Request with JSON data | |
client.println("POST "+ url +" HTTP/1.1"); | |
//Serial.println("POST "+ url +" HTTP/1.1"); | |
client.println("Host: "+ String(ifttt_url)); | |
//Serial.println("Host: "+ String(ifttt_url)); | |
client.println("User-Agent: Arduino/1.0"); | |
//Serial.println("User-Agent: Arduino/1.0"); | |
client.print("Accept: *"); | |
//Serial.print("Accept: *"); | |
client.print("/"); | |
//Serial.print("/"); | |
client.println("*"); | |
//Serial.println("*"); | |
client.print("Content-Length: "); | |
//Serial.print("Content-Length: "); | |
client.println(data.length()); | |
//Serial.println(data.length()); | |
client.println("Content-Type: application/json"); | |
//Serial.println("Content-Type: application/json"); | |
client.println("Connection: close"); | |
//Serial.println("Connection: close"); | |
client.println(); | |
//Serial.println(); | |
client.println(data); | |
//Serial.println(data); | |
// any feedback, success or failure? | |
// flash LED appropreately | |
WiFi.disconnect(); | |
// end | |
digitalWrite(button, HIGH); // release | |
while(1); | |
} | |
void loop(void) { | |
// should never get to this point | |
digitalWrite(button, HIGH); // release | |
while(1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment