On-Device Face Detection: Integrating ESP32-CAM (OV2640) with ASI Biont AI Agent

Introduction

Edge AI is moving from the cloud to the device. The ESP32-CAM, built around the OV2640 2MP sensor and an Espressif ESP32 dual-core microcontroller, can run face detection locally for less than $10. But detecting a face is only the first step. The real power comes when that detection event is delivered to an AI agent that decides what to do next.

ASI Biont is an AI agent that connects to any device through a simple chat dialog — no control panels, no "add device" buttons. In this article, we'll show you how to integrate an ESP32-CAM face detection system with ASI Biont using MQTT, and how the AI agent writes the entire integration script for you.

Why this combination?

A bare ESP32-CAM can identify a face, but it cannot reason about the context. Should the door unlock? Should a Slack message be sent? Should a visitor be logged? ASI Biont adds the reasoning layer. It receives a lightweight JSON payload from the camera, processes it, and triggers multi-step workflows — all generated automatically from a natural language command.

Choosing the connection protocol

The table below summarizes the protocols ASI Biont supports:

Protocol Python library Use case
MQTT paho-mqtt Wireless sensors, ESP32, low bandwidth
HTTP API / WebSocket aiohttp REST and streaming endpoints
COM port (Serial) Hardware Bridge (bridge.py) RS-232/RS-485 and legacy controllers
Modbus/TCP pymodbus Industrial PLCs
SSH paramiko Remote Linux systems
execute_python sandboxed subprocess Any device, fully custom code

For ESP32-CAM, MQTT is the most natural choice. The camera is Wi-Fi enabled and MQTT is designed for low-power, unreliable networks. We'll use a local broker at 192.168.1.100:1883.

MicroPython firmware on ESP32-CAM

First, you need to flash the ESP32-CAM with MicroPython. According to the official MicroPython ESP32 port (https://micropython.org/download/ESP32_GENERIC_CAM/), the OV2640 camera can be accessed via the camera module. Here is a minimal script that captures frames and performs a basic face detection:

import network
import ujson
import time
from umqtt.simple import MQTTClient
import camera

camera.init(0, format=camera.JPEG, fb_location=camera.PSRAM)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('YOUR_SSID', 'YOUR_PASSWORD')

while not wlan.isconnected():
    time.sleep(0.5)

client = MQTTClient('esp32cam', '192.168.1.100')
client.connect()

frame = camera.capture()
faces = camera.detect_faces(frame)  # returns count
if faces > 0:
    payload = ujson.dumps({
        'device': 'cam-01',
        'count': faces,
        'ts': time.time()
    })
    client.publish('office/face', payload)

time.sleep(3)

Note: camera.detect_faces() is a simplified placeholder. In a real setup, use Espressif's ESP-Face library (https://docs.espressif.com/projects/esp-face/). For practical deployments, you may also send JPEG snapshots to ASI Biont over HTTP for cloud-based recognition.

ASI Biont side: AI generates the subscriber

Now comes the key part. Instead of writing a Python script yourself, you describe your intent in the ASI Biont chat:

"Connect to MQTT broker at 192.168.1.100, subscribe to office/face, and send a Telegram message with the face count."

ASI Biont generates a script using paho-mqtt and runs it in a sandbox. The generated code looks like this:

import paho.mqtt.client as mqtt
import requests
import json

TOKEN = '123456:ABC-DEF'
CHAT_ID = '987654321'

def on_message(client, userdata, msg):
    data = json.loads(msg.payload.decode())
    text = 'Face detected: ' + str(data['count'])
    requests.post(
        'https://api.telegram.org/bot' + TOKEN + '/sendMessage',
        data={'chat_id': CHAT_ID, 'text': text}
    )

client = mqtt.Client()
client.on_message = on_message
client.connect('192.168.1.100', 1883)
client.subscribe('office/face')
# ASI Biont keeps this process alive as a background service
client.loop_forever()

Note: for one-off execute_python tasks (30s timeout), ASI Biont uses the script without the infinite loop. For continuous monitoring, the AI wraps it into a background daemon.

Real-world scenario: Access control

Consider a small office with two meeting rooms. The CAM-01 is mounted outside each door. When someone approaches, the camera sends a JSON event with a face count. ASI Biont maintains a simple allowlist in employees.csv. The AI-generated script checks the face ID (or uses the count as a trigger) and sends an MQTT command to an ESP32 relay board to unlock the door.

You could add a second camera for the second room — just describe it in chat and ASI Biont automatically duplicates the workflow with the new topic.

Alternative: wired serial using Hardware Bridge

If your ESP32-CAM is connected via a UART-USB adapter, you can bypass MQTT entirely. Download bridge.py from the ASI Biont dashboard (note: the bridge is not hosted on GitHub, only on the dashboard) and run:

python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 115200 --rate=10

Then in chat you can say: "Read face detection messages from COM3 and log them to a file." ASI Biont generates a script that uses industrial_command(protocol='serial', command='read', ...) to communicate with the bridge.

Why ASI Biont integration is a game-changer

Traditional integration involves writing shell scripts, installing dependencies, and debugging protocol parsers. With ASI Biont, the AI agent does all of that in seconds. It selects the right library, writes the code, sandboxes it, and runs it — all from a chat message.

Many developers find that cloud middleware takes more time than the firmware itself. AI-generated integration shortens this from days to minutes.

Conclusion

Integrating an OV2640 + ESP32 face detection camera with ASI Biont is not just about making a device talk to the internet. It is about giving the edge a brain. By combining on-device ML with AI-generated glue code, you can build security, retail, or home automation systems that are both reactive and intelligent.

Try it now: visit asibiont.com, describe your ESP32-CAM and let ASI Biont do the rest. The integration takes less time than brewing a cup of coffee.

← All posts

Comments