Senin, 10 Maret 2025

arduino rfid

 #include <Wire.h> 
#include <WiFiClientSecure.h>  
#include <WiFi.h>
#include <HTTPClient.h>  

byte sda = 5;
byte rst = 15;
#include <KRrfid.h>
#define RED_LED 26
#define GREEN_LED 27
#define buzzer 4
#define YELLOW_LED 12  //cadangan,,...
const int button1Pin = 32;
int st_ok = 0, count = 0;

//=============================================
#include <NTPClient.h>
#include <WiFiUdp.h>
// WiFi Credentials
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);  // LCD 20x4

//const char* ssid = "-";
//const char* password = "-";
const char* ssid = "-";
const char* password = "12345678";

const char* serverURL = "https://lp2maray.com/sentWA.php?psn=";
const char* serverName ="https://script.google.com/macros/s/AKfycbx4-EwSjyguE2p0LFiuF9zjvcw0JOVW4ejqq8W8weSM6Q4d4DRLykRWxWAwJv_gTe8AStT/exec";
String sts = "Masuk"; 
String tanggal="";
String jam="";
String tgl="";
String RFID="RFID01"; 
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7 * 3600, 60000); // Zona waktu GMT+7 (Indonesia)

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  rfidBegin();

  // Inisialisasi LED
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(RED_LED, OUTPUT);
  pinMode(YELLOW_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(buzzer, OUTPUT); 
  // Sambungkan ke WiFi
  lcd.setCursor(0, 0);
  lcd.print("Connecting...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting to WiFi...");
   timeClient.begin();
  } 

  lcd.clear();
  lcd.print("WiFi Connected");
  delay(2000);
  lcd.clear();
}
 
//################################################
void loop() { 
      int jamNow=waktu();
      getTAG();
        if (digitalRead(button1Pin) == HIGH) {
              sts = "MASUK  "; 
              if(jamNow>12){sts= " IZIN  "; }
smasuk();
          }
        else if (digitalRead(button1Pin) == LOW) {
              sts = "PULANG "; 
              spulang();
          }
       
        String pesan1="ABSEN ="+sts         ;
        String pesan2="TAP KARTU  !!!        ";
        lihat0(pesan1,pesan2);   
      
        if (TAG != "") { 
          Serial.print("TAG :");
          Serial.println(TAG);
          String input = TAG;
          input.trim();
          CEK(input);
        }
      else if (Serial.available() > 0) {
          String input = Serial.readStringUntil('\n');   
          input.trim();   
          CEK(input);
        } 
      delay(500);
}
//################################################
void CEK(String input){
  if (input == "RFID01" || input == "395593") {sendWA("2401001","A","OPS","083842690xxx"); }
  else if (input == "RFID02" || input == "2250103") {sendWA("2401002","D","Marketing","08896699xx"); }
  else if (input == "RFID03" || input == "2765740") {sendWA("2401002","D","Marketing","082125745xxx"); }
  else if (input == "RFID04" || input == "7511215528") {sendWA("2401002","D","Marketing","083842690xxx"); }
  //dst
    
  else {buzdeny();}  // Jika TAG tidak valid
  TAG = "";
  input="";
}
void sendWA(String kode,String nama,String divisi,String hp){
          buzacc();
          skirim();
          String pesan1="ABSENSI:"+sts+"       ";
          nama.toUpperCase();
          sts.trim();
          String pesan2="Yth "+nama            ;
          
          lihat(pesan1,pesan2); 
          String pesan="Yth "+nama+"/"+kode+" ("+divisi+"), Absen "+sts+" Anda Sukses disimpan...";
          sendCondition(pesan,hp);
          sendXLS(kode, nama, divisi,hp); 
  }
//==============================================================
void sendXLS(String kode,String nama,String divisi,String hp){
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverName);
    http.addHeader("Content-Type", "application/json");

    String jsonData = "{\"tanggal\":\""+tanggal+"\", \"jam\":\""+jam+"\", \"kode\":\""+kode+"\", \"nama\":\""+nama+"\", \"divisi\":\""+divisi+"\", \"hp\":\""+hp+"\", \"status\":\""+sts+"\"}";   
    Serial.println(jsonData);
    int httpResponseCode = http.POST(jsonData); 
    if (httpResponseCode > 0) {
      if (httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY || httpResponseCode == HTTP_CODE_FOUND) {
        String newLocation = http.getLocation();
        Serial.println("Redirected to: " + newLocation);
        http.end(); // End the current connection
        http.begin(newLocation);
        http.addHeader("Content-Type", "application/json");
        httpResponseCode = http.POST(jsonData);
        if (httpResponseCode > 0) {
          String response = http.getString();
          Serial.println(httpResponseCode);
          Serial.println(response);
        } else {
          Serial.println("Error on sending POST after redirect: " + String(httpResponseCode));
        }
      } else {
        String response = http.getString();
        Serial.println(httpResponseCode);
        Serial.println(response);
      }
    } else {
      Serial.println("Error on sending POST: " + String(httpResponseCode));
    }
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
  delay(5000); // process 5 seconds
}

