#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include <ESP32Servo.h>
#define SDA 25
#define SCL 26
#define RFID_SS_PIN 21 // RFID reader slave select pin
#define RFID_RST_PIN 22 // RFID reader reset pin
#define IR_SENSOR_EXIT_A_PIN 33 // Exit sensor pin for ZoneA
#define IR_SENSOR_EXIT_B_PIN 34 // Exit sensor pin for ZoneB
#define SERVO_ZONE_A_PIN 12 // Servomotor pin for ZoneA gate
#define SERVO_ZONE_B_PIN 14 // Servomotor pin for ZoneB gate
#define SERVO_MAIN_PIN 13 // Servomotor pin for the main gate not found 13 is working
#define LED_ZONE_A_PIN 27 // LED pin for ZoneA
#define LED_ZONE_B_PIN 2 // LED pin for ZoneB
#define LED_MAIN_PIN 4 // LED pin for the main gate
#define BUZZER_PIN 15 // Buzzer pin
const char* ssid = "zproject.wifi";
const char* password = "source.pyc";
const char* serverAddress = "http://tutorial.iotmajorprojects.com/rfid_post_api.php";
// Initialize RFID reader
MFRC522 rfid(RFID_SS_PIN, RFID_RST_PIN);
// Initialize Servos
Servo servoZoneA;
Servo servoZoneB;
Servo servoMain;
int zoneACount = 0; // Variable to store the count of people in ZoneA
int zoneBCount = 0; // Variable to store the count of people in ZoneB
int maxCapacity = 3; // Maximum capacity of each zone
int previousZoneACount = -1; // To store the previous count of ZoneA
int previousZoneBCount = -1; // To store the previous count of ZoneB
WiFiClient client;
TaskHandle_t SensorTaskHandle = NULL;
void setup() {
Serial.begin(9600); // Initialize serial communication
Wire.begin(); // Initialize I2C communication for LCD
initWiFi();
SPI.begin();
rfid.PCD_Init();
servoZoneA.attach(SERVO_ZONE_A_PIN);
servoZoneB.attach(SERVO_ZONE_B_PIN);
servoMain.attach(SERVO_MAIN_PIN);
pinMode(LED_ZONE_A_PIN, OUTPUT);
pinMode(LED_ZONE_B_PIN, OUTPUT);
pinMode(LED_MAIN_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(IR_SENSOR_EXIT_A_PIN, INPUT);
pinMode(IR_SENSOR_EXIT_B_PIN, INPUT);
servoZoneA.write(0);
servoZoneB.write(0);
digitalWrite(LED_ZONE_A_PIN, HIGH);
digitalWrite(LED_ZONE_B_PIN, HIGH);
// Create a task for sensor reading and counting
xTaskCreate(
sensorTask, // Task function
"Sensor Task", // Name of the task
2048, // Stack size for the task
NULL, // Parameter to pass
1, // Task priority
&SensorTaskHandle // Task handle
);
}
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting... to WiFi SSID: ");
Serial.println(ssid);
delay(1000);
}
Serial.println(WiFi.localIP());
}
void loop() {
// Read RFID tags and update zone count
readRFIDTags();
// Manage Zone gates based on zone count
manageZoneGates();
// Manage main gate based on zone counts
manageMainGate();
// Delay for stability
delay(1000);
}
void sensorTask(void* parameter) {
for (;;) {
// Check if exit sensor for ZoneA is triggered
if (digitalRead(IR_SENSOR_EXIT_A_PIN) == HIGH) {
zoneACount = max(0, zoneACount - 1);
updateServerWithZoneCounts(); // Update server with new counts
delay(100); // Debounce delay
}
// Check if exit sensor for ZoneB is triggered
if (digitalRead(IR_SENSOR_EXIT_B_PIN) == HIGH) {
zoneBCount = max(0, zoneBCount - 1);
updateServerWithZoneCounts(); // Update server with new counts
delay(100); // Debounce delay
}
vTaskDelay(100 / portTICK_PERIOD_MS); // Task delay
}
}
void readRFIDTags() {
// Check for RFID tag
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String rfidTag = "";
for (byte i = 0; i < rfid.uid.size; i++) {
rfidTag += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
rfidTag += String(rfid.uid.uidByte[i], HEX);
}
rfidTag.toUpperCase();
Serial.println("Card No: " + rfidTag);
rfid.PICC_HaltA();
// Verify and manage zones based on the tag
if (zoneACount < maxCapacity) {
// Valid card and capacity available in ZoneA, open the gate
servoZoneA.write(90);
digitalWrite(BUZZER_PIN, HIGH); // Buzzer on
delay(1000); // Keep buzzer on for 1 second
digitalWrite(BUZZER_PIN, LOW); // Buzzer off
// Wait for 10 seconds before closing the gate
delay(10000);
servoZoneA.write(0);
// Increment ZoneA count
zoneACount++;
updateServerWithZoneCounts(); // Update server with new counts
} else if (zoneBCount < maxCapacity) {
// Valid card and capacity available in ZoneB, open the gate
servoZoneB.write(90);
digitalWrite(BUZZER_PIN, HIGH); // Buzzer on
delay(1000); // Keep buzzer on for 1 second
digitalWrite(BUZZER_PIN, LOW); // Buzzer off
// Wait for 10 seconds before closing the gate
delay(10000);
servoZoneB.write(0);
// Increment ZoneB count
zoneBCount++;
updateServerWithZoneCounts(); // Update server with new counts
} else {
// Both zones are full, deny access
Serial.println("Both zones are full, access denied.");
digitalWrite(LED_MAIN_PIN, HIGH); // Main gate LED red
delay(3000); // Keep LED on for 3 seconds
digitalWrite(LED_MAIN_PIN, LOW); // Main gate LED off
}
// Send tag data to server
sendTAGDataToServer(rfidTag);
}
delay(1000); // Adjust delay as needed
}
void manageZoneGates() {
// Manage ZoneA gate
if (zoneACount >= maxCapacity) {
servoZoneA.write(0); // Close ZoneA gate
digitalWrite(LED_ZONE_A_PIN, HIGH); // Red LED on
} else {
servoZoneA.write(90); // Open ZoneA gate
digitalWrite(LED_ZONE_A_PIN, LOW); // Red LED off
}
// Manage ZoneB gate
if (zoneBCount >= maxCapacity) {
servoZoneB.write(0); // Close ZoneB gate
digitalWrite(LED_ZONE_B_PIN, HIGH); // Red LED on
} else {
servoZoneB.write(90); // Open ZoneB gate
digitalWrite(LED_ZONE_B_PIN, LOW); // Red LED off
}
}
void manageMainGate() {
// Manage main gate based on zone counts
if (zoneACount >= maxCapacity && zoneBCount >= maxCapacity) {
servoMain.write(0); // Close main gate
digitalWrite(LED_MAIN_PIN, HIGH); // Red LED on
} else {
servoMain.write(90); // Open main gate
digitalWrite(LED_MAIN_PIN, LOW); // Red LED off
}
}
void sendTAGDataToServer(String rfidTag) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverAddress);
// Construct JSON payload
String jsonData = "{";
jsonData += "\"rfidTag\": \"";
jsonData += rfidTag;
jsonData += "\"}";
Serial.println("Sending JSON data: " + jsonData);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Server response: ");
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}
void updateServerWithZoneCounts() {
if (WiFi.status() == WL_CONNECTED) {
if (zoneACount != previousZoneACount || zoneBCount != previousZoneBCount) {
HTTPClient http;
http.begin(serverAddress);
// Construct JSON payload
String jsonData = "{";
jsonData += "\"zoneACount\": ";
jsonData += String(zoneACount);
jsonData += ", \"zoneBCount\": ";
jsonData += String(zoneBCount);
jsonData += "}";
Serial.println("Sending JSON data: " + jsonData);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Server response: ");
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
// Update previous counts
previousZoneACount = zoneACount;
previousZoneBCount = zoneBCount;
}
} else {
Serial.println("WiFi Disconnected");
}
}