ESP32-CAM Face Detection with ASI Biont: Build an Edge AI Security System in Minutes

ESP32-CAM Face Detection with ASI Biont: Build an Edge AI Security System in Minutes

Imagine a cheap ESP32-CAM module with an OV2640 camera sitting in your office, running on-device face detection — and an AI agent that automatically alerts you via Telegram when it sees a known or unknown face, records video clips, and even controls a door lock. That's the kind of setup you can build today with ASI Biont, without writing complex backend code.

This guide walks you through the real integration: how to connect an ESP32-CAM running face detection (using the esp32-camera library and the built-in face detection module) to the ASI Biont AI agent, and automate security workflows. You'll see actual code, wiring tips, and the exact chat commands to make it work.


Why Connect a Camera to an AI Agent?

A standalone ESP32-CAM can detect faces, but it can't send alerts, store history, or make decisions based on context. By connecting it to ASI Biont, you get:

  • Instant notifications — The AI sends you a Telegram or email when a face is detected.
  • Automated actions — The AI can trigger a relay (door unlock), save snapshots, or start a video stream.
  • Low-code integration — You just describe what you want in the chat; the AI writes the Python code and runs it.
  • Edge AI benefits — Face detection runs locally on the ESP32 (no cloud dependency), while the AI agent handles logic and communication.

How Does ASI Biont Connect to the ESP32-CAM?

There are two practical ways to connect an ESP32-CAM to ASI Biont:

  1. MQTT (recommended for most cases) — The ESP32 publishes face detection events to an MQTT broker (e.g., Mosquitto on a Raspberry Pi or a cloud broker). ASI Biont's AI agent subscribes to that topic using paho-mqtt inside an execute_python script, processes the data, and sends commands back via MQTT.

  2. Hardware Bridge + Serial — If your ESP32 is connected to a PC via USB (e.g., for debugging or serial output), you can run bridge.py on that PC. The AI agent sends serial commands through industrial_command(protocol='serial://', command='serial_write_and_read') to read camera data or trigger actions.

For a wireless security camera, MQTT is cleaner. Below, we focus on the MQTT approach.


Real-World Example: Face Detection Alerts via Telegram

Scenario: An ESP32-CAM with OV2640 detects a face, publishes an MQTT message with the face coordinates and timestamp. The ASI Biont AI agent subscribes to that topic, checks if the face is known (using a simple hash or name from a stored list), and sends a Telegram alert with a snapshot.

Step 1: ESP32-CAM Code (Arduino IDE)

You'll need the esp32-camera library and the built-in face detection (part of the ESP32's esp-face component). Here's a minimal sketch:

#include "esp_camera.h"
#include <WiFi.h>
#include <PubSubClient.h>

// Camera pins for AI-Thinker ESP32-CAM
#define PWDN_GPIO_NUM    -1
#define RESET_GPIO_NUM   -1
#define XCLK_GPIO_NUM    0
#define SIOD_GPIO_NUM    26
#define SIOC_GPIO_NUM    27
#define Y9_GPIO_NUM      35
#define Y8_GPIO_NUM      34
#define Y7_GPIO_NUM      39
#define Y6_GPIO_NUM      36
#define Y5_GPIO_NUM      21
#define Y4_GPIO_NUM      19
#define Y3_GPIO_NUM      18
#define Y2_GPIO_NUM      5
#define VSYNC_GPIO_NUM   25
#define HREF_GPIO_NUM    23
#define PCLK_GPIO_NUM    22

const char* ssid = "your_SSID";
const char* password = "your_PASS";
const char* mqtt_server = "192.168.1.100"; // your broker IP
WiFiClient espClient;
PubSubClient client(espClient);

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

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_QVGA;
  config.jpeg_quality = 12;
  config.fb_count = 1;

  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed: 0x%x", err);
    return;
  }

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  client.setServer(mqtt_server, 1883);
  client.connect("esp32cam");
}

void loop() {
  if (!client.connected()) {
    client.connect("esp32cam");
  }
  client.loop();

  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) return;

  // Run on-device face detection
  dl_matrix3du_t *image_matrix = NULL;
  box_array_t *boxes = face_detect(fb, &image_matrix);

  if (boxes) {
    // Publish MQTT message with face count
    String msg = "{\"faces\":" + String((int)boxes->len) + "}";
    client.publish("esp32cam/face", msg.c_str());

    // Optionally send the image (base64) - for small images only
    // ...

    esp_camera_fb_return(fb);
    free(boxes);
    if (image_matrix) dl_matrix3du_free(image_matrix);
  } else {
    esp_camera_fb_return(fb);
  }
  delay(5000);
}

