Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by takai ( 9 years ago )
/* GR-PEACH Sketch Template V2.01 */
#include <Arduino>
#include "mbed.h"
#include "TCPSocket.h"
#include "ESP8266Interface.h"
#define WIFI_SSID "0_402SH8415276" //wifi
#define WIFI_PASSWORD "iida310ru" //wifi pasword
#define IFTTT_EVENT "peach"
#define IFTTT_KEY "bgSuQ4zNXIOLLKzUiXzx2r"
bool ifttt_get(const char* event, const char* key);
bool ifttt_post(const char* event, const char* key, char* value1);
void led_blink(int pin, int num);
ESP8266Interface* wifi_handle;
void setup(){
static ESP8266Interface wifi(P5_0, P5_1);
Serial.begin(9600);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(PIN_LED_BLUE, OUTPUT);
Serial.println("IFTTT example with ESP8266");
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
int error = wifi.connect(WIFI_SSID, WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (error != false) {
Serial.println("Connection error");
digitalWrite(PIN_LED_RED, HIGH);
while(1);
}
Serial.println("Success");
led_blink(PIN_LED_GREEN, 3);
Serial.print("MAC");
Serial.println(wifi.get_mac_address());
Serial.print("IP");
Serial.println(wifi.get_ip_address());
wifi_handle = &wifi;
}
void loop(){
const char event[] = IFTTT_EVENT;
const char key[] = IFTTT_KEY;
char c[6] = "PEACH";
if(ifttt_post(event, key, c)) {
Serial.println("Done to get.");
led_blink(PIN_LED_BLUE, 3);
}
while(1);
}
bool ifttt_get(const char* event, const char* key){
TCPSocket socket;
socket.open(wifi_handle);
socket.connect("maker.ifttt.com", 80);
String send_buf("GET /trigger/");
send_buf += event;
send_buf += "/with/key/";
send_buf += key;
send_buf += " ";
send_buf += "HTTP/1.1\r\n";
send_buf += "Host: maker.ifttt.com\r\n";
send_buf += "Connection: close\r\n\r\n";
Serial.println(send_buf.c_str());
int scount = socket.send(send_buf.c_str(), send_buf.length());
if(scount < 0) return false;
Serial.print("[Send Count]: ");
Serial.println(scount);
// Recieve a simple http response and print out the response line
char receive_buffer[100];
int rcount = socket.recv(receive_buffer, sizeof(receive_buffer));
if(rcount < 0) return false;
for(int i = 0; i < rcount; i++){
Serial.print(receive_buffer[i]);
}
Serial.println();
Serial.print("[Receive Count]: ");
Serial.println(rcount);
// Close the socket to return its memory and bring down the network interface
socket.close();
return true;
}
bool ifttt_post(const char* event, const char* key, char* value1){
TCPSocket socket;
socket.open(wifi_handle);
socket.connect("maker.ifttt.com", 80);
String header, contents;
contents = "{\"value1\" : \"";
contents += value1;
contents += "\"}\r\n";
contents += "\r\n";
header = "POST /trigger/";
header += event;
header += "/with/key/";
header += key;
header += " ";
header += "HTTP/1.1\r\n";
header += "Host: maker.ifttt.com\r\n";
header += "Content-Type: application/json\r\n";
header += "Connection: close\r\n";
header += "Content-Length:";
header += contents.length();
header += "\r\n";
header += "\r\n";
header += contents;
Serial.println(header.c_str());
int scount = socket.send(header.c_str(), header.length());
if(scount < 0) return false;
Serial.print("[Send Count]: ");
Serial.println(scount);
// Recieve a simple http response and print out the response line
char receive_buffer[100];
int rcount = socket.recv(receive_buffer, sizeof(receive_buffer));
if(rcount < 0) return false;
for(int i = 0; i < rcount; i++){
Serial.print(receive_buffer[i]);
}
Serial.println();
Serial.print("[Receive Count]: ");
Serial.println(rcount);
// Close the socket to return its memory and bring down the network interface
socket.close();
return true;
}
void led_blink(int pin, int num){
pinMode(pin, OUTPUT);
for(int i = 0; i < num; i++){
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
delay(200);
}
}
Revise this Paste