avanzado1 — Esp32 Cam simulation

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

Sketch code

// A-02 — TensorFlow Lite Micro en ESP32-CAM (Con RAM Interna)
#include <WiFi.h>
#include <WebServer.h>

const char* SSID = "Wokwi-GUEST";
const char* PASS = "";
WebServer server(80); 

// ── Constantes del modelo ─
#define MODEL_INPUT_W    96   
#define MODEL_INPUT_H    96   
#define MODEL_INPUT_C     3   
#define TENSOR_ARENA_SZ  (96*1024)  

#define NUM_CLASSES       5   

// ── Etiquetas de clases ──
const char* CLASES[NUM_CLASSES] = {   
   "persona", "automovil", "perro", "gato", "silla" 
}; 

// ── Simulación de inferencia ────────
struct ResultadoInferencia {   
   int    claseIdx;   
   float  confianza;   
   float  puntuaciones[NUM_CLASSES];   
   long   tiempoMs; 
}; 

ResultadoInferencia ejecutarInferencia(uint8_t* imagen) {   
   // SIMULACIÓN para Velxio (resultados aleatorios realistas)   
   ResultadoInferencia r;   
   long t0 = millis();   
   
   // Simular softmax: distribución de probabilidad realista   
   float total = 0;   
   for(int i=0;i<NUM_CLASSES;i++) {     
     r.puntuaciones[i] = (float)random(1,100) / 100.0f;     
     total += r.puntuaciones[i];   
   }   
   
   // Normalizar a suma = 1.0   
   r.claseIdx = 0; r.confianza = 0;   
   for(int i=0;i<NUM_CLASSES;i++) {     
     r.puntuaciones[i] /= total;     
     if(r.puntuaciones[i] > r.confianza) {       
       r.confianza = r.puntuaciones[i];       
       r.claseIdx  = i;     
     }   
   }   
   
   // Simular tiempo de inferencia ESP32-CAM: 100-500ms   
   delay(random(100,500));   
   r.tiempoMs = millis() - t0;   
   return r; 
} 

void setup() {   
   Serial.begin(115200);   
   WiFi.begin(SSID,PASS);   
   while(WiFi.status()!=WL_CONNECTED) delay(500);   
   
   Serial.println("\n=== TFLite Micro — Diagnóstico ===" );   
   Serial.printf("PSRAM total:  %d KB\n", ESP.getPsramSize()/1024);   
   Serial.printf("PSRAM libre:  %d KB\n", ESP.getFreePsram()/1024);   
   Serial.printf("Tensor arena: %d KB (configurado)\n", TENSOR_ARENA_SZ/1024);   
   Serial.printf("Input shape:  %dx%dx%d = %d bytes\n",     
     MODEL_INPUT_W, MODEL_INPUT_H, MODEL_INPUT_C,     
     MODEL_INPUT_W*MODEL_INPUT_H*MODEL_INPUT_C);   
   Serial.printf("Clases:       %d (%s...%s)\n\n",     
     NUM_CLASSES, CLASES[0], CLASES[NUM_CLASSES-1]);   
     
   server.on("/inferir", [](){     
     Serial.println("🧠 Ejecutando inferencia...");     
     
     // TRUCO NINJA: Usamos malloc normal para que no de error en el simulador
     uint8_t* imagen = (uint8_t*)malloc(MODEL_INPUT_W*MODEL_INPUT_H*3);     
     if(imagen) {
         for(int i=0;i<MODEL_INPUT_W*MODEL_INPUT_H*3;i++) imagen[i]=random(256);     
         ResultadoInferencia r = ejecutarInferencia(imagen);     
         free(imagen);     
         
         String j = String("{\"clase\":\"")+CLASES[r.claseIdx]+"\","       
           + "\"confianza\":" + r.confianza + ","       
           + "\"tiempo_ms\":" + r.tiempoMs + ","       
           + "\"clases\":{" ;     
         for(int i=0;i<NUM_CLASSES;i++){       
        

More projects by amirandalo