The following sketch is an optimized sketch that is meant to used with an ESP32 CAM board in order to publish images from the built-in camera to a configurable MQTT broker. The usage is very simple and only requires the sketch to be configured by setting variables within the configuration section of the sketch.
First check out the current source of the sketch via Subversion:
svn co https://svn.grimore.org/arduino-sketches/esp32CamMqttStream
because it contains multiple files that must be available when opening up the project in Arduino.
After the project has been checked out with Subversion, open up esp32CamMqttStream.ino
with Arduino and configure the sketch by setting the parameters in the configuration section.
Finally, upload the sketch and subscribe to the MQTT broker on the configured MQTT topics in order to receive data from the ESP32 CAM.
The board should be set to:
and the partition scheme should be set to "Huge App 3MB / No OTA / 1MB SPIFFS".
The sketch uses MQTT_TOPIC
in order to send status updates and for control whilst the MQTT_TOPIC_STREAM
topic is only used by the sketch to publish image data in binary format to the MQTT broker.
The ESP32 CAM has a powerful LED that is used as a camera flash and by delivering a message to the MQTT_TOPIC
, the sketch will pick up the message and change the state of the ESP32 CAM flash.
For example, the following JSON payload can be used to set the ESP32 CAM flash on:
{ "action": "set" "flash" : { "state": "on" } }
and the flash can be turned off by changing the value of the state
key to off
.
The sketch leverages ESP32 tasks and a semaphore used as a mutex to ensure coherent concurrency. The function TakeImage
acts as a producer that snaps an image from the camera and pushes it onto a buffer, whilst the SendImage
acts as a consumer that publishes the image buffer to the MQTT broker. Given that Arduino sketches typically run on core 1
, both TakeImage
and SendImage
functions are scheduled and pinned to core 0
in order to not interfere with the background work of the sketch. In doing so sending messages to the control topic MQTT_TOPIC
and processing them does not interfere with streaming image data to the MQTT broker via the streaming topic MQTT_TOPIC_STREAM
. At the same time, the rest of the sketch that ensures WiFi connectivity is scheduled on the default sketch core 1
without interfering with the process of capturing images and publishing them that takes place on core 0
where both TakeImage
and SendImage
are scheduled.
Unable to display file "http://svn.grimore.org/arduino-sketches/esp32CamMqttStream/esp32CamMqttStream.ino": It may not exist, or permission may be denied.