#include <WiFi.h>
#include <HTTPClient.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";
String apiUrl = "https://virtserver.swaggerhub.com/ABDULAHALI982/Access_Scan/1.0.0/get-user";
const int RELAY_PIN = 16;
bool relayState = false;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize the relay
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
// Generate a random RFID UID for demonstration purposes
String uid = generateRandomUid();
// Make the GET request
makeGetRequest(uid);
// Toggle relay based on the response
toggleRelay();
delay(10000); // Wait for 10 seconds before the next request
}
void makeGetRequest(String uid) {
HTTPClient http;
// Construct the GET request URL with the RFID UID
String url = apiUrl + "?uid=" + uid;
// Begin the request
http.begin(url);
// Send the GET request
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String response = http.getString();
Serial.println("API Response: " + response);
// You can add more logic here based on the response if needed
} else {
Serial.println("Error in HTTP request. HTTP Response Code: " + httpResponseCode);
}
// End the request
http.end();
}
void toggleRelay() {
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState);
}
String generateRandomUid() {
String uid = "A1254627";
return uid;
}