//==============================================================
void sendCondition(String pesan, String hp) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http; 
    String fullURL = serverURL  + urlencode(pesan) + "&ph=" + urlencode(hp);
    Serial.println("Request URL: " + fullURL);
    http.begin(fullURL); 
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response Code: ");
      Serial.println(httpResponseCode);
      String response = http.getString();
      Serial.println("Response: " + response);
    } else {
      Serial.print("Error sending GET: ");
      Serial.println(http.errorToString(httpResponseCode).c_str());
    }
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
  delay(3000);
}

String urlencode(String str) {
  String encodedString = "";
  char c;
  char code0;
  char code1;
  char code2;
  for (int i = 0; i < str.length(); i++) {
    c = str.charAt(i);
    if (c == ' ') {
      encodedString += '+';
    } else if (isalnum(c)) {
      encodedString += c;
    } else {
      code1 = (c & 0xf) + '0';
      if ((c & 0xf) > 9) {
        code1 = (c & 0xf) - 10 + 'A';
      }
      c = (c >> 4) & 0xf;
      code0 = c + '0';
      if (c > 9) {
        code0 = c - 10 + 'A';
      }
      code2 = '\0';
      encodedString += '%';
      encodedString += code0;
      encodedString += code1;
    }
  }
  return encodedString;
}
 
void buzacc() {
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  delay(100);
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  delay(100);
}
void buzdeny() {
  digitalWrite(buzzer, HIGH);
  delay(1000);
  digitalWrite(buzzer, LOW);
}

void lihat(String pesan1, String pesan2){
  lcd.setCursor(0, 0);
  lcd.print("ABSENSI              ");
  lcd.setCursor(0, 1);
  lcd.print(tanggal + " | " + jam);  // Tanggal dan waktu
  lcd.setCursor(0, 2);
  lcd.print(pesan1);   // Pesan pertama, misalnya "ABSEN = Masuk"
  lcd.setCursor(0, 3);
  lcd.print(pesan2);   // Pesan kedua, misalnya "Yth [Nama]"
  delay(5000); // Display for 5 seconds
}

void lihat0(String pesan1, String pesan2){
  lcd.setCursor(0, 0);
  lcd.print("NINJA EXPRES        ");   // Tampilkan nama aplikasi
  lcd.setCursor(0, 1);
  lcd.print(tanggal + " | " + jam);  // Tanggal dan waktu
  lcd.setCursor(0, 2);
  lcd.print("SILAHKAN TAB KARTU");    // Instruksi untuk men-tap kartu
  lcd.setCursor(0, 3);
  lcd.print("Status: " + sts);        // Status absen (Masuk / Pulang)
  delay(500); // Display for 500ms
}

void smasuk(){
   digitalWrite(RED_LED, LOW);
   digitalWrite(YELLOW_LED, LOW);
   digitalWrite(GREEN_LED, HIGH);  
}
void spulang(){
   digitalWrite(RED_LED, HIGH);
   digitalWrite(YELLOW_LED, LOW);
   digitalWrite(GREEN_LED, LOW);  
}

void skirim(){
  Serial.println("Tanggal: " + tanggal+" ("+tgl+")");
  Serial.println("Jam: " + jam);
   digitalWrite(RED_LED, LOW);
   digitalWrite(YELLOW_LED, HIGH);
   digitalWrite(GREEN_LED, LOW);  
}

int waktu(){
  timeClient.update(); // Update waktu dari server NTP

  time_t rawTime = timeClient.getEpochTime(); // Waktu epoch
  struct tm* timeInfo = localtime(&rawTime);
  int year = timeInfo->tm_year + 1900;
  int month = timeInfo->tm_mon + 1;
  int day = timeInfo->tm_mday;
  int hour = timeInfo->tm_hour;
  int minute = timeInfo->tm_min;
  int second = timeInfo->tm_sec;

  char bufferTGL[5]; // Format: DD/MM
  char bufferTanggal[11]; // Format: YYYY-MM-DD
  char bufferJam[9];      // Format: HH:MM:SS

  sprintf(bufferTanggal, "%04d-%02d-%02d", year, month, day);
  sprintf(bufferJam, "%02d:%02d:%02d", hour, minute, second);
  sprintf(bufferTGL, "%02d/%02d", day, month);

  tanggal = String(bufferTanggal);
  jam = String(hour) + ":" + String(minute) + ":" + String(second);
  tgl = String(bufferTGL);

  String pesan1 = " " + tgl + " : " + jam + " ";
  String pesan2 = "ABSEN = " + sts;
  lihat0(pesan1, pesan2);  // Update tampilan dengan tanggal, jam, dan status absen
return hour;
}