Using the following API for large payload
String msg = " THIS IS A VERY LONG MESSAGE THAT IM TRYING TO SEND BUT DOES NOT WORK BECAUSE IT IS VERY LONG";
pubSubClient.beginPublish(topic.c_str(), msg.length(), false);
byte __msg[sizeof(msg)];
msg.getBytes(__msg, sizeof(__msg));
pubSubClient.write(__msg, sizeof(__msg));
pubSubClient.endPublish();
You still need to change the max packet size in PubSubClient.h if you want to send >128 bytes. Have you done that?
Yes but not directly in PubSubClient.h. I defined it in my code before
including PubSubClient.h which should override its value.
On Mon, Nov 5, 2018 at 12:18 AM knolleary notifications@github.com wrote:
You still need to change the max packet size in PubSubClient.h if you want
to send >128 bytes. Have you done that?—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/knolleary/pubsubclient/issues/517#issuecomment-435776398,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADKkVro6wPCGzT1Mhn0W1Wjk_zUrUP_1ks5ur-ZKgaJpZM4YNpxS
.
I defined it in my code before
including PubSubClient.h which should override its value.
No, it wont. The Arduino IDE moves all #includes to the top of the file before compiling... making it impossible for you to #define your own values. You must edit PubSubClient.h.
Still not working.
That still wouldn't work: Defining it before including the header file only overrules the define in the .h for the compile of the .cpp file containing that define. When PubSubClient.cpp is compiled (separately in a subsequent run) the max packet size define is again undefined and as such then defined as 128. I don't get why the #ifndef then #define construction is there in the first place but if the intent was to allow to overrule it; I think it won't ever work.
You'll have to edit .h file.
There are alternative build environments such as https://platformio.org/ that use this library and are able to #define these values from the sketch.
Is my usage, above, for sending long payloads correct?
@amcewen can you take a look?
Sorry I hadn't had time to look at this till today.
@knolleary with the large payload API you don't need to increase the max packet size buffer as it isn't used (for sending, there isn't a large payload receiving API yet).
@haimiko That is how you use the API, but there's a tiny (yet crucial) bug in your code. The __msg buffer that you create only ends up being 12 bytes in size, because sizeof(msg) will return the size of the String object rather than the size of the message it contains.
There's also a problem with getBytes, because that uses the last byte in the buffer you give it to add a \0-terminator, so you need to have an extra byte in the buffer or you'll lose the last byte of your message.
So, changing the declaration of __msg as I have here works:
String msg = " THIS IS A VERY LONG MESSAGE THAT IM TRYING TO SEND BUT DOES NOT WORK BECAUSE IT IS VERY LONG";
pubSubClient.beginPublish(topic.c_str(), msg.length(), false);
byte __msg[msg.length()+1];
msg.getBytes(__msg, sizeof(__msg));
pubSubClient.write(__msg, sizeof(__msg));
pubSubClient.endPublish();
You don't actually need the separate buffer to copy things into, as the original String object msg isn't going to be changed during the calls to write, so you can also reduce your code to:
String msg = " THIS IS A VERY LONG MESSAGE THAT IM TRYING TO SEND BUT DOES NOT WORK BECAUSE IT IS VERY LONG";
pubSubClient.beginPublish(topic.c_str(), msg.length(), false);
pubSubClient.write((const uint8_t*)msg.c_str(), msg.length());
pubSubClient.endPublish();
That did it! Thank you @amcewen !
@amcewen thanks for this 3-line example! Given the mqtt_large_message.ino example I figured one needed to write in small chunks, but: no. 👍