Created
December 6, 2016 07:29
-
-
Save ya-ma-cho/54a3459f76dac2406c6f82dcfe1d0c33 to your computer and use it in GitHub Desktop.
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
#include <Nefry.h> | |
#define DELAY_TIME1 3400 | |
#define DELAY_TIME2 460 | |
#define DELAY_TIME3 570 | |
#define DELAY_TIME4 510 | |
#define PIN_MAGNET D2 | |
#define PIN_BUZZER D4 | |
int temp; | |
int state; | |
String Event, SecretKey; | |
bool ngSend(String,String,String data=""); | |
void setup() { | |
pinMode(PIN_MAGNET, INPUT); | |
pinMode(PIN_BUZZER, OUTPUT); | |
Nefry.setConfHtml("SecretKey",0); | |
Nefry.setConfHtml("Event",1); | |
SecretKey = Nefry.getConfStr(0); | |
Event = Nefry.getConfStr(1); | |
temp = digitalRead(PIN_MAGNET); | |
} | |
void loop() { | |
state = digitalRead(PIN_MAGNET); | |
if (state == HIGH) { | |
if(state != temp){ | |
Nefry.setLed(0, 0, 255); //LED:青色 | |
okAlert(); | |
okAlert(); | |
temp = state; | |
} | |
} else if (state == LOW) { | |
if(state != temp){ | |
Nefry.setLed(255, 0, 0); //LED:赤色 | |
if (!ngSend(Event, SecretKey)) { | |
Nefry.setLed(0, 255, 0);//Errの時、緑色点灯 | |
Nefry.ndelay(5000); | |
} | |
temp = state; | |
} | |
ngAlert(); | |
} | |
} | |
bool ngSend(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; | |
} | |
} | |
void ngAlert() { | |
int i; | |
for (i=0; i<70; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME1); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME1); | |
} | |
for (i=0; i<5; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME2); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME2); | |
} | |
delay(250); | |
for (i=0; i<70; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME1); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME1); | |
} | |
delay(250); | |
for (i=0; i<5; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME2); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME2); | |
} | |
delay(400); | |
} | |
void okAlert() { | |
int i; | |
for (i=0; i<180; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME3); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME3); | |
} | |
delay(20); | |
for (i=0; i<180; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME3); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME3); | |
} | |
delay(180); | |
for (i=0; i<45; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME3); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME3); | |
} | |
delay(180); | |
for (i=0; i<45; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME3); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME3); | |
} | |
delay(90); | |
for (i=0; i<180; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME4); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME4); | |
} | |
delay(20); | |
for (i=0; i<180; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME4); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME4); | |
} | |
delay(180); | |
for (i=0; i<45; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME4); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME4); | |
} | |
delay(90); | |
for (i=0; i<45; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME4); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME4); | |
} | |
delay(90); | |
for (i=0; i<45; i++) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(DELAY_TIME4); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(DELAY_TIME4); | |
} | |
delay(90); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment