Pubsubclient: Max Message size

Created on 10 Oct 2018  路  8Comments  路  Source: knolleary/pubsubclient

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

Most helpful comment

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.

All 8 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

warriorfenixSM picture warriorfenixSM  路  6Comments

NickWaterton picture NickWaterton  路  6Comments

HobbytronicsPK picture HobbytronicsPK  路  4Comments

zvone141 picture zvone141  路  3Comments

kbialkowski picture kbialkowski  路  7Comments