#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName = "https://internal.system.watan-food-chain.com/api/log-attendance";
const int buttonPin = 25;
int lastButtonState = HIGH;
// --- إعدادات إضاءة RGB ---
const int ledRed = 13;
const int ledGreen = 12;
const int ledBlue = 14;
// دالة للتحكم بألوان الـ RGB بسهولة
void setLEDColor(int r, int g, int b) {
digitalWrite(ledRed, r);
digitalWrite(ledGreen, g);
digitalWrite(ledBlue, b);
}
// -------------------------
void updateScreen(String title, String message) {
u8g2.clearBuffer();
// العنوان الإنجليزي
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(0, 15);
u8g2.print(title);
// الرسالة العربية
u8g2.setFont(u8g2_font_cu12_t_arabic);
int width = u8g2.getUTF8Width(message.c_str());
int x = 128 - width;
if (x < 0) x = 0;
u8g2.drawUTF8(x, 45, message.c_str());
u8g2.sendBuffer();
}
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
// تهيئة منافذ الإضاءة
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
// إطفاء اللمبة في البداية
setLEDColor(LOW, LOW, LOW);
if(!u8g2.begin()) {
Serial.println("OLED Failed");
for(;;);
}
u8g2.enableUTF8Print();
updateScreen("Watan Food", "...ﻞﻴﻐﺸﺘﻟﺍ ﻱﺭﺎﺟ"); // جاري التشغيل...
delay(2000);
WiFi.begin(ssid, password);
updateScreen("WiFi","...ﻝﺎﺼﺗﻻﺍ ﻱﺭﺎﺟ"); // جاري الاتصال...
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(2000);
// تفعيل اللون الأزرق (جاهز / بانتظار البصمة)
setLEDColor(LOW, LOW, HIGH);
updateScreen("READY", "ﻚﺘﻤﺼﺑ ﻊﺿ"); // ضع بصمتك
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
// تفعيل اللون الأصفر (جاري المعالجة: أحمر + أخضر)
setLEDColor(HIGH, HIGH, LOW);
updateScreen("Processing...", "..ﺺﺤﻔﻟﺍ ﻱﺭﺎﺟ"); // جاري الفحص..
sendAttendanceRecord(102, "check_in");
delay(5000);
}
lastButtonState = buttonState;
}
void sendAttendanceRecord(int employeeId, String status) {
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
if (http.begin(client, serverName)) {
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
http.addHeader("X-API-KEY", "Wa#tchO#ut_Fo#r12Th#_eSec#re32t_3K435e_4yI546#n45The_EnvF#il567567e_!ChangeT#hisT68_678oAStron#_gRan#do6mStrin4g");
String jsonPayload = "{\"fingerprint_id\":" + String(employeeId) + "}";
int httpResponseCode = http.POST(jsonPayload);
if(httpResponseCode == 200) {
// تفعيل اللون الأخضر (نجاح التسجيل)
setLEDColor(LOW, HIGH, LOW);
String responseBody = http.getString();
StaticJsonDocument<300> doc;
DeserializationError error = deserializeJson(doc, responseBody);
if (!error) {
String apiMessage = doc["message"].as<String>();
updateScreen("SUCCESS", apiMessage);
delay(1500);
updateScreen("SUCCESS", "ﺭﻮﻀﺤﻟﺍ ﻞﻴﺠﺴﺗ ﻢﺗ");
} else {
updateScreen("SUCCESS", "ﺭﻮﻀﺤﻟﺍ ﻞﻴﺠﺴﺗ ﻢﺗ"); // تم تسجيل الحضور
}
} else {
// تفعيل اللون الأحمر (فشل / خطأ سيرفر)
setLEDColor(HIGH, LOW, LOW);
updateScreen("SERVER ERROR", "Code: " + String(httpResponseCode));
}
http.end();
}
delay(3000);
// العودة للون الأزرق (جاهز لبصمة جديدة)
setLEDColor(LOW, LOW, HIGH);
updateScreen("READY", "ﻚﺘﻤﺼﺑ ﻊﺿ"); // ضع بصمتك
}
}