iotProject — Esp32 Cam simulation

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

Components

Sketch code

#include "esp_camera.h"
#include <WiFi.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>

const char* ssid = "Velxio-GUEST";

// --- Azure VM Configuration ---
const char* serverIP = "98.70.59.164";
const int serverPort = 8000;
const char* apiEndpoint = "/api/v1/process_vehicle";

// --- Hardware Pins ---
const int S1_PIN = 13;
const int S2_PIN = 14;
const int S3_PIN = 15;
const int SERVO_PIN = 2;

Servo gateServo;

// State tracking so we don't spam the API while a car sits on the sensors
bool vehiclePresent = false; 

void sendPhotoToServer();

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

    // Initialize Pins
    pinMode(S1_PIN, INPUT);
    pinMode(S2_PIN, INPUT);
    pinMode(S3_PIN, INPUT);

    // Initialize Servo (Starts closed at 0 degrees)
    gateServo.setPeriodHertz(50);
    gateServo.attach(SERVO_PIN, 500, 2400);
    gateServo.write(0); 

    WiFi.begin(ssid, "", 6);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nWiFi Connected!");

    // --- Camera Configuration ---
    camera_config_t cfg;
    cfg.ledc_channel = LEDC_CHANNEL_0;
    cfg.ledc_timer   = LEDC_TIMER_0;
    cfg.pin_d0 = 5;
    cfg.pin_d1 = 18;
    cfg.pin_d2 = 19;
    cfg.pin_d3 = 21;
    cfg.pin_d4 = 36;
    cfg.pin_d5 = 39;
    cfg.pin_d6 = 34;
    cfg.pin_d7 = 35;
    cfg.pin_xclk = 0;
    cfg.pin_pclk = 22;
    cfg.pin_vsync = 25;
    cfg.pin_href = 23;
    cfg.pin_sccb_sda = 26;
    cfg.pin_sccb_scl = 27;
    cfg.pin_pwdn  = 32;
    cfg.pin_reset = -1;
    cfg.xclk_freq_hz = 20000000;
    cfg.pixel_format = PIXFORMAT_JPEG;
    cfg.frame_size   = FRAMESIZE_SVGA;
    cfg.jpeg_quality = 12;
    cfg.fb_count     = 1;
    cfg.fb_location  = CAMERA_FB_IN_DRAM;
    cfg.grab_mode    = CAMERA_GRAB_WHEN_EMPTY;

    esp_err_t err = esp_camera_init(&cfg);
    if (err != ESP_OK) {
        Serial.println("Camera Init Failed");
        return;
    }
}

void loop() {
    // Read the three sensors
    bool s1 = digitalRead(S1_PIN) == HIGH;
    bool s2 = digitalRead(S2_PIN) == HIGH;
    bool s3 = digitalRead(S3_PIN) == HIGH;

    // The Detection Logic: (S1 AND S2) OR (S2 AND S3)
    bool triggerCondition = (s1 && s2) || (s2 && s3);

    if (triggerCondition && !vehiclePresent) {
        // A new vehicle just hit the sensors
        vehiclePresent = true;
        Serial.println("\n[INFO] Vehicle detected by sensors! Snapping photo...");
        sendPhotoToServer();
        
    } else if (!triggerCondition && vehiclePresent) {
        // Vehicle has fully cleared all sensors, reset the system
        vehiclePresent = false;
        Serial.println("[INFO] Vehicle cleared the gate area. System reset.");
    }

    delay(100); // Small delay to prevent CPU hogging
}

void sendPhotoToServer() {
    camera_fb_t* fb = esp_camera_fb_get();
    
    // Quick sensor setting fix for SVGA stability
    sensor_t *s = esp_camera_sensor_get();
    s->set_framesize(s, FRAMESIZE_SVGA);

    if (!fb) {
        Ser