#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#define SS_PIN 10 // RFID Selection Pin
#define RST_PIN 9 // RFID Reset Pin
// Your WiFi credentials
const char* ssid = "mousa_5Ghz";
const char* password = "***************";
// Your server endpoint
const char* serverEndpoint = "https://script.google.com/macros/s/AKfycbyPeD6cVu1ond1pIno6_20T3UXPpUNDaR3Pf1vLsv1AQ6_6_AKw0xGeuwI2qx_XLjJY/exec";
MFRC522 rfid(SS_PIN, RST_PIN);
struct UserInfo {
String uid;
String fullName;
String id;
};
UserInfo userList[] = {
{"F3D74CA8", "Alice", "003"},
{"430D96A6", "Bob", "002"},
// Add more users if required
};
const int userCount = sizeof(userList) / sizeof(userList[0]);
String encodeUrl(const String& input) {
String encoded = "";
for (char c : input) {
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encoded += c;
} else {
encoded += '%';
encoded += String((int)c, HEX);
}
}
return encoded;
}
bool submitUID(String uid, String fullName, String id) {
Serial.print("Sending UID: ");
Serial.println(uid);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Attempting to reconnect to WiFi...");
WiFi.begin(ssid, password);
for (int attempts = 0; attempts < 10 && WiFi.status() != WL_CONNECTED; attempts++) {
delay(1000);
Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Unable to reconnect to WiFi.");
return false;
}
Serial.println("Connected to WiFi.");
}
WiFiClient client;
if (client.connect("script.google.com", 443)) {
String requestUrl = String(serverEndpoint) + "?id=" + encodeUrl(id) + "&fullName=" + encodeUrl(fullName);
Serial.println("Request URL: " + requestUrl);
client.print("GET " + requestUrl + " HTTP/1.1\r\n" +
"Host: script.google.com\r\n" +
"Connection: close\r\n\r\n");
unsigned long start = millis();
while (client.connected() && millis() - start < 5000) {
if (client.available()) {
String serverResponse = client.readStringUntil('\n');
Serial.println("Response: " + serverResponse);
if (serverResponse.startsWith("HTTP/1.1 200")) {
Serial.println("Data sent successfully.");
return true;
}
}
}
Serial.println("Server response timed out.");
client.stop();
return false;
} else {
Serial.println("Failed to connect to the server.");
return false;
}
}
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Initializing...");
SPI.begin();
rfid.PCD_Init();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected successfully.");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) return;
if (!rfid.PICC_ReadCardSerial()) return;
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toUpperCase();
Serial.println("Detected UID: " + uid);
String fullName = "";
String id = "";
bool isRecognized = false;
for (int j = 0; j < userCount; j++) {
if (uid.equals(userList[j].uid)) {
isRecognized = true;
fullName = userList[j].fullName;
id = userList[j].id;
break;
}
}
if (isRecognized) {
Serial.println("Valid UID detected. Sending information...");
submitUID(uid, fullName, id);
} else {
Serial.println("Access Denied: Invalid UID.");
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
The provided code is failing to send data when swiping the RFID card. I’ve verified the app script URL in a browser, so the script should work. Could anyone identify potential issues or necessary modifications in the code?