smartclassroom — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by gorebhakti277.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

// -------- ESP32 PINS --------
#define DHT_PIN     4
#define DHT_TYPE    DHT22
#define PIR_PIN     13
#define LDR_PIN     34
#define FAN_PIN     26
#define LED_PIN     15

// -------- OLED --------
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

Adafruit_SSD1306 display(
  SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET
);

DHT dht(DHT_PIN, DHT_TYPE);

// -------- DEMO SETTINGS --------
const int DARK_THRESHOLD = 1500;
const unsigned long UPDATE_INTERVAL = 1000;

// Simulated values for fast demonstration
int studentCount = 0;
int simulatedLight = 300;

float temperature = 25.0;
float humidity = 50.0;

int actualLDR = 0;
int motion = 0;

bool fanOn = false;
bool ledOn = false;

unsigned long lastUpdate = 0;
int demoStep = 0;

void setup() {
  Serial.begin(115200);

  pinMode(PIR_PIN, INPUT);
  pinMode(LDR_PIN, INPUT);
  pinMode(FAN_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  digitalWrite(FAN_PIN, LOW);
  digitalWrite(LED_PIN, LOW);

  Wire.begin(21, 22);
  dht.begin();

  if (display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    display.clearDisplay();
    display.setTextColor(SSD1306_WHITE);
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.println("SMART CLASSROOM");
    display.println("Demo starting...");
    display.display();
  }

  randomSeed(micros());
  delay(500);
}

void loop() {
  if (millis() - lastUpdate < UPDATE_INTERVAL) {
    return;
  }

  lastUpdate = millis();

  // 1. Read real simulated sensors
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if (!isnan(t)) temperature = t;
  if (!isnan(h)) humidity = h;

  actualLDR = analogRead(LDR_PIN);
  motion = digitalRead(PIR_PIN);

  // 2. Cycle through student counts: 0, 1, 2, 3, 4, 5
  studentCount = demoStep % 6;

  // 3. Automatically vary demo light intensity
  // Low = dark, high = bright
  int lightCycle = demoStep % 8;

  if (lightCycle < 4) {
    simulatedLight = 300 + lightCycle * 300;
  } else {
    simulatedLight = 2100 - (lightCycle - 4) * 400;
  }

  // 4. Fan ON only when MORE THAN 2 students
  fanOn = (studentCount > 2);

  // 5. LED ON when simulated light is below threshold
  ledOn = (simulatedLight < DARK_THRESHOLD);

  // 6. Apply outputs
  digitalWrite(FAN_PIN, fanOn ? HIGH : LOW);
  digitalWrite(LED_PIN, ledOn ? HIGH : LOW);

  // 7. Update OLED
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);
  display.println("SMART CLASSROOM");

  display.setCursor(0, 9);
  display.print("Temp:");
  display.print(temperature, 1);
  display.print("C Hum:");
  display.print(humidity, 0);
  display.println("%");

  display.setCursor(0, 18);
  display.print("LDR:");
  display.print(actualLDR);
  display.print(" Demo:");
  display.println(simulatedLight);

  display.setCursor(0, 27);
  display.print("Motion:");
  display.println(motion ? "YES" : "NO");

  displa