Smart Classroom Occupancy Monitor + AI Energy Saver

Smart Classroom Occupancy Monitor + AI Energy Saver

Technologies Used: C++ + Raspberry Pi + Sensors + MySQL + AI


1. Hardware Components

  • Raspberry Pi (or Arduino if using C++ only)

  • PIR Sensor – Detects motion (presence)

  • Ultrasonic or IR Sensor Pair – Counts people entering or leaving

  • LDR – Measures light intensity

  • DHT11 – Monitors temperature and humidity

  • Relay Module or LEDs – Simulates fan/light control

  • Optional: LCD to display real-time status


2. Software Components

C++ Main Program (on Raspberry Pi)

  • Reads data from all sensors

  • Saves environmental and occupancy data in a MySQL database

  • Controls fan/light using GPIO with relays or LEDs

AI Energy Saver Component (Python or C++)

  • Analyzes usage patterns (occupancy, temperature, light)

  • Recommends optimal ON/OFF times

  • Forecasts unoccupied hours to save energy


3. Project Folder Structure

smart_classroom/
├── src/
│   ├── main.cpp            # Sensor reading + control logic
│   ├── db.cpp              # Database interactions
│   ├── gpio_utils.cpp      # GPIO control functions
├── ai_advisor/
│   ├── energy_predictor.py # AI logic
├── data/
│   └── readings.db         # MySQL or SQLite data storage
├── include/
│   └── sensor_headers.h    # Sensor-related declarations

4. Workflow Overview

  1. PIR detects motion → someone enters → occupancy count increases

  2. If count > 0:

    • Check temperature → adjust fan

    • Check light level → control lighting

  3. If no motion for 15 minutes → switch off fan and lights

  4. AI module runs hourly → analyzes previous data → recommends usage schedule


5. System Features Recap

This intelligent system supports:

  • Motion sensing via PIR

  • Presence detection using an ultrasonic sensor

  • Environmental monitoring (temperature, humidity, light)

  • Data logging with MySQL

  • GPIO-based device management (LEDs or relays for fan/light simulation)


6. System Requirements

  • Raspberry Pi (with WiringPi configured)

  • MySQL or MariaDB installed

  • C++ MySQL Connector (libmysqlcppconn-dev)


7. Sample Header File – sensors.h

#ifndef SENSORS_H
#define SENSORS_H

float readDistance();           // Ultrasonic
bool detectMotion();            // PIR
int readLDR();                  // Light via MCP3008
void readDHT(float &temp, float &humidity); // DHT11

#endif

8. Sample Code – sensors.cpp

#include <wiringPi.h>
#include <softPwm.h>
#include <cstdlib>
#include "sensors.h"

#define TRIG 4
#define ECHO 5
#define PIR  0

float readDistance() {
    digitalWrite(TRIG, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG, LOW);
    while(digitalRead(ECHO) == LOW);
    long start = micros();
    while(digitalRead(ECHO) == HIGH);
    long travel = micros() - start;
    return travel * 0.034 / 2;
}

bool detectMotion() {
    return digitalRead(PIR);
}

int readLDR() {
    return rand() % 1024;  // Simulated value for testing
}

void readDHT(float &temp, float &humidity) {
    temp = 26.5;
    humidity = 50.2;
}

9. Sample Header File – db.h

#ifndef DB_H
#define DB_H

void insertData(float temp, float hum, float dist, int light, int motion);

#endif

10. Sample Code – db.cpp

#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/prepared_statement.h>
#include "db.h"

void insertData(float temp, float hum, float dist, int light, int motion) {
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::PreparedStatement *stmt;

    driver = sql::mysql::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "your_user", "your_password");
    con->setSchema("smart_classroom");

    stmt = con->prepareStatement("INSERT INTO environment_log(temp, humidity, distance, light, motion) VALUES (?, ?, ?, ?, ?)");
    stmt->setDouble(1, temp);
    stmt->setDouble(2, hum);
    stmt->setDouble(3, dist);
    stmt->setInt(4, light);
    stmt->setInt(5, motion);
    stmt->execute();

    delete stmt;
    delete con;
}

11. Main Program – main.cpp

#include <wiringPi.h>
#include <iostream>
#include <unistd.h>
#include "sensors.h"
#include "db.h"

#define FAN_PIN 1
#define LIGHT_PIN 2

int main() {
    wiringPiSetup();
    pinMode(TRIG, OUTPUT);
    pinMode(ECHO, INPUT);
    pinMode(PIR, INPUT);
    pinMode(FAN_PIN, OUTPUT);
    pinMode(LIGHT_PIN, OUTPUT);

    while (true) {
        float temp, hum;
        float dist = readDistance();
        bool motion = detectMotion();
        int light = readLDR();
        readDHT(temp, hum);

        std::cout << "Motion: " << motion << ", Dist: " << dist << "cm, Temp: "
                  << temp << "°C, Hum: " << hum << "%, Light: " << light << std::endl;

        // Device control logic
        digitalWrite(FAN_PIN, (temp > 28) ? HIGH : LOW);
        digitalWrite(LIGHT_PIN, (light < 500) ? HIGH : LOW);

        insertData(temp, hum, dist, light, motion);

        sleep(5); // wait before next reading
    }

    return 0;
}

12. Sample MySQL Table

CREATE DATABASE smart_classroom;

USE smart_classroom;

CREATE TABLE environment_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    temp FLOAT,
    humidity FLOAT,
    distance FLOAT,
    light INT,
    motion BOOLEAN,
    logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Final Notes

This project is a comprehensive demonstration of how C++, Raspberry Pi, sensor integration, MySQL, and AI can be combined to create smart classroom solutions that save energy while maintaining comfort. The modularity and educational value make it ideal for schools, universities, or home automation enthusiasts.