Pubsubclient: MQTT client.publish, How do I publish a dynamic string? Stopped working after recompile!

Created on 26 Mar 2018  路  4Comments  路  Source: knolleary/pubsubclient

I cannot publish any strings in variables anymore!
All worked fine for a year or so. Then I recompiled my sketch and it stopped working.
I can do client.publish("topic","Sometext"),
but not
char msg[] = "Sometext";
client.publish("topic",msg);

I see the definition of publish in the API documentation says you have to use const char[] As I am not a C programmer I don't really understand the fine meaning of that definition. But if I define the variable then it will give a compile error when I try to change that "variable" because it is readonly.

Most helpful comment

incomming = .....

char charBuf[incoming.length() + 1];
incoming.toCharArray(charBuf,incoming.length() + 1);
client.publish(mqtt_topic_pub, charBuf);

Try it ^^

All 4 comments

I found out that the MQTT_MAX_PACKET_SIZE variable was set too low. This has to be done in the PubSubClient.h file of the library, it cannot be redefinied in the Sketch!
See also https://gist.github.com/igrr/7f7e7973366fc01d6393 for the precise commands to publish a String and here you will find the statement of the builder of the lib what to do.

I tried char[10] = "abc";
it's ok , but not dynamic

incomming = .....

char charBuf[incoming.length() + 1];
incoming.toCharArray(charBuf,incoming.length() + 1);
client.publish(mqtt_topic_pub, charBuf);

Try it ^^

It works. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

msnederland picture msnederland  路  9Comments

romuye123 picture romuye123  路  5Comments

himanshu-hearthacker picture himanshu-hearthacker  路  8Comments

mhmayyan picture mhmayyan  路  9Comments

bedomarci picture bedomarci  路  8Comments