Thursday, May 30, 2019

Basic ESP32-CAM sketch

Some time back, I got myself some A.I. Thinker ESP32-CAM camera modules. Basically, these are camera modules controlled by an ESP32 microcontroller.

These modules actually work with the example found in the Arduino-ESP32 core. Which makes them very easy to program. But the example comes with a web interface for configuring the camera, such as resolution, white balance, etc. This web interface takes up quite a bit of flash memory space, actually. In fact, the example needs to be flashed onto the ESP32 using the "Huge app" partition setting in Arduino IDE for the ESP32.

The example also has another problem. It doesn't allow the ESP32-CAM to be used as a network camera on MotionEye. I tried; MotionEye refuses to recognize the module as a valid network camera. I think it is likely the web interface getting in the way.

Searching around, there is this basic sketch which removes the web interface. Wi-Fi credentials need to be put into a separate header file, which is included into the main sketch. And the sketch automatically sets the resolution to UXGA if PSRAM is found, and SVGA if not. The sketch is also small enough to fit into normal partitioning of the ESP32.

An improved version of the sketch can be found here. Basically, the changes are:
1. Wi-Fi credentials moved into the main sketch, just edit them directly without having to come up with additional header file (this may not be a good idea if you are sharing your sketches, but it is convenient otherwise).
2. mDNS is added to allow the camera module to be accessed by hostname; there is no need to hunt around for the IP address. For example,
http://esp32cam.local/
gives access to the video being streamed by the camera module if the hostname is set to esp32cam.
3. Resolution with PSRAM is set to SVGA (800x600) because that seems to be the best balance between resolution and FPS.

Anyway, MotionEye recognizes camera modules flashed with this sketch as a network camera. So when adding a camera, just select "Camera Type" as "Network Camera" and then enter "http://esp32cam.local/" (of whatever you change the hostname to) for the URL. Hook the module up to a small USB power supply, and put them in a corner of the house, and you have your own cheap surveillance camera.

Hurray!! 🎉🎊

No comments: