// D0 = 16; D1 = 5; D2 = 4;
// D3 = 0; D4 = 2; D5 = 14;
// D6 = 12; D7 = 13; D8 = 15;
// D9 = 3; D10 = 1;
#include <ESP8266WiFi.h>
//=============RFID Reader===================
#include <Wiegand.h>
#define FALSE 0
#define TRUE 1
WIEGAND wg;
int value = 0;
// WiFiアクセスポイントの設定
const char *WIFI_SSID = "PTS-2F";
const char *WIFI_PASSWORD = "";
// ITFFFの設定
const char *IFTTT_HOST = "maker.ifttt.com";
const char *IFTTT_URI = "/trigger/rfid_data/with/key/zaHdzsOGgZ71vZT1Y1FA6";
//enevnt name rfid_data
//To trigger an Event
//Make a POST or GET web request to:
//https://maker.ifttt.com/trigger/{event}/with/key/zaHdzsOGgZ71vZT1Y1FA6
// DeepSleepのインターバル
//const unsigned long DEEP_SLEEP_INTERVAL = 60 * 60 * 1000 * 1000; // 1時間
void setup() {
Serial.begin(115200);
// WiFiに接続
Serial.print("connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address is: ");
Serial.println(WiFi.localIP());
//===========RFID Read==============
//GATE A
wg.D0PinA =4;
wg.D1PinA =5;
//GATE B
//wg.D0PinB =18;
//wg.D1PinB =19;
//GATE C
//wg.D0PinC =20;
//wg.D1PinC =21;
// Reader enable
wg.begin(TRUE, FALSE, FALSE); // wg.begin(GateA , GateB, GateC)
//===========RFID Read==============
}
void loop() {
if(wg.available()) {
Serial.print("Wiegand HEX = ");
Serial.print(wg.getCode(),HEX);
Serial.print(", DECIMAL = ");
Serial.print(wg.getCode());
value=wg.getCode();
Serial.print(", Gate= ");
Serial.print(wg.getGateActive());
Serial.print(",Type:Wiegand");
Serial.println(wg.getWiegandType());
} //if(wg.available())
if (value != 0) {
// IFTTT RFID Data
RequestToIFTTT ("?value1=" + String(wg.getCode()) + "&value2=" + String(wg.getWiegandType()));
// DeepSleepに入る
Serial.println("Go to sleep...");
//ESP.deepSleep(DEEP_SLEEP_INTERVAL, WAKE_RF_DEFAULT);
delay(20*1000);
Serial.println("Waked up , ready to reading RFID card...");
value = 0;
}
}
// IFTTTにリクエストを送る
void RequestToIFTTT(String param) {
WiFiClientSecure client;
while (!client.connect(IFTTT_HOST, 443)) {
delay(10);
}
client.print(String("GET ") + IFTTT_URI + param +
" HTTP/1.1\r\n" +
"Host: " + IFTTT_HOST + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println(String("GET ") + IFTTT_URI + param +
" HTTP/1.1\r\n" +
"Host: " + IFTTT_HOST + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
while (!client.available()) {
delay(10);
}
Serial.println(client.readStringUntil('\r'));
client.flush();
client.stop();
}
沒有留言:
張貼留言