//=============================================================================
// RFID-IFTTT
//=============================================================================
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include "MFRC522.h"
/* Wiring RFID RC522 module
=============================================================================
GND = GND 3.3V = 3.3V
The following table shows the typical pin layout used:
Signal MFRC522 WeMos D1 mini NodeMcu Generic
RST/Reset RST D3 [1] D3 [1] GPIO-0 [1]
SPI SS SDA [3] D8 [2] D8 [2] GPIO-15 [2]
SPI MOSI MOSI D7 D7 GPIO-13
SPI MISO MISO D6 D6 GPIO-12
SPI SCK SCK D5 D5 GPIO-14
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
=============================================================================
*/
#define RST_PIN D3 // RST-PIN für RC522 - RFID - SPI - D3
#define SS_PIN D8 // SDA-PIN für RC522 - RFID - SPI - D8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//=============================================================================
// WiFi接入點設置
const char *WIFI_SSID = "PTS-2F";
const char *WIFI_PASSWORD = "PTS6662594";
//const char *WIFI_SSID = "74170287";
//const char *WIFI_PASSWORD = "24063173";
//const char *WIFI_SSID = "yourSSID"; // change according to your Network - cannot be longer than 32 characters!
//const char *WIFI_PASSWORD = "yourPASSWORD"; // change according to your Network
// ITFFF的設定
const char* host = "maker.ifttt.com"; //Ifttt主機
const char* eventName = "RFID_LINE"; //Ifttt觸發條件名稱---依照自我設定的eventName
const char* key = "d72d5B9gxxgtitLL4aK6gF"; //Ifttt授權碼
//======================================================
//Variables
long lastMsg = 0;
String IDNo_buf="";
int value=0;
//======================================================
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
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: ");
Serial.println(WiFi.localIP());
}
//=============================================================================
String printHex(byte *buffer, byte bufferSize) {
String id = "";
for (byte i = 0; i < bufferSize; i++) {
id += buffer[i] < 0x10 ? "0" : "";
id += String(buffer[i], HEX);
}
return id;
}
//=============================================================================
void setup() {
Serial.begin(115200);
// 連接到WiFi
setup_wifi();
Serial.println(F("Booting...."));
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
Serial.println(F("Ready!"));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card and print UID:"));
}
//=============================================================================
void loop() {
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
long now = millis();
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { // 如果出現新卡片就讀取卡片資料
delay(100);
String IDNo = printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
// Show some details of the PICC (that is: the tag/card)
if ((IDNo != IDNo_buf) || (now - lastMsg > 5000)) { //不同卡片 或是 等5秒
lastMsg = now;
Serial.print(F("Card UID:"));
Serial.println(IDNo);
//Serial.println(IDNo_buf);
value=1 ; //set flag for read Card
IDNo_buf="";
IDNo_buf=IDNo;
} // ((IDNo != IDNo_buf) || (now - lastMsg > 5000))
} // if (mfrc522.PICC_IsNewCardPresent()
if (value != 0) {
// IFTTT RFID Data
//RequestToIFTTT ("?value1=" + IDNo) ;
String url = "/trigger/"; //組合觸發網址
url += eventName;
url += "/with/key/";
url += key;
url += "?value1=";
url += String(IDNo_buf);
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
while (!client.available()) {
delay(10);
}
Serial.println(client.readStringUntil('\r'));
client.flush();
client.stop();
// 進入 DeepSleep
Serial.println("Go to sleep...");
//ESP.deepSleep(DEEP_SLEEP_INTERVAL, WAKE_RF_DEFAULT);
delay(5*1000);
Serial.println("Waked up , ready to reading RFID card...");
value = 0;
} // if (value != 0)
}
//=============================================================================
沒有留言:
張貼留言