Turn your M5CAM into a webcam exposing RTSP stream

The ESP32 is a huge hit amongst makers. It’s both powerful and versatile and therefore a good starting point for many IoT projects and POCs.

Several spinoffs are offered where the ESP32 is combined with eg. extra connectivity (LoRa) or cameras.

These ESP32 boards with a camera are known as ESP32CAM but there are many types.

One of them is the M5CAM where an OV2640 Camera Module is mounted on the ESP32 development board:

In this blog, we look at how to turn this M5CAM device into a webcam supporting the RTSP protocol.

If you look around on the internet, you will find several libraries and project which do the same.

Acknowledgment: This blog is based on https://github.com/bnbe-club/rtsp-video-streamer-diy-14 which is originally written for few types of ESP32CAMs. We extended it for the M5CAM.

Clone our project from the following GitHub project and open the INO file in your Arduino IDE:

git clone https://github.com/sandervandevelde/rtsp-video-streamer-diy-14.git

Each type of an ESP32CAM has its own pin layout. Our M5CAM was not supported in the original project so together with my friend Hans, we experimented with the library to find the right pin layout for our ESP32.

After checking some possible combinations taken from here and here and here, we found the right combination:

#elif defined(CAMERA_MODEL_M5CAM)
#define PWDN_GPIO_NUM     -1
#define RESET_GPIO_NUM    15
#define XCLK_GPIO_NUM     27
#define SIOD_GPIO_NUM     25
#define SIOC_GPIO_NUM     23

#define Y9_GPIO_NUM       19
#define Y8_GPIO_NUM       36
#define Y7_GPIO_NUM       18
#define Y6_GPIO_NUM       39
#define Y5_GPIO_NUM       5
#define Y4_GPIO_NUM       34
#define Y3_GPIO_NUM       35
#define Y2_GPIO_NUM       17
#define VSYNC_GPIO_NUM    22
#define HREF_GPIO_NUM     26
#define PCLK_GPIO_NUM     21

Note: our fork of the original library is found on my GitHub. The layout is alreadt added.

We aslo enabled this particular ESP32CAM to be used when deployed to our M5CAM:

#define CAMERA_MODEL_M5CAM

So now, you only thing you have to do is to enter the SSID and password of your own local WIFI network in the ‘wifikey.h’ file:

const char *ssid =  "<SSID>";         // Put your SSID here
const char *password = "<PASSWORD>";  // Put your PASSWORD here

Run the sketch and once it is compiled and deployed on the ESP, the console should tell you the board is reset.

Of course, your Arduino IDE has to be enabled for the ESP32, first. If this is not the case, please follow these installation instructions.

Note: In windows, make sure the related GitHub stuff actually lands in C:\[your user name]\sande\Documents\Arduino\hardware\espressif\esp32)

Now, check out the Serial Monitor in the Arduino IDE.

You can either open it using the Tools menu or click this button:

There, the dynamic IP address of your M5CAM is shown as an RTSP URI.

Note: If you see a lot of gibberish, you probably have to alter the baud rate to 115200 (as seen in the code). Just reset the board and see the right text being shown.

Your webcam is running already, so how to access the video?

Opening the stream in a web browser

First, we are going to use our favorite browser to see the video.

Take the IP address of your ESP32 and fill it in into the browser:

192.168.1.54

This will give this result:

So we have a live stream running in the browser. The delay is just a fraction of a second so the ESP32 is quite efficient.

I did some simple tests regarding the distance and the quality of the signal. My M5CAM has an onboard antenna and I was still able to stream, without hickups, over a distance like 10+ meters while having the camera outside my house.

I also did some simple tests regarding energy consumption. I found out it can run at least 4-5 days on a 20100 mAh power bank.

Real-Time Streaming Protocol

The same sketch also supports the RTSP protocol. This is ideal for video recording solutions as found on a regular (Synology) NAS.

As seen in the serial monitor, The stream is of type RTSP/MJPEG:

rtsp://192.168.1.54:8554/mjpeg/1

Here, I use the well-known VLC media player which is also capable to display RTSP streams:

VLC is just showing the stream. The quality of the camera image is fair during the day.

Note: If you want to start and stop recording depending on something moving in front of the camera, you need extra software.

Note: At first the stream was not showing in the VLC media player. It turned out, my browser was still running the stream. So I closed the browser and the RTSP stream was finally shown in VLC.

Conclusion

Thanks to the heavy lifting of Bits N Blobs Electronics and some troubleshooting to find the right pin layout, we are now able to turn our M5CAM ESP32 into a webcam exposing an RTSP stream.