244 lines
6.3 KiB
C++
244 lines
6.3 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <SPI.h>
|
|
#include <MFRC522.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <ArduinoJson.h>AllTopic:All
|
|
#include "icon.h"
|
|
#include <UniversalTelegramBot.h> // tambahkan include untuk library Telegram
|
|
#include <ArduinoJson.h>
|
|
|
|
// Inisialisasi OLED
|
|
#define SCREEN_WIDTH 128
|
|
#define SCREEN_HEIGHT 64
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
|
|
|
|
#define RST_PIN 16 // pin-D0
|
|
#define SS_PIN 0 // pin-D3
|
|
byte buzzer = 15; // pin D8
|
|
MFRC522 mfrc522(SS_PIN, RST_PIN); // Buat instance MFRC522
|
|
|
|
const char *ssid = "Iphone"; // SSID WiFi Anda
|
|
const char *password = "44444444"; // Kata sandi WiFi Anda
|
|
String server_addr = "192.168.107.197"; // Alamat server atau IP komputer Anda
|
|
|
|
byte readCard[4];
|
|
uint8_t successRead;
|
|
String UIDCard;
|
|
|
|
#define BOT_TOKEN "6508339619:AAGeFNlU5M7uwpiImHHi88M6HGCxjVrob58" // Ganti dengan token bot Anda
|
|
WiFiClient client;
|
|
UniversalTelegramBot bot(BOT_TOKEN, client);
|
|
|
|
void setup() {
|
|
pinMode(buzzer, OUTPUT);
|
|
Serial.begin(9600); // Inisialisasi komunikasi serial dengan PC
|
|
SPI.begin(); // Inisialisasi bus SPI
|
|
mfrc522.PCD_Init(); // Inisialisasi kartu MFRC522
|
|
Serial.println(F("Read Uid data on a MIFARE PICC:"));
|
|
ShowReaderDetails(); // Tampilkan detail pembaca kartu MFRC522
|
|
|
|
// Inisialisasi koneksi WiFi dan OLED
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
|
|
Serial.println(F("SSD1306 allocation failed"));
|
|
for (;;)
|
|
;
|
|
}
|
|
display.clearDisplay();
|
|
display.setTextColor(WHITE);
|
|
display.setTextSize(1);
|
|
display.setCursor(38, 20);
|
|
display.println(F("ARDUCODING"));
|
|
display.setCursor(35, 35);
|
|
display.println(F("Mesin Absen"));
|
|
display.display();
|
|
delay(1000);
|
|
ConnectWIFI();
|
|
delay(2000);
|
|
}
|
|
|
|
void loop() {
|
|
display.clearDisplay();
|
|
display.drawBitmap(32, 0, cardBitmap, 68, 50, WHITE);
|
|
display.setTextSize(1);
|
|
display.setCursor(30, 55);
|
|
display.print("Tap Your Card!");
|
|
display.display();
|
|
successRead = getID();
|
|
}
|
|
|
|
uint8_t getID() {
|
|
if (!mfrc522.PICC_IsNewCardPresent()) {
|
|
return 0;
|
|
}
|
|
if (!mfrc522.PICC_ReadCardSerial()) {
|
|
return 0;
|
|
}
|
|
UIDCard = "";
|
|
Serial.println(F("Scanned PICC's UID:"));
|
|
|
|
for (uint8_t i = 0; i < mfrc522.uid.size; i++) {
|
|
UIDCard += String(mfrc522.uid.uidByte[i], HEX);
|
|
}
|
|
UIDCard.toUpperCase();
|
|
Serial.print("UID:");
|
|
Serial.println(UIDCard);
|
|
Serial.println(F("**End Reading**"));
|
|
digitalWrite(buzzer, HIGH);
|
|
delay(200);
|
|
digitalWrite(buzzer, LOW);
|
|
delay(200);
|
|
digitalWrite(buzzer, HIGH);
|
|
delay(200);
|
|
digitalWrite(buzzer, LOW);
|
|
|
|
storeData();
|
|
delay(2000);
|
|
|
|
mfrc522.PICC_HaltA();
|
|
return 1;
|
|
}
|
|
|
|
void storeData() {
|
|
ConnectWIFI();
|
|
WiFiClient client;
|
|
String address, massage, first_name;
|
|
|
|
address = "http://" + server_addr + "/absensi/webapi/api/create.php?uid=" + UIDCard;
|
|
|
|
HTTPClient http;
|
|
http.begin(client, address);
|
|
int httpCode = http.GET();
|
|
String payload;
|
|
Serial.print("Response: ");
|
|
if (httpCode > 0) {
|
|
payload = http.getString();
|
|
payload.trim();
|
|
if (payload.length() > 0) {
|
|
Serial.println(payload + "\n");
|
|
}
|
|
}
|
|
http.end();
|
|
|
|
const size_t capacity = JSON_OBJECT_SIZE(4) + 70;
|
|
DynamicJsonDocument doc(capacity);
|
|
|
|
DeserializationError error = deserializeJson(doc, payload);
|
|
|
|
if (error) {
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
Serial.println(error.c_str());
|
|
return;
|
|
}
|
|
|
|
const char *waktu_res = doc["waktu"];
|
|
String nama_res = doc["nama"];
|
|
const char *uid_res = doc["uid"];
|
|
String status_res = doc["status"];
|
|
|
|
for (int i = 0; i < nama_res.length(); i++) {
|
|
if (nama_res.charAt(i) == ' ') {
|
|
first_name = nama_res.substring(0, i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Kirim notifikasi Telegram
|
|
sendTelegramNotification(status_res, first_name, waktu_res, uid_res);
|
|
|
|
display.clearDisplay();
|
|
display.drawBitmap(0, 5, userBitmap, 50, 60, WHITE);
|
|
display.setTextColor(WHITE);
|
|
display.setTextSize(1);
|
|
|
|
if (status_res == "INVALID") {
|
|
massage = "Who are you?";
|
|
display.setCursor(52, 15);
|
|
display.print(massage);
|
|
display.setCursor(52, 30);
|
|
display.print(uid_res);
|
|
display.setCursor(52, 40);
|
|
display.print(status_res);
|
|
} else {
|
|
if (status_res == "IN") {
|
|
massage = "Welcome!";
|
|
} else {
|
|
massage = "See you!";
|
|
}
|
|
display.setCursor(52, 15);
|
|
display.print(massage);
|
|
display.setCursor(52, 30);
|
|
display.print(first_name);
|
|
display.setCursor(52, 40);
|
|
display.print(waktu_res);
|
|
}
|
|
display.display();
|
|
delay(3000);
|
|
}
|
|
|
|
void sendTelegramNotification(String status, String first_name, const char *waktu, const char *uid) {
|
|
String message = "Kartu dengan UID " + String(uid) + " telah " + (status == "IN" ? "masuk" : "keluar") + ".";
|
|
bot.sendMessage("1263710714", message); // Ganti "chat_id" dengan ID obrolan Anda
|
|
}
|
|
|
|
void ConnectWIFI() {
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print("Attempting to connect to SSID: ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
int i = 0;
|
|
int a = 0;
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print(".");
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
if (a == 0) {
|
|
display.drawBitmap(52, 20, wifi_icon, 16, 16, WHITE);
|
|
a = 1;
|
|
} else {
|
|
display.drawBitmap(52, 20, wifi_icon, 16, 16, BLACK);
|
|
a = 0;
|
|
}
|
|
display.setCursor(25, 50);
|
|
display.print("Connecting ...");
|
|
display.display();
|
|
delay(1000);
|
|
++i;
|
|
if (i == 30) {
|
|
i = 0;
|
|
Serial.println("\n Failed to Connect.");
|
|
break;
|
|
}
|
|
}
|
|
Serial.println("\n Connected!");
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.drawBitmap(52, 20, wifi_icon, 16, 16, WHITE);
|
|
display.setCursor(33, 50);
|
|
display.print("Connected!");
|
|
display.display();
|
|
delay(2000);
|
|
}
|
|
}
|
|
|
|
void ShowReaderDetails() {
|
|
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
|
Serial.print(F("MFRC522 Software Version: 0x"));
|
|
Serial.print(v, HEX);
|
|
if (v == 0x91)
|
|
Serial.print(F(" = v1.0"));
|
|
else if (v == 0x92)
|
|
Serial.print(F(" = v2.0"));
|
|
else
|
|
Serial.print(F(" (unknown),probably a chinese clone?"));
|
|
Serial.println("");
|
|
|
|
if ((v == 0x00) || (v == 0xFF)) {
|
|
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
|
Serial.println(F("SYSTEM HALTED: Check connections."));
|
|
while (true)
|
|
;
|
|
}
|
|
}
|