- Thu Jan 14, 2016 3:55 am
#38749
eduperez wrote:Looks like that library is not ready to accept binary data, as it always uses NULL characters for termination. Perhaps you can reroute your project around that limitation: for example, you could use Base64 or some other algorithm to encode your binary data to ASCII. On the other hand, after a quick glimpse to the source code, it does not seem quite hard to modify that library to accept binary data.
I tried to do just that. Modify the library to accept binary data.
The original code was;
Code: Select allbool MqttClient::publish(String topic, String message, bool retained /* = false*/)
{
int res = mqtt_publish(&broker, topic.c_str(), message.c_str(), retained);
return res > 0;
}
I added a new function to handle binary data.
Code: Select allbool MqttClient::publish_binary(String topic, const char* message, bool retained /* = false*/)
{
int res = mqtt_publish(&broker, topic.c_str(), message, retained);
return res > 0;
} Unfortunately, this new function publish_binary takes in messages that are null-terminated like a normal string. What can be done to have a mqtt function that sends binary data? What is wrong with my code?