From 07eaebe7747a85b0441bbd13a6bdf6b6d93b926c Mon Sep 17 00:00:00 2001 From: Eyuep Sueyruege Date: Tue, 21 Nov 2023 16:13:43 +0100 Subject: [PATCH] Update src/arduino/card_scanner.ino --- src/arduino/card_scanner.ino | 165 +++++++++++++++++++++++++++-------- 1 file changed, 129 insertions(+), 36 deletions(-) diff --git a/src/arduino/card_scanner.ino b/src/arduino/card_scanner.ino index b833a24..db4fced 100644 --- a/src/arduino/card_scanner.ino +++ b/src/arduino/card_scanner.ino @@ -1,53 +1,146 @@ -// imports +#include +#include +#include +#include #include #include +#define RST_PIN 22 +#define SS_PIN 21 -// Pin Definition -#define SS_PIN 10 -#define RST_PIN 9 +String lastCardName = ""; -// Instatiate Motor and RFID Module +MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance -MFRC522 rfid(SS_PIN, RST_PIN); +// Define an array of UUIDs +const char* uuids[] = { + "1391d43", + "3263784d", + "b1fcbd6", + "a24aee4b", + "b1a37b9", + "a21d9c4d", + "b1e8316", + "929b444d", + "f12a5c9", + "717f5347", + "c1b6af9" +}; -byte nuidPICC[4]; +// Corresponding names for each UUID +const char* names[] = { + "Card1", + "Card2", + "Card3", + "Card4", + "Card5", + "Card6", + "Card7", + "Card8", + "Card9", + "Card10", + "Card11" +}; -// Setup for first time -void setup() { - // Serial Begin - Serial.begin(9600); - // Init SPI bus - SPI.begin(); - // Init MFRC522 - rfid.PCD_Init(); +unsigned long lastReadTime = 0; +unsigned long readInterval = 1000; // Interval between card reading operations in milliseconds + +WiFiMulti wifiMulti; + +void setup() { + Serial.begin(115200); + SPI.begin(); // Initiate SPI bus + mfrc522.PCD_Init(); // Initiate MFRC522 + + Serial.println(); + Serial.println("Waiting for connection to Wi-Fi..."); + + wifiMulti.addAP("SSID", "Password"); // Add your WLAN SSID and password + + while (wifiMulti.run() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to Wi-Fi..."); + } + + Serial.println("Connected to Wi-Fi!"); } -// iteration void loop() { - // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. - if (!rfid.PICC_IsNewCardPresent()){ - return; - } - // Verify if the NUID has been read - if (!rfid.PICC_ReadCardSerial()){ - return; - } - - for (byte i = 0; i < 4; i++) { - nuidPICC[i] = rfid.uid.uidByte[i]; + // Check if it's time to read a card + if (millis() - lastReadTime > readInterval) { + if (readCard()) { + sendHttpRequest(lastCardName); } + lastReadTime = millis(); + } +} - String id = "ID: "+String(nuidPICC[0])+" : "+String(nuidPICC[1])+" : "+String(nuidPICC[2])+" : "+String(nuidPICC[3]); - Serial.println(id); - +bool readCard() { + // Look for new cards + if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { + return false; + } + // Print UID + Serial.print("Card read. UID: "); + String uidString = ""; + for (byte i = 0; i < mfrc522.uid.size; ++i) { + Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); + Serial.print(mfrc522.uid.uidByte[i], HEX); + uidString += String(mfrc522.uid.uidByte[i], HEX); + } + Serial.println(); - delay(1000); + // Search for the UID in the array and get the corresponding name + const char* cardName = getCardName(uidString.c_str()); - // Halt PICC - rfid.PICC_HaltA(); + lastCardName = cardName; + Serial.print("Card name: "); + Serial.println(cardName); - // Stop encryption on PCD - rfid.PCD_StopCrypto1(); -} \ No newline at end of file + return true; +} + +const char* getCardName(const char* uid) { + for (int i = 0; i < sizeof(uuids) / sizeof(uuids[0]); i++) { + if (strcmp(uid, uuids[i]) == 0) { + return names[i]; + } + } + return "Unknown Card"; +} + +void sendHttpRequest(String card) { + HTTPClient http; + + Serial.println("[HTTP] begin..."); + + // Create the URL with the name as a query parameter + String serverUrl = "http://IP_Address:3000/result/"; + String uidString = ""; + for (byte i = 0; i < mfrc522.uid.size; ++i) { + uidString += String(mfrc522.uid.uidByte[i], HEX); + } + const char* cardName = getCardName(uidString.c_str()); + + serverUrl += cardName; + + http.begin(serverUrl); + + Serial.println("[HTTP] GET..."); + + int httpCode = http.GET(); + + if (httpCode > 0) { + Serial.printf("[HTTP] GET... code: %d\n", httpCode); + + if (httpCode == HTTP_CODE_OK) { + String payload = http.getString(); + Serial.println(payload); + } + } else { + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); +}