From 9e5f0788cbde4c7293ac567123ac55ada08df006 Mon Sep 17 00:00:00 2001 From: PWojtaszek <180088@stud.prz.edu.pl> Date: Wed, 14 Jan 2026 10:17:54 +0100 Subject: [PATCH] Add main.cpp --- main.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9079ee5 --- /dev/null +++ b/main.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include // Biblioteka do pamięci nieulotnej + +// --- KONFIGURACJA --- +const char* ssid = "TWOJA_NAZWA_WIFI"; +const char* password = "TWOJE_HASLO_WIFI"; +const String serverUrl = "http://twoja-domena.pl/check_access.php"; +const String apiKey = "MojeTajneHaslo123"; + +#define RST_PIN 22 +#define SS_PIN 5 +#define RELAY_PIN 2 +#define CACHE_SIZE 10 // Liczba zapamiętanych kart + +MFRC522 mfrc522(SS_PIN, RST_PIN); +Preferences preferences; + +// Funkcja sprawdzająca, czy UID jest w lokalnej pamięci +bool checkLocalCache(String uid) { + preferences.begin("access-cache", true); // Tryb tylko do odczytu + for (int i = 0; i < CACHE_SIZE; i++) { + String key = "uid" + String(i); + if (preferences.getString(key.c_str(), "") == uid) { + preferences.end(); + return true; + } + } + preferences.end(); + return false; + } + + // Funkcja dodająca UID do pamięci (zastępuje najstarszy wpis - Round Robin) + void addToCache(String uid) { + preferences.begin("access-cache", false); + int nextIndex = preferences.getInt("nextIdx", 0); + + String key = "uid" + String(nextIndex); + preferences.putString(key.c_str(), uid); + + // Inkrementacja indeksu (0-9) + nextIndex = (nextIndex + 1) % CACHE_SIZE; + preferences.putInt("nextIdx", nextIndex); + + preferences.end(); + Serial.println("Dodano kartę do lokalnej pamięci cache."); + } + + void openGate() { + Serial.println(">>> DOSTĘP PRZYZNANY - OTWIERAM BRAMĘ <<<"); + digitalWrite(RELAY_PIN, LOW); + delay(2000); + digitalWrite(RELAY_PIN, HIGH); + } + + void setup() { + Serial.begin(115200); + SPI.begin(); + mfrc522.PCD_Init(); + pinMode(RELAY_PIN, OUTPUT); + digitalWrite(RELAY_PIN, HIGH); + + WiFi.begin(ssid, password); + Serial.print("Łączenie z WiFi"); + } + + void loop() { + // 1. Wykrywanie karty + if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) return; + + String uid = ""; + for (byte i = 0; i < mfrc522.uid.size; i++) { + uid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); + uid += String(mfrc522.uid.uidByte[i], HEX); + } + uid.toUpperCase(); + Serial.println("\nZczytano UID: " + uid); + + // 2. Próba weryfikacji ONLINE + if (WiFi.status() == WL_CONNECTED) { + HTTPClient http; + String url = serverUrl + "?uid=" + uid + "&key=" + apiKey; + http.begin(url); + int httpCode = http.GET(); + + if (httpCode > 0) { + String payload = http.getString(); + if (payload == "ALLOW") { + addToCache(uid); // Aktualizacja cache przy każdym sukcesie online + openGate(); + } else { + Serial.println("Dostęp zabroniony (Serwer)"); + } + } else { + // Błąd połączenia z serwerem, przejdź do trybu OFFLINE + Serial.println("Błąd serwera. Próba autoryzacji OFFLINE..."); + if (checkLocalCache(uid)) openGate(); + else Serial.println("Brak karty w pamięci lokalnej."); + } + http.end(); + } + // 3. Brak WiFi - Tryb OFFLINE + else { + Serial.println("Brak WiFi! Próba autoryzacji OFFLINE..."); + if (checkLocalCache(uid)) openGate(); + else Serial.println("Odmowa: Karta nieznana w trybie offline."); + } + + mfrc522.PICC_HaltA(); + delay(1500); + } + \ No newline at end of file