#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Pins für das Display
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Pins für das NFC/RFID-Modul
#define NFC_CS 21
#define NFC_RST 22
MFRC522 nfc(NFC_CS, NFC_RST);
// Wokwi Standard WLAN-Zugangsdaten
const char* ssid = "Wokwi-GUEST";
const char* password = "";
bool eingestempelt = false;
void setup() {
Serial.begin(115200);
SPI.begin();
// 1. Display starten
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 50);
tft.print("Verbinde WLAN...");
// 2. WLAN starten
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWLAN verbunden!");
// 3. NFC-Leser starten
nfc.PCD_Init();
zeigeBereit();
}
void loop() {
// HIER WAR DER FEHLER: PICC statt PCD nutzen!
// Prüfen, ob eine NFC-Karte aufgelegt wird
if (!nfc.PICC_IsNewCardPresent() || !nfc.PICC_ReadCardSerial()) {
return;
}
// ID der Karte auslesen und als Text (String) speichern
String cardID = "";
for (byte i = 0; i < nfc.uid.size; i++) {
cardID += String(nfc.uid.uidByte[i] < 0x10 ? "0" : "");
cardID += String(nfc.uid.uidByte[i], HEX);
}
cardID.toUpperCase();
// Status wechseln
eingestempelt = !eingestempelt;
// TEXT AN DEN SERVER SENDEN
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://httpbin.org" + cardID + "&status=" + (eingestempelt ? "IN" : "OUT");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 120);
tft.setTextSize(2);
tft.print("Sende Daten...");
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Server-Antwort Code: " + String(httpCode));
}
http.end();
}
// DISPLAY UMSCHALTEN
if (eingestempelt) {
tft.fillScreen(ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.setCursor(40, 60);
tft.print("HALLO!");
tft.setTextSize(2);
tft.setCursor(20, 130);
tft.print("ID: " + cardID);
tft.setCursor(60, 170);
tft.setTextSize(4);
tft.print("08:00");
} else {
tft.fillScreen(ILI9341_BLUE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(30, 60);
tft.print("TSCHUESS!");
tft.setTextSize(2);
tft.setCursor(15, 130);
tft.print("Arbeitszeit heute:");
tft.setCursor(50, 170);
tft.setTextSize(4);
tft.print("08:15h");
}
nfc.PICC_HaltA(); // Karte stoppen
delay(4000);
zeigeBereit();
}
void zeigeBereit() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(45, 100);
tft.print("Bitte NFC");
tft.setCursor(30, 130);
tft.print("auflegen...");
tft.drawCircle(120, 160, 110, ILI9341_DARKGREY);
}