Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <Nefry.h>
#define PIN_INSIDE D2
#define PIN_OUTSIDE D4
int state_inside;
int state_outside;
int state_inside_temp;
int state_outside_temp;
String Event, SecretKey;
bool insideSend(String,String,String data="");
bool outsideSend(String,String,String data="");
void setup() {
pinMode(PIN_INSIDE, INPUT);
pinMode(PIN_OUTSIDE, INPUT);
Nefry.setConfHtml("SecretKey",0);
Nefry.setConfHtml("Event",1);
SecretKey = Nefry.getConfStr(0);
Event = Nefry.getConfStr(1);
state_inside_temp = digitalRead(PIN_INSIDE);
state_outside_temp = digitalRead(PIN_OUTSIDE);
}
void loop() {
state_inside = digitalRead(PIN_INSIDE);
state_outside = digitalRead(PIN_OUTSIDE);
if (state_inside_temp == LOW && state_inside == HIGH) {
Nefry.setLed(0, 0, 255); //LED:青色
if (!iftttSend(Event, SecretKey ,"{\"value1\":\"回収したよ?\"}")) {
Nefry.setLed(0, 255, 0);//Errの時、緑色点灯
Nefry.ndelay(5000);
}
}
if (state_outside_temp == LOW && state_outside == HIGH) {
Nefry.setLed(255, 0, 0); //LED:赤色
if (!iftttSend(Event, SecretKey ,"{\"value1\":\"投函されたよ?\"}")) {
Nefry.setLed(0, 255, 0);//Errの時、緑色点灯
Nefry.ndelay(5000);
}
}
state_inside_temp = state_inside;
state_outside_temp = state_outside;
}
bool iftttSend(String event, String Secretkey, String data) {
WiFiClient client;
if (client.connect("maker.ifttt.com", 80)) {
Nefry.println("connection");
client.println("POST /trigger/" + event + "/with/key/" + Secretkey + " HTTP/1.1");
client.println("Host: maker.ifttt.com");
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
delay(10);
Nefry.println("OK");
return true;
} else {
Nefry.println("Err");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment