Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max Message size #501

Open
trlafleur opened this issue Oct 9, 2018 · 14 comments
Open

Max Message size #501

trlafleur opened this issue Oct 9, 2018 · 14 comments

Comments

@trlafleur
Copy link

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

@knolleary
Copy link
Owner

Hi - changing the settings via PubSubClient.h is the only supported way of changing the value.

@sprior
Copy link

sprior commented Nov 24, 2018

@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?

@lurkie
Copy link

lurkie commented Dec 13, 2018

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.

@jarvelov
Copy link

The build flag that @lurkie provided worked perfectly! I'd recommend putting that in the README.

@RobertoDebarba
Copy link

RobertoDebarba commented Jan 12, 2019

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.

@knolleary
Copy link
Owner

knolleary commented Jan 12, 2019

@RobertoDebarba using what ide/build tool?

@RobertoDebarba
Copy link

@knolleary Arduino

@yoiang
Copy link

yoiang commented Jan 23, 2019

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?

@IacopoOrtis
Copy link

IacopoOrtis commented Feb 2, 2021

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);

@Ayan4044
Copy link

Ayan4044 commented Aug 1, 2022

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

@vks007
Copy link

vks007 commented Dec 21, 2022

This is fixed via the PubSubClient.setBufferSize(size) API now. @knolleary , please close this issue.

@Hrishie89
Copy link

How much is the maximum size we can keep?

@kgmuzungu
Copy link

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.

@jtowe1
Copy link

jtowe1 commented Jul 12, 2024

This worked for me, thanks!

client.setBufferSize(512);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests