Esp32-demo — ESP32 simulation

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

Components

Sketch code

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// ===============================
// DHT22 & Sensors & LEDs
// ===============================
#define DHTPIN 14
#define DHTTYPE DHT22
#define LED_GREEN  21
#define LED_ORANGE 19
#define LED_RED    18
#define VIBRATION_PIN 35
#define MIC_PIN 33

const int MIC_THRESHOLD = 1500;

// ประกาศขนาดจอ LCD เป็น 20x4
LiquidCrystal_I2C lcd(0x27, 20, 4);

// ===============================
// WiFi & MQTT
// ===============================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com";
const int mqtt_port = 1883;
const char* topic_sensor = "my_esp32_dht22/all_data";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
unsigned long lastMsg = 0;

void setup_wifi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void reconnect() {
  while (!client.connected()) {
    String clientId = "ESP32Client-" + String(random(0xffff), HEX);
    if (client.connect(clientId.c_str())) {} 
    else { delay(5000); }
  }
}

void allLEDOff() {
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_ORANGE, LOW);
  digitalWrite(LED_RED, LOW);
}

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

  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_ORANGE, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  allLEDOff();

  pinMode(VIBRATION_PIN, INPUT);
  pinMode(MIC_PIN, INPUT);
  analogReadResolution(12);

  dht.begin();

  Wire.begin(22, 23);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("System Starting...");

  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  delay(1500);
  lcd.clear();
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) setup_wifi();
  if (!client.connected()) reconnect();
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg >= 1000) {
    lastMsg = now;

    float h = dht.readHumidity();
    float t = dht.readTemperature();
    int vibrationState = digitalRead(VIBRATION_PIN);
    int micValue = analogRead(MIC_PIN);

    if (isnan(h) || isnan(t)) return;

    String statusStr;
    if (micValue >= MIC_THRESHOLD) {
      allLEDOff();
      digitalWrite(LED_RED, HIGH);
      statusStr = "Error: Mic High";
    }
    else if (vibrationState == LOW) {
      allLEDOff();
      digitalWrite(LED_ORANGE, HIGH);
      statusStr = "Error: Vibration";
    }
    else if (t >= 76) {
      allLEDOff();
      digitalWrite(LED_RED, HIGH);
      statusStr = "Error: Temp High";
    }
    else if (t >= 55) {
      allLEDOff();
      digitalWrite(LED_ORANGE, HIGH);
      statusStr = "Warning: Temp";
    }
    else {
      allLEDOff();
      digitalWrite(LED_GREEN, HIGH);
      statusStr = "Normal";
    }

    String vibrationStatus = (vibrationState == LOW) ? "DETECTED" : "NORMAL";

    // ส่ง MQTT
    String combinedData = "Temp: " + String(t, 2) + " C, Humidity: " + String(h, 2) + " %, Vibration: " + String(vibrationSta

More projects by aekkachai-c2545