garasi — 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 <ESP32Servo.h>
#include <Adafruit_NeoPixel.h>

// ==========================================
// WIFI & MQTT
// ==========================================
WiFiClient wifiClient;
const char* MQTT_TOPIC = "smarthome_acd81x/garasiku_65f21x0z/sensor";

// ==========================================
// PIN GPIO (Sesuai Diagram Kamu)
// ==========================================
#define TRIG_PIN       5   // Pin Trigger HC-SR04
#define ECHO_PIN      18   // Pin Echo HC-SR04
#define SERVO_PIN      21   // Pin Servo
#define BTN_OPEN       6   // Pin Tombol Open (Diagonal ke GND)
#define BTN_CLOSE      7   // Pin Tombol Close (Diagonal ke GND)
#define LED_PIN       38   // Pin Data LED Strip
#define NUM_LEDS      60   // Jumlah LED pada Strip

// ==========================================
// OBJECT & VARIABLE
// ==========================================
Servo myServo;
Adafruit_NeoPixel leds(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

int servoAngle = 0;
unsigned long lastMqttPublish = 0;

// ==========================================
// CALLBACK MQTT
// ==========================================
void onMqttMessageReceived(String topic, String payload) {
  Serial.println("MQTT Masuk: [" + topic + "] -> " + payload);

  if (payload == "BUKA") {
    servoAngle = 90;
    myServo.write(servoAngle);
    Serial.println("MQTT -> Pintu DIBUKA");
  }

  if (payload == "TUTUP") {
    servoAngle = 0;
    myServo.write(servoAngle);
    Serial.println("MQTT -> Pintu DITUTUP");
  }
}

// ==========================================
// BACA JARAK
// ==========================================
float readDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) return -1;

  return duration * 0.0343 / 2;
}

// ==========================================
// SETUP
// ==========================================
void setup() {
  Serial.begin(115200);
  delay(300); // Penstabil Boot Simulator

  // Sensor Ultrasonik
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Button (Internal Pull-Up)
  pinMode(BTN_OPEN, INPUT_PULLUP);
  pinMode(BTN_CLOSE, INPUT_PULLUP);

  // Servo
  myServo.attach(SERVO_PIN, 500, 2400);
  myServo.write(servoAngle);

  // Inisialisasi LED NeoPixel
  leds.begin();
  leds.clear();
  leds.show();

  // WIFI
  WiFi.begin("Velxio-GUEST", "");
  Serial.print("Menghubungkan WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi Terhubung!");

  // MQTT
  setupMqtt(wifiClient, "broker.hivemq.com", 1883, "KucingMiawSigma67_Garasi");
  setMqttCallback(onMqttMessageReceived);
}

// ==========================================
// LOOP
// ==========================================
void loop() {
  // 1. MQTT Process
  if (WiFi.status() == WL_CONNECTED) {
    loopM

More projects by itsmebenn