#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Constants
#define PIR_PIN_IN 34
#define PIR_PIN_OUT 35
#define LED_PIN 25 // Changed to avoid conflict
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define Fan_PIN 12
#define Temp_PIN 4
#define MQ2_PIN 16
#define BUZZER_PIN 26 // New buzzer pin
//// Constants-with websocket real time monitoring
// #define PIR_PIN_IN 34
// #define PIR_PIN_OUT 35
// #define LED_PIN 27
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// #define OLED_RESET -1
// #define Fan_PIN 25
// #define DHTPIN 4 // Changed from Temp_PIN to DHTPIN
// #define MQ2_PIN 16
// #define BUZZER_PIN 26
// #define DHTTYPE DHT22
// OLED Display Object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// People Counter Class
class PeopleCounter {
private:
int peopleCount;
bool motionDetected_IN;
bool motionDetected_OUT;
unsigned long lastEventTime;
String lastEventType;
int lastPersonID;
bool eventChanged;
public:
PeopleCounter() {
peopleCount = 0;
motionDetected_IN = false;
motionDetected_OUT = false;
lastEventTime = 0;
lastEventType = "";
lastPersonID = 0;
eventChanged = false;
}
void testHardwarePin() {
pinMode(Temp_PIN, INPUT);
pinMode(Fan_PIN, OUTPUT);
pinMode(MQ2_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(Fan_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
int temp = digitalRead(Temp_PIN);
int MQ = digitalRead(MQ2_PIN);
if (temp == LOW && MQ == LOW) {
Serial.println("Temp MQ");
}
}
void initializeSystem() {
pinMode(PIR_PIN_IN, INPUT_PULLUP);
pinMode(PIR_PIN_OUT, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(5, 5);
display.println("Initializing...");
display.display();
delay(1000);
display.clearDisplay();
}
void detectMotion() {
int val_IN = digitalRead(PIR_PIN_IN);
int val_OUT = digitalRead(PIR_PIN_OUT);
if (val_IN == LOW && !motionDetected_IN) {
motionDetected_IN = true;
peopleCount++;
lastPersonID++;
lastEventTime = millis();
lastEventType = "IN";
eventChanged = true;
Serial.println("Person Entered");
} else if (val_IN == HIGH && motionDetected_IN) {
motionDetected_IN = false;
}
if (val_OUT == LOW && !motionDetected_OUT) {
motionDetected_OUT = true;
if (peopleCount > 0) {
peopleCount--;
lastEventTime = millis();
lastEventType = "OUT";
eventChanged = true;
Serial.println("Person Exited");
}
} else if (val_OUT == HIGH && motionDetected_OUT) {
motionDetected_OUT = false;
}
}
void controlLED() {
digitalWrite(LED_PIN, peopleCount > 0 ? HIGH : LOW);
}
void updateOLED() {
if (eventChanged) {
displayTimeAndCount();
eventChanged = false;
}
}
void monitorEnvironment() {
int tempValue = analogRead(Temp_PIN); // Read analog temperature sensor
float voltage = tempValue * (3.3 / 4095.0); // For ESP32 12-bit ADC and 3.3V
float temperatureC = (voltage - 0.5)*100; // TMP36 correction
int mq2Status = digitalRead(MQ2_PIN);
Serial.print("Temperature: ");
Serial.println(temperatureC);
Serial.print("Gas Sensor: ");
Serial.println(mq2Status == LOW ? "Gas Detected!" : "No Gas");
// Fan and Buzzer control
if (temperatureC > 24 && peopleCount > 0) {
digitalWrite(Fan_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
Serial.println("Fan and Buzzer ON");
} else {
digitalWrite(Fan_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("Fan and Buzzer OFF");
}
// Display gas alert on OLED
if (mq2Status == LOW) { // Gas detected
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Warning!");
display.println("Gas Detected");
display.display();
}
}
private:
void displayTimeAndCount() {
unsigned long seconds = (lastEventTime / 1000) % 60;
unsigned long minutes = (lastEventTime / 60000) % 60;
unsigned long hours = (lastEventTime / 3600000) % 24;
display.clearDisplay();
display.setTextSize(1);
display.setCursor(1, 1);
display.print("Event: ");
display.print(lastEventType);
display.setCursor(0, 15);
display.print("Time: ");
if (hours < 10) display.print("0");
display.print(hours);
display.print(":");
if (minutes < 10) display.print("0");
display.print(minutes);
display.print(":");
if (seconds < 10) display.print("0");
display.print(seconds);
display.setCursor(0, 35);
display.print("Count: ");
display.print(peopleCount);
display.display();
}
} counter;
void setup() {
counter.initializeSystem();
counter.testHardwarePin();
}
void loop() {
counter.detectMotion();
counter.controlLED();
counter.updateOLED();
counter.monitorEnvironment();
delay(1000);
}