Sabtu, 04 Januari 2025

RUNEVERY

byte sda = D8;
byte rst = D0;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

#include<KRrfid.h>
#include <Servo.h>
Servo servo;
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
WiFiClientSecure secured_client;
const char* ssid = "4G";
const char* password = "98765432";
const char* server = "192.168.1.1/simpan.php?";

String idrf = "", payload;
int st_ok = 0, count = 0;

int buka = 0, tutup = 180;


void setup() {
  Serial.begin(9600);
  LCD.init();
  LCD.backlight();
  LCD.setCursor(0, 0);
  LCD.print("Connecting to ");
  LCD.setCursor(0, 1);
  LCD.print("WiFi ");
  servo.attach(D4);
  servo.write (tutup);
  rfidBegin();
  Serial.print("Menghubungkan ke Wi-Fi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi terhubung.");
  LCD.setCursor(0, 0);
  LCD.print("Selamat Datang   ");
  LCD.setCursor(0, 1);
  LCD.print("Tap Kartu        ");
}

void loop() {
  getTAG();
  if (TAG != "") {
    Serial.print("TAG :");
    Serial.println(TAG);
    idrf = TAG;
    LCD.setCursor(0, 0);
    LCD.print("Send data       ");
    LCD.setCursor(0, 1);
    LCD.print("                ");
    //    SendData();
    if (idrf == "1817713031") {
      st_ok = 1;
      servo.write (buka);
      LCD.setCursor(0, 0);
      LCD.print("ACC             ");
      LCD.setCursor(0, 1);
      LCD.print("Pintu terbuka!!!");
      count = 0;
    }
    TAG = "";
  }


  if (runEvery2(1000) && st_ok == 1) {
    count++;
    if (count >= 6) {
      LCD.setCursor(0, 0);
      LCD.print("Selamat Datang   ");
      LCD.setCursor(0, 1);
      LCD.print("Tap Kartu        ");
      count = 0;
      idrf = "";
      st_ok = 0;
    }
    else if (count >= 4) {
      LCD.setCursor(0, 0);
      LCD.print("                 ");
      LCD.setCursor(0, 1);
      LCD.print("Pintu tertutup   ");
      servo.write (tutup);
    }
    Serial.println(count);
  }
}

void SendData() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;
    String url = String(server) + "id=" + String(idrf);
    Serial.println( url);
    http.begin(client, url);
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      payload = http.getString();
      payload.trim();
      Serial.println(payload);

      if (payload == "1") {
        LCD.setCursor(0, 0);
        LCD.print("ACC             ");
        LCD.setCursor(0, 1);
        LCD.print("Pintu terbuka   ");
        servo.write (buka);
        st_ok = 1;
        count = 0;
      } else {
        LCD.setCursor(0, 0);
        LCD.print("Gagal           ");
        LCD.setCursor(0, 1);
        LCD.print("                ");
        delay(1000);
      }
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
}

boolean runEvery2(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;