MQTT

MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth.

To restart the ESP32-CAM, an arduino device remotely without taking up a lot of resources, PubSubClient based on MQTT is likely the better choice. It is simpler, more efficient in terms of resource usage, and sufficient for the task of listening to an MQTT topic and triggering a restart command. CameraSend sketch provides a detailed implementation.

Some of the configured mqtt related commands are given below

prabu@homepc2 ~> mosquitto_pub -h 192.168.1.198 -u esp32 -P esp32 -t "esp32/restart" -m "restart"
prabu@homepc2 /d/d/p/e/uploads> mosquitto_pub -h 192.168.1.198 -u esp32 -P esp32 -t "esp32/capture_photo" -m "capture_photo"
prabu@homepc2 ~> mosquitto_sub -h 192.168.1.198 -u esp32 -P esp32 -t "esp32/#"
prabu@homepc2 ~> journalctl -u mosquitto.service -f
prabu@homepc2 /d/d/p/e/uploads> grep ^[^\#] /etc/mosquitto/mosquitto.conf
per_listener_settings true
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd
prabu@homepc2 ~>netstat -tuln | grep 1883

Configuring MQTT

The below section is entirely based on inputs given by chatgpt.

Option 1: Using a Cloud-based MQTT Broker

Using Eclipse Mosquitto on Cloud

Sign Up for CloudMQTT:

CloudMQTT is a popular hosted MQTT broker service. You can sign up for a free plan which allows you to get started with MQTT quickly. Visit CloudMQTT and create an account.

Create an Instance:

Once logged in, create a new MQTT instance. Note down the connection details: Server, Port, Username, and Password.

Connect to CloudMQTT Broker:

Use these details in your ESP32-CAM code to connect to the MQTT broker.


#define MQTT_SERVER "your_server.cloudmqtt.com"
#define MQTT_PORT 12345
#define MQTT_USER "your_username"
#define MQTT_PASSWORD "your_password"

Testing with an MQTT Client:

You can use tools like MQTT.fx or Mosquitto clients to test your connection. Publish messages to the topic and verify that the ESP32-CAM receives them.

Option 2: Setting Up a Local MQTT Broker

Using Eclipse Mosquitto on a Local Machine

Install Mosquitto Broker on Arch Linux:


sudo pacman -S mosquitto

Configure Mosquitto

Configuration files are located in etc/mosquitto on most systems. The default configuration works out of the box, but you can edit /etc/mosquitto/mosquitto.conf for advanced settings.

Remember to add Listener Configuration: Add or modify the listener directive to bind Mosquitto to the IP address of your MQTT server (192.168.1.198) and allow connections from your local network. Otherwise, you’ll receive “Error: Connection refused”

Start the Mosquitto Service:


sudo systemctl start mosquitto
sudo systemctl enable mosquitto

Testing the Mosquitto Broker:

Open two terminal windows. In the first, start a subscriber:

mosquitto_sub -h localhost -t test/topic

In the second, publish a message:


mosquitto_pub -h localhost -t test/topic -m "Hello MQTT"

You should see the message “Hello MQTT” in the subscriber terminal.

Connect Your ESP32-CAM to the Local Broker:


#define MQTT_SERVER "192.168.1.x"  // Replace with the local IP of your broker
#define MQTT_PORT 1883
#define MQTT_USER "your_username"  // If you configured authentication
#define MQTT_PASSWORD "your_password"  // If you configured authentication

Customizing MQTT

Steps to create a similar MQTT command to trigger the capturePhoto() function in your ESP32 sketch. Here’s how you can modify your sketch to include this functionality: Step-by-Step Implementation

Define MQTT Topic for Photo Capture Command:

Define an MQTT topic where you will publish the command to capture a photo. For consistency, let’s use “esp32/capture_photo”.

Subscribe to the MQTT Topic:

Modify your callback function to handle messages on “esp32/capture_photo” and trigger the capturePhoto() function when a message is received.

Update MQTT Client Setup:

Ensure your MQTT client is subscribed to “esp32/capture_photo” and ready to receive commands.

Note: Using MQTT for lightweight messaging (like sensor data) is typically fine, but for handling large image or video data, HTTP or other protocols designed for such purposes are more suitable.


© Prabu Anand K 2020-2026