Sending commands with larger payloads

If you’re using a Losant Builder Kit or the losant-sdk-arduino on another board, you’ll likely hit the default mqtt packet size limit defined in the underlying pubsubclient library. Unfortunately the packet is blocked before reaching any of your code, so it’s hard to debug. It simply looks like the command was never received. For example:

{ "foo" : "bar" } works, whereas { "somethingLarger" : "with a longer value" } doesn’t work. This is because the default packet size is 128, which provides enough room for the command meta info and a small payload. Once you increase the payload, the packets will start being dropped. Fortunately this is easy to fix:

  1. Open the pubsubclient.h file from the Arduino libraries folder. On a Mac, this will be located at ~/Documents/Arduino/libraries/pubsubclient/src/PubSubClient.h
  2. Edit the MQTT_MAX_PACKET_SIZE to something larger. Increasing it to 256 seems to work pretty well.
  3. Recompile and reupload the firmware to the device.

If you plan on sending even larger payloads, you can increase the value as needed. Just remember many boards don’t have a ton of memory, and the entire payload will have to fit.

5 Likes

Works perfectly - I compiled the example code for an ESP8266 and found the maximum command size was 18 characters. I made this change and everything seems to work as expected.

1 Like

This post was very helpful - however, as a newbie I am at a loss about what to do with
“Recompile and reupload the firmware to the device.”

Any hint on where to start looking how to do this on a Mac? :slight_smile:

Thanks!

OK, I found out. Get the PubSubClient separately from GitHub, then unzip, change the value and rezip. Install this library from the Arduino software (Sketch/Include Library/Add .ZIP Library)

1 Like

This is what i could find and looks like the default size is set to 256 already…
still not working

/*

PubSubClient.h - A simple client for MQTT.

Nick O’Leary

*/

#ifndef PubSubClient_h

#define PubSubClient_h

#include <Arduino.h>

#include “IPAddress.h”

#include “Client.h”

#include “Stream.h”

#define MQTT_VERSION_3_1 3

#define MQTT_VERSION_3_1_1 4

// MQTT_VERSION : Pick the version

//#define MQTT_VERSION MQTT_VERSION_3_1

#ifndef MQTT_VERSION

#define MQTT_VERSION MQTT_VERSION_3_1_1

#endif

// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().

#ifndef MQTT_MAX_PACKET_SIZE

#define MQTT_MAX_PACKET_SIZE 256

#endif

// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()

#ifndef MQTT_KEEPALIVE

#define MQTT_KEEPALIVE 15

#endif

// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()

#ifndef MQTT_SOCKET_TIMEOUT

#define MQTT_SOCKET_TIMEOUT 15

#endif