UTS IOT FIX 2 — ESP32-S3 simulation

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

Components

Sketch code

#include <WiFi.h>
#include <WiFiClient.h>
#include "MqttHub.h"
#include <Adafruit_NeoPixel.h>
#include <DHT.h>

// ==========================================
// PIN & HARDWARE DEFINITIONS (ESP32-S3)
// ==========================================
#define BUTTON_PIN        4     // Push button pin
#define DHT_PIN           1     // DHT22 signal pin
#define DOOR_SWITCH_PIN   42    // Magnetic door switch signal pin
#define LED_STRIP_1_PIN   2     // RGB LED strip 1 signal pin
#define LED_STRIP_2_PIN   41    // RGB LED strip 2 signal pin

#define NUM_LEDS          60    // Number of LEDs on each strip
#define DHTTYPE           DHT22

// Objects
Adafruit_NeoPixel strip1(NUM_LEDS, LED_STRIP_1_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(NUM_LEDS, LED_STRIP_2_PIN, NEO_GRB + NEO_KHZ800);
DHT dht(DHT_PIN, DHTTYPE);

// --- MEMORI STATE LAMPU (Terpisah agar bisa dikontrol Hub) ---
bool lampState[2] = {true, true};        // Default ON saat menyala
uint8_t lampR[2] = {150, 150};           // Default Merah (Komposisi Ungu)
uint8_t lampG[2] = {0, 0};               // Default Hijau 
uint8_t lampB[2] = {255, 255};           // Default Biru (Komposisi Ungu)
uint8_t lampBright[2] = {255, 255};      // Kecerahan Maksimal

// Variables for Push Button Debounce
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;

// Variables for Non-blocking DHT Reading
unsigned long lastDHTReadTime = 0;
const unsigned long dhtInterval = 2000;

float humidity = 0.0f;
float tempC = 0.0f;

// Variable for Magnetic Switch State
int lastDoorState = -1;

WiFiClient wifiClient;

// ==========================================
// FUNGSI UPDATE LED FISIK
// ==========================================
void updateLamp(int idx) {
  Adafruit_NeoPixel& strip = (idx == 0) ? strip1 : strip2;
  
  if (!lampState[idx]) {
    strip.clear(); // Matikan
  } else {
    strip.setBrightness(lampBright[idx]);
    uint32_t color = strip.Color(lampR[idx], lampG[idx], lampB[idx]);
    for(int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, color);
    }
  }
  strip.show();
}

// ==========================================
// PARSER PERINTAH DARI HUB UTAMA
// ==========================================
void parseLampCommand(int idx, String cmd) {
  if (cmd == "ON") {
    lampState[idx] = true;
  } 
  else if (cmd == "OFF") {
    lampState[idx] = false;
  } 
  else if (cmd.startsWith("RGB:")) {
    // Membedah format "RGB:255,100,50,255" dari Hub Utama
    int c1 = cmd.indexOf(':');
    int c2 = cmd.indexOf(',', c1 + 1);
    int c3 = cmd.indexOf(',', c2 + 1);
    int c4 = cmd.indexOf(',', c3 + 1);
    
    if (c1 > 0 && c2 > 0 && c3 > 0 && c4 > 0) {
      lampR[idx] = cmd.substring(c1 + 1, c2).toInt();
      lampG[idx] = cmd.substring(c2 + 1, c3).toInt();
      lampB[idx] = cmd.substring(c3 + 1, c4).toInt();
      lampBright[idx] = cmd.substring(c4 + 1).toInt();
      lampState[idx] = true; // Otomatis ON jika ada perubahan 

More projects by itsmebenn