pubsubclient
pubsubclient copied to clipboard
Max Message size
In the documentation, it stated:
The maximum message size, including header, is 128 bytes by default. This is configurable via MQTT_MAX_PACKET_SIZE in PubSubClient.h
Trying to override this in the application on an ESP32 processor by setting the "MQTT_MAX_PACKET_SIZE" to a new value in the application, will allocate the storage needed, but the pubsubclient will never receive a message greater than the value preset in the .h file. ie: It allocated storage, but there appears to be some parameter(s) in the .h file that is not referencing the new allocation correctly.
Changing the setting it in the "PubSubClient.h" works just fine...
ESP32 code load of 2 Oct 2018 Arduino 1.8.5 pubsubclient 2.6.0
Hi - changing the settings via PubSubClient.h is the only supported way of changing the value.
@knolleary With the 2.7.0 changes to allow large messages to be published, did the requirement to modify MQTT_MAX_PACKET_SIZE to receive large messages get changed? Is there something in the works?
With the information above, I got the idea to try using build flags. In VSCode with PlatformIO, I was able to get this to work by configuring a define in a build flag in platformio.ini
, e.g. build_flags = -D MQTT_MAX_PACKET_SIZE=256
. My 140 byte message gets send happily to Mosquitto now :)
This should/could work in Arduino IDE as well, not tested though.
The build flag that @lurkie provided worked perfectly! I'd recommend putting that in the README.
I put the flag after the include and worked for me:
#include <PubSubClient.h>
#define MQTT_MAX_PACKET_SIZE 2048
Putting before didn't work.
On ESP8266.
@RobertoDebarba using what ide/build tool?
@knolleary Arduino
To clarify the recommended course is to modify the macro def in the library? What is the process for using a library manager that isn't aware of changes?
Reading the cpp and .h file you see that the limit is 256 and it could be set in various ways:
- defining MQTT_MAX_PACKET_SIZE
- using setBufferSize function
For me:
This won't work
file.h #define MQTT_MAX_PACKET_SIZE 512 #include <PubSubClient.h>
This will work
file.h #include <PubSubClient.h> extern PubSubClient client;
file.cpp client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); client.setBufferSize(512);
head to PubsubClient/src available under libraries folder
`#ifndef PubSubClient_h #define PubSubClient_h
#include <Arduino.h> #include "IPAddress.h"
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize(). #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 1024 #endif ` increase the packet size value from 256 to the size you need. I had faced the same problem while sending data to MQTT broker running on 8883, the broker was getting broken data
This is fixed via the PubSubClient.setBufferSize(size) API now. @knolleary , please close this issue.
How much is the maximum size we can keep?
It seems that if you use a GSM modem you might be limited in how big a single message is allowed to be. Like on the SIM800L max size seems to be 1450bytes per message. Then you would have to split your payload. MQTT servers have limits per message set. And your microcontrollers memory is limiting you as well.
This worked for me, thanks!
client.setBufferSize(512);