TESTsensoer — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 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>

// กำหนดพินอุปกรณ์
#define DHTPIN 14
#define DHTTYPE DHT22

#define LED_GREEN 21
#define LED_ORANGE 19
#define LED_RED 18

#define BUZZER_PIN 4
#define VIBRATION_PIN 35
#define MIC_PIN 33

// กำหนดค่า Threshold
const int MIC_WARNING = 1629;
const int MIC_ERROR = 2470;

const float TEMP_WARNING = 55.0;
const float TEMP_ERROR = 76.0;

// LCD I2C (20x4)
LiquidCrystal_I2C lcd(0x27, 20, 4);

// WiFi & MQTT Config
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_1";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

unsigned long lastMsg = 0;

void setup_wifi() {
  WiFi.mode(WIFI_STA);
  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())) {
      Serial.println("MQTT Connected");
    } else {
      delay(2000);
    }
  }
}

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);
  pinMode(BUZZER_PIN, OUTPUT);

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

  analogReadResolution(12);

  allLEDOff();
  digitalWrite(BUZZER_PIN, LOW);

  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)) {
      Serial.println("DHT22 Read Error");
      return;
    }

    String statusStr;
    bool alertActive = false;

    // เคลียร์สถานะ LED ก่อนเช็คเงื่อนไข
    allLEDOff();

    // 1. ระดับวิกฤต: ไฟแดง + บัซเซอร์
    if (micValue >= MIC_ERROR || t >= TEMP_ERROR) {
      digitalWrite(LED_RED, HIGH);
      statusStr = (micValue >= MIC_ERROR) ? "Error: Mic High" : "Error: Temp High";
      alertActive = true;
    }
    // 2. ระดับสั่นสะเทือน: ไฟส้ม
    else if (vibrationState == HIGH) {
      digitalWrite(LED_ORANGE, HIGH);
      statusStr = "Error: Vibration";
    }
    // 3. ระดับเตือน (Warning): ไฟส้ม
    else if (micValue >= MIC_WARNING || t >= TEMP_WARNING) {
      digitalWrite(LED_OR

More projects by aekkachai-c2545