Advertisement
andretafta

RFID Logging Website

Jan 31st, 2021 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*PIN SDA/SS : D2
  2.  *PIN SCK : D5
  3.  *PIN MOSI: D7
  4.  *PIN MISO: D6
  5.  *PIN GND : GND
  6.  *PIN RST : D1
  7.  *PIN 3.3V: 3V
  8.  *KAKI PANJANG LED HIJAU : D0
  9.  *KAKI PANJANG LED MERAH : D4
  10.  *SEMUA KAKI PENDEK LED : GND
  11.  */
  12.  
  13.  
  14. #include <ESP8266WiFi.h>
  15. #include <ESP8266HTTPClient.h>
  16. #include <ArduinoJson.h>
  17. #include <SPI.h>
  18. #include <MFRC522.h>  
  19.  
  20. #define SS_PIN D2
  21. #define RST_PIN D1
  22. #define Merah D4
  23. #define Hijau D0
  24.  
  25. #define WLAN_SSID "sesuaikan"  //Nama Wi-Fi
  26. #define WLAN_PASS "sesuaikan" //Password  
  27.  
  28. MFRC522 rfid(SS_PIN, RST_PIN);
  29. MFRC522::MIFARE_Key key;
  30.  
  31. String nama = "sesuaikan"; //Diisi sesuai nama masing
  32. String strID;
  33.  
  34. void setup() {
  35.   Serial.begin(115200);
  36.   pinMode(Merah, OUTPUT);
  37.   pinMode(Hijau, OUTPUT);
  38.   WiFi.begin(WLAN_SSID, WLAN_PASS);
  39.  
  40.   while (WiFi.status() != WL_CONNECTED) {
  41.     delay(1000);
  42.     Serial.println("Menghubungkan");
  43.   }
  44.  
  45.   if(WiFi.status() == WL_CONNECTED){
  46.     Serial.println("Terhubung");
  47.     }
  48.   else{
  49.     Serial.println("Gagal Terhubung");
  50.   }
  51.   SPI.begin(); // Init SPI bus
  52.   rfid.PCD_Init(); // Init MFRC522
  53. }
  54.  
  55. void loop() {
  56.   digitalWrite(Merah, LOW);
  57.   digitalWrite(Hijau, LOW);
  58.   if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
  59.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  60.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  61.       piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  62.       piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  63.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  64.     return;
  65.   }
  66.   strID = "";
  67.   for (byte i = 0; i < 4; i++) {
  68.     strID +=
  69.       (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
  70.       String(rfid.uid.uidByte[i], HEX) +
  71.       (i != 3 ? ":" : "");
  72.   }
  73.   strID.toUpperCase();
  74.   Serial.print("ID dari RFID Tag : ");
  75.   Serial.println(strID);
  76.   if (WiFi.status() == WL_CONNECTED) {
  77.     HTTPClient http;
  78.     http.begin("http://logrfid.sintara.co.id/data.php?rfid_tag="+String(strID)+"&nama="+String(nama)); //Alamat File Data.php Disimpan
  79.     int httpCode = http.GET();
  80.     if (httpCode > 0) {
  81.       char json[500];
  82.       String payload = http.getString();
  83.       payload.toCharArray(json, 500);
  84.  
  85.       //StaticJsonDocument<200> doc;
  86.       DynamicJsonDocument doc(JSON_OBJECT_SIZE(5));
  87.  
  88.      // Deserialize the JSON document
  89.       deserializeJson(doc, json);
  90.  
  91.       String Pesan  = doc["Pesan"];
  92.       if( Pesan == "1" ){
  93.         Serial.println("Pesan : Kartu belum diaktifkan");
  94.         digitalWrite(Merah, HIGH);
  95.         delay(3000);
  96.       } else if ( Pesan == "2" ){
  97.         Serial.println("Pesan : Pengguna Tidak Aktif");
  98.         digitalWrite(Merah, HIGH);
  99.         delay(3000);
  100.       } else if ( Pesan == "3" ){
  101.         Serial.println("Pesan : Pengguna Aktif");
  102.         digitalWrite(Hijau, HIGH);
  103.         delay(3000);
  104.       } else {
  105.         Serial.println("Pesan : Pengguna Baru ditambahkan");
  106.         digitalWrite(Hijau, HIGH);
  107.         delay(3000);
  108.       }
  109.   }
  110.     http.end();
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement