Update src/arduino/card_scanner.ino

main
Eyuep Sueyruege 2023-11-21 16:13:43 +01:00
parent 4bd1daed40
commit 07eaebe774
1 changed files with 129 additions and 36 deletions

View File

@ -1,53 +1,146 @@
// imports #include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#define RST_PIN 22
#define SS_PIN 21
// Pin Definition String lastCardName = "";
#define SS_PIN 10
#define RST_PIN 9
// 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 unsigned long lastReadTime = 0;
void setup() { unsigned long readInterval = 1000; // Interval between card reading operations in milliseconds
// Serial Begin
Serial.begin(9600); WiFiMulti wifiMulti;
// Init SPI bus
SPI.begin(); void setup() {
// Init MFRC522 Serial.begin(115200);
rfid.PCD_Init(); 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() { void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. // Check if it's time to read a card
if (!rfid.PICC_IsNewCardPresent()){ if (millis() - lastReadTime > readInterval) {
return; if (readCard()) {
} sendHttpRequest(lastCardName);
// 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];
} }
lastReadTime = millis();
}
}
String id = "ID: "+String(nuidPICC[0])+" : "+String(nuidPICC[1])+" : "+String(nuidPICC[2])+" : "+String(nuidPICC[3]); bool readCard() {
Serial.println(id); // 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 lastCardName = cardName;
rfid.PICC_HaltA(); Serial.print("Card name: ");
Serial.println(cardName);
// Stop encryption on PCD return true;
rfid.PCD_StopCrypto1(); }
}
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();
}