A sample program that trigger on IFTTT Maker channel with ESP8266

Dependencies:   ESP8266Interface mbed

Fork of ESP8266_Test by ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ESP8266Interface.h"
00003 #include "TCPSocketConnection.h"
00004 
00005 // You need to change these 4 lines.
00006 #define SSID "YOUR SSID"
00007 #define WIFIKEY "YOUR WIFI KEY"
00008 const char EVENT[] = "YOUR IFTTT EVENT NAME";
00009 const char KEY[] = "PUT YOUT IFTTT SECRET KEY HERE";
00010 //
00011 
00012 const char* ECHO_SERVER_ADDRESS = "54.243.120.63"; // maker.ifttt.com
00013 const int ECHO_SERVER_PORT = 80;
00014 
00015 ESP8266Interface wifi(p28,p27,p29,SSID,WIFIKEY,115200); // TX,RX,Reset,SSID,Password
00016 RawSerial pc(USBTX, USBRX); // tx, rx
00017 AnalogIn pot1 (p19);
00018 AnalogIn pot2 (p20);
00019  
00020 int main() {
00021     pc.baud(115200);
00022    
00023     int http_cmd_sz=800;
00024     char http_cmd[http_cmd_sz];
00025     
00026     wifi.init(); //Use DHCP
00027     wifi.connect();
00028     pc.printf("IP Address is %s\n", wifi.getIPAddress());
00029     
00030     TCPSocketConnection socket;
00031     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
00032         pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00033         wait(1);
00034     }
00035     
00036     char Val1[6];
00037     char Val2[6];
00038     char Val3[6];
00039     snprintf(Val1,5,"%.2f", (float)pot1);
00040     snprintf(Val2,5,"%.2f", (float)pot2);
00041     
00042     snprintf(http_cmd, http_cmd_sz, "GET /trigger/%s/with/key/%s/?value1=%s&value2=%s&value3=%s HTTP/1.1\r\nHost: maker.ifttt.com\r\nConnection: close\r\n\r\n", EVENT, KEY, Val1, Val2, Val3);
00043     pc.printf(http_cmd);
00044     socket.send_all(http_cmd, sizeof(http_cmd) - 1);
00045     
00046     char buf[512];
00047     int n = socket.receive(buf, 512);
00048     buf[n] = '\0';
00049     pc.printf("%s\r\n", buf);
00050     
00051     socket.close();
00052     wifi.disconnect();
00053     
00054     while(true) {}
00055 }
00056