gg — ESP32 simulation

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

Components

Sketch code

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

// --- Main ESP32 TFT Pin Definitions (Verified Working) ---
#define TFT_CS   15
#define TFT_DC   14
#define TFT_RST   2
#define TFT_MOSI 13
#define TFT_SCK  12
#define TFT_MISO 19

SPIClass customSPI(VSPI);
Adafruit_ILI9341 tft = Adafruit_ILI9341(&customSPI, TFT_DC, TFT_CS, TFT_RST);

bool lastDetectionState = false;
unsigned long lastPacketReceived = 0;

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

  // Initialize UART1 to listen to ESP32-CAM (RX1 = GPIO 26, TX1 = GPIO 25)
  Serial1.begin(115200, SERIAL_8N1, 26, 25);

  // Initialize Custom SPI Bus & Display
  customSPI.begin(TFT_SCK, TFT_MISO, TFT_MOSI, TFT_CS);
  tft.begin();
  tft.setRotation(1); // Landscape mode
  tft.fillScreen(ILI9341_BLACK);

  // Draw Static Dashboard Header
  tft.fillRect(0, 0, 320, 35, ILI9341_NAVY);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.setCursor(15, 10);
  tft.print("HARVEST DASHBOARD");

  tft.drawFastHLine(0, 36, 320, ILI9341_WHITE);

  renderDashboardCard(false);
}

void loop() {
  // Listen for incoming packets from the ESP32-CAM
  if (Serial1.available()) {
    String payload = Serial1.readStringUntil('\n');
    payload.trim();

    if (payload.indexOf("STATUS:PERSON_FOUND") >= 0) {
      lastPacketReceived = millis();
      if (!lastDetectionState) {
        lastDetectionState = true;
        renderDashboardCard(true);
      }
    } else if (payload.indexOf("STATUS:CLEAR") >= 0) {
      lastPacketReceived = millis();
      if (lastDetectionState) {
        lastDetectionState = false;
        renderDashboardCard(false);
      }
    }
  }

  // Timeout guard: Revert to safe state if camera packet stops
  if (millis() - lastPacketReceived > 4000 && lastPacketReceived != 0) {
    if (lastDetectionState) {
      lastDetectionState = false;
      renderDashboardCard(false);
    }
  }
}

void renderDashboardCard(bool isDetected) {
  if (isDetected) {
    // Red Alert Card
    tft.fillRect(15, 60, 290, 150, ILI9341_RED);
    tft.drawRect(15, 60, 290, 150, ILI9341_WHITE);

    tft.setTextSize(3);
    tft.setTextColor(ILI9341_WHITE);
    tft.setCursor(35, 85);
    tft.print("ALERT!");

    tft.setTextSize(2);
    tft.setCursor(35, 130);
    tft.print("PERSON DETECTED");

    tft.setTextSize(1);
    tft.setCursor(35, 170);
    tft.print("Camera trigger active.");
  } else {
    // Safe Status Card
    tft.fillRect(15, 60, 290, 150, ILI9341_DARKCYAN);
    tft.drawRect(15, 60, 290, 150, ILI9341_WHITE);

    tft.setTextSize(2);
    tft.setTextColor(ILI9341_WHITE);
    tft.setCursor(35, 95);
    tft.print("SYSTEM SECURE");

    tft.setCursor(35, 135);
    tft.setTextColor(ILI9341_GREENYELLOW);
    tft.print("NO DETECTION");

    tft.setTextSize(1);
    tft.setTextColor(ILI9341_WHITE);
    tft.setCursor(35, 175);
    tft.print("Field monitoring active...");
  }
}

More projects by seabecyber