proyectolab6 — Esp32 Cam simulation

An interactive Esp32 Cam circuit simulation you can run free in your browser on Velxio, by amirandalo.

Components

Sketch code

// ╔══════════════════════════════════════════════════════════╗ 
// ║  VIGIAIOT — Sistema de Vigilancia con ESP32-CAM          ║ 
// ║  Pines Optimizados para tu simulación Wokwi (13,12,16,2) ║ 
// ║  Taller IoT — EPIS UNSA 2026-A | Velxio.dev             ║ 
// ╚══════════════════════════════════════════════════════════╝ 

#include <WiFi.h> 
#include <WebServer.h> 
#include <WiFiClientSecure.h> 
#include <PubSubClient.h> 
#include <UniversalTelegramBot.h> 
#include <NTPClient.h> 
#include <WiFiUdp.h> 
#include <Preferences.h> 
#include <ArduinoJson.h> 
#include <time.h> 

// ── Configuración ──────────────────────────────────────── 
const char* SSID      = "Wokwi-GUEST"; 
const char* PASS      = ""; 
const char* BROKER    = "broker.hivemq.com"; 
const char* BOT_TOKEN = "8522508063:AAEUVC2FBAUPNboPBIyDNL-XiDf0Tv8XsSc";  
const char* CHAT_ID   = "8769231224";   

// ── Topics MQTT ────────────────────────────────────────── 
#define T_MOVIMIENTO  "epis/vigia/movimiento" 
#define T_CAPTURA     "epis/vigia/captura" 
#define T_ANALISIS    "epis/vigia/ia/resultado" 
#define T_ESTADO      "epis/vigia/estado" 
#define T_CONTROL     "epis/vigia/control" 

// ── Pines de tu Simulación Wokwi ───────────────────────── 
#define PIR_PIN    13  
#define LED_FLASH  4 
#define LED_ROJO   12  
#define LED_VERDE  16  
#define SENSOR_LUZ  2  

// ── Sistema de estados ─────────────────────────────────── 
enum EstadoSistema { NORMAL, ALERTA, BLOQUEO }; 
EstadoSistema estado = NORMAL; 
int    totalCapturas    = 0; 
int    totalDetecciones = 0; 
bool   streamingActivo  = false; 
bool   modoNocturno     = false; 

String ultimoMovimiento = "{}";
String ultimaCaptura    = "{}";
String ultimoAnalisis   = "{}";
String ultimoEstado     = "{}";

// ── Objetos globales ───────────────────────────────────── 
WiFiUDP          ntpUDP; 
NTPClient        ntp(ntpUDP, "pool.ntp.org", -18000, 60000); 
WebServer        server(80); 
WiFiClient       mqttCli; 
PubSubClient     mqtt(mqttCli); 
WiFiClientSecure httpsCli; 
UniversalTelegramBot tgBot(BOT_TOKEN, httpsCli); 
Preferences      prefs; 

WiFiClient sseClient; // Cliente TCP activo para Server-Sent Events

// ── Callback MQTT (Comandos remotos) ───────────────────── 
void onMqttMsg(char* topic, byte* payload, unsigned int len) { 
   String msg = ""; 
   for(unsigned int i=0; i<len; i++) msg += (char)payload[i]; 
   Serial.printf("[MQTT<] %s: %s\n", topic, msg.c_str()); 
   
   if(String(topic) == T_CONTROL) { 
     JsonDocument d; 
     if(!deserializeJson(d, msg)) { 
       if(d.containsKey("flash")) ledcWrite(0, d["flash"].as<int>()); 
       if(d.containsKey("modo") && String((const char*)d["modo"]) == "noche") {
         modoNocturno = true; 
       }
     } 
   } 
} 

// ── Función principal de alerta e Inteligencia Artificial ── 
void procesarDeteccion(String fuente) { 
   ntp.update(); 
   totalDetecciones++; 
   totalCapturas++; 
   
   unsigned long epochTime = ntp.getEpochTime();
   struct tm *ptm = gmtime((const time_t *

More projects by amirandalo