#include "SPI.h"
#include "MFRC522.h"
#include <Arduino.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
ESP8266WiFiMulti WiFiMulti;
#define SS_PIN 4
#define RST_PIN 5
#define SP_PIN 8
int led3 = D3;
int led4 = D4;
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFiMulti.addAP("***ชื่อไวไฟ***", "***รหัสผ่าน***"); // ssid , password
randomSeed(50);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
String strID = "";
for (byte i = 0; i < 4; i++) {
strID +=
(rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
String(rfid.uid.uidByte[i], HEX) +
(i!=3 ? ":" : "");
}
strID.toUpperCase();
Serial.println(strID);
digitalWrite(led3, HIGH);
HTTPClient http;
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(led4, HIGH);
String url = "http://hinsorn.ac.th/deparoom/show_arduino_in.php?nmbr="+String(strID)+"&idroom=***&MM_insert=frmMain";
Serial.println(url);
http.begin(url); //HTTP
int httpCode = http.GET();
delay(1000);
digitalWrite(led4, LOW);
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
digitalWrite(led4, LOW);
digitalWrite(led4, HIGH);
}
http.end();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
|