Pitfall: The face_detect() function uses the ESP32's built-in neural network accelerator (ESP-DL). It works well for frontal faces but may miss side profiles. Use good lighting and keep the camera steady.

Step 2: Connect to ASI Biont via Chat

Once the ESP32 is publishing MQTT messages, you open a chat with the AI agent in the ASI Biont dashboard and describe your goal:

"Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic 'esp32cam/face'. When a face is detected, send me a Telegram alert with the number of faces and a snapshot. Also log the event to a CSV file."

The AI agent will generate and run this Python script using execute_python:

import paho.mqtt.client as mqtt
import json
import requests
import csv
from datetime import datetime

TELEGRAM_BOT_TOKEN = "your_bot_token"
TELEGRAM_CHAT_ID = "your_chat_id"

def on_message(client, userdata, msg):
    payload = json.loads(msg.payload)
    faces = payload.get("faces", 0)
    if faces > 0:
        alert = f"🚨 Face detected! Count: {faces} at {datetime.now()}"
        # Send Telegram
        url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
        requests.post(url, json={"chat_id": TELEGRAM_CHAT_ID, "text": alert})
        # Log to CSV
        with open("face_log.csv", "a") as f:
            writer = csv.writer(f)
            writer.writerow([datetime.now(), faces])
        print(alert)

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("esp32cam/face")
client.loop_forever()  # Note: runs in sandbox with 30s timeout; use loop_start() for production

Important: The sandbox has a 30-second timeout. For a persistent subscription, you'd run the script on your own server. But for demos and quick tests, this works within the limit.


Automation Scenarios You Can Build

Scenario How It Works AI Agent Role
Telegram Alert on Face ESP32 publishes face count; AI sends Telegram message Subscribe to MQTT, call Telegram API
Door Unlock for Known Faces ESP32 sends face ID (e.g., from a local database); AI checks whitelist and sends MQTT command to relay Compare face ID, publish to esp32cam/relay
Video Recording on Detection AI triggers another script to save camera stream locally Execute Python script with OpenCV on Raspberry Pi via SSH
Daily Face Detection Report AI aggregates events over 24h and emails you a summary Schedule cron-like check via time.sleep in a loop

Why This Approach Beats Traditional CCTV

  • Cost: ESP32-CAM costs under $10. No cloud subscription needed.
  • Privacy: Face detection runs on the device, not in the cloud.
  • Flexibility: You can add new automations by just chatting with the AI — no firmware update required.
  • Edge AI: ESP32's face detection is fast and low-power ( ~200mA).

Pitfalls to Avoid

  1. MQTT Broker Choice — Use a local broker (Mosquitto on a Raspberry Pi) for low latency. Cloud brokers add delay.
  2. Image Size — Sending full JPEG via MQTT (even base64) can overload the ESP32's memory. Stick to face count or send thumbnails.
  3. Sandbox Timeout — Long-running MQTT listeners in execute_python will be killed after 30 seconds. For production, run the Python script on your own PC or server, and have the AI generate it for you.
  4. Face Detection Accuracy — The built-in detector works best with good lighting and frontal faces. Consider using a Raspberry Pi with OpenCV for complex scenes.

Try It Yourself

  1. Flash the ESP32-CAM code above (adjust MQTT broker IP).
  2. Open a chat with ASI Biont at asibiont.com.
  3. Say: "Set up MQTT listener for face detection on broker 192.168.1.100, topic esp32cam/face, and send alerts to Telegram."

The AI will generate the Python code, run it in the sandbox, and you'll get your first alert within seconds.


Conclusion

Integrating an ESP32-CAM with face detection into ASI Biont turns a simple hobby camera into an intelligent edge security system. Thanks to the AI agent's ability to write and execute integration code on the fly, you can focus on what matters — the automation logic — rather than plumbing. Whether you're monitoring your home, office, or lab, this setup gives you a production-ready security camera with AI-driven alerts, all for under $20.

Try the integration today at asibiont.com — no coding required. Just describe your device and let the AI handle the rest.

← All posts

Comments