- Hardware
- AI Thinker ESP32 CAM
- USB UART Programmer (CP2102 or FTDI)
- Libraries
- PubSubClient@2.7
- Publish to Adafruit.IO
- Using Platform IO
- Using Arduino IDE
- Customizing The Credentials
- Expected Output
- Subscribe to Topic Using Python
-
In the file platformio.ini, before the library dependencies, add a build flag to modify the MQTT Packet Size to the required size. The default packet size defined in PubSubClient Library is 128 Bytes. It's increased to 36000 Bytes inorder to satisfy the requirements.
build_flags = -DMQTT_MAX_PACKET_SIZE=36000 lib_deps = PubSubClient@2.7
For additional information, go to Platform IO build_flags.
-
Install the PubSubClient Library from Tools -> Manage Libraries. We used the 2.7 version of the same. After installing the library go to the directory of Libraries related to Arduino, and navigate to PubSubClient Library.
-
user@user:~/Arduino/libraries/PubSubClient$ pwd /home/user/Arduino/libraries/PubSubClient
-
Navigate to
user/Documents/Arduino/libraries/PubSubClient
Go to PubSubClient.h residing in the src folder, Find the following codeblock.
// MQTT_MAX_PACKET_SIZE : Maximum packet size #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 128 #endif
And modify according to requirements.
// MQTT_MAX_PACKET_SIZE : Maximum packet size #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 36000 #endif
-
In the main.cpp file (If Arduino IDE, ESP32Cam_MQTT.ino),
-
const char *ssid = "WiFi_SSID"; const char *password = "WiFi_Password";
-
const char *mqtt_server = "io.adafruit.com"; const char *mqtt_clientid = "mqtt_clientid"; const char *mqtt_username = "mqtt_username"; const char *mqtt_password = "mqtt_password"; const char *mqtt_publish_topic = "username/feeds/camera";
- Replace mqtt_clientid with a unique random id.
- Replace mqtt_username with the Adafruit Username.
- Replace mqtt_password with the Adafruit AIO Key.
Connecting to Dhanish
.......
WiFi connected
IP address:
192.168.1.5
Attempting MQTT connection...connected
Camera Captured
Buffer Length:
22008
Publishing...Published
Camera Captured
Buffer Length:
21960
Publishing...Published
...
A basic python code to subscribe to the topic username/feeds/camera
and updates realtime.
- Python3
- Paho MQTT Client
- PIL (Python Imaging Library)
- PyGame
-
def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) client.subscribe("username/feeds/camera")
-
client.username_pw_set(username="mqtt_username",password="mqtt_password" client.connect("io.adafruit.com", 1883, 60)
-
python3 test_subscriber.py