Capture still images from a USB camera using ESP-IDF
MIT License
Capture still images from a USB camera using ESP-IDF.
This example demonstrates how to:
This example enumerates the attached camera descriptors, negotiates the selected resolution and FPS, and starts capturing video.
I based it on this official example.
This example requires any ESP32-S2 or ESP32-S3 with external PSRAM and exposed USB connector attached to USB camera. ESP module without external PSRAM will fail to initialize. I used this board.
USB cameras consume a lot of electricity. If the power supplied to the USB port is insufficient, the camera will not operate and ESP32 resets.
Breakout boards are available from AliExpress or eBay.
Here is a list of USB cameras that support UVC that work with FreeBSD. From what I've researched, ESP-IDF has limited USB support. For example, the Logitech C615 works with Linux, but not with ESP-IDF. I tested with these cameras.
It is very hard to find a camera that works with ESP-IDF. On this document says that these cameras work with ESP-IDF.
When usb support provided by ESP-IDF is updated, this issue may eliminate the problem. Detail is here.
ESP-IDF V5.0 or later. ESP-IDF V4.4 release branch reached EOL in July 2024.
ESP BOARD USB CONNECTOR (type A)
+--+
5V -------------> | || VCC
[GPIO19] -------------> | || D-
[GPIO20] -------------> | || D+
GND -------------> | || GND
+--+
git clone https://github.com/nopnop2002/esp-idf-video-snapshot
cd esp-idf-video-snapshot
idf.py set-target {esp32s2/esp32s3}
idf.py menuconfig
idf.py flash monitor
You can connect using the mDNS hostname instead of the IP address.
You can use static IP.
You can choose one of the following as trigger.
Trigger is a GPIO toggle Some GPIOs are used as boot straps and cannot be used as inputs.
Initial Sate is PULLDOWN The trigger is prepared when it is turned from OFF to ON, and a picture is taken when it is turned from ON to OFF.
Initial Sate is PULLUP The trigger is prepared when it is turned from ON to OFF, and a picture is taken when it is turned from OFF to ON.
python3 ./tcp_send.py
python3 -m pip install -U netifaces
python3 ./udp_send.py
mosquitto_pub -h your_broker -p 1883 -t "/take/picture" -m ""
curl "http://esp32-camera.local:8080/take/picture"
Filenames can be timestamped.
Some cameras need to change frame size and frame format. See here.
Build and flash firmware to esp32.
Connect the USB camera at this timing.
For available USB cameras, device information will be displayed and video streaming will begin.
For unavailable USB cameras, you will see an error like this.
Execute trigger.
You can view the pictures taken using the built-in WEB server. Enter the ESP32's IP address and port number in the address bar of your browser. You can use mDNS instead of IP address.