knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

Publish() to take a Print implementation? #103

Open karlp opened 8 years ago

karlp commented 8 years ago

I'd like to be able to pass in a Print implementation to publish, to avoid having to serialize json into a char* just to hand it to the pubsubclient, if at all possible, eg

https://github.com/bblanchon/ArduinoJson/wiki/Encoding-JSON#send-to-a-print-implementation

skorokithakis commented 8 years ago

+1, or adding a String-type argument. char[] is very cumbersome to deal with :(

knolleary commented 8 years ago

Well, normally you pass a Print implementation to the object that wants to do the printing - as shown in the JSON example. But there is nothing the print api to indicate the printing has completed, so the library would not know when the packet was ready to be sent. The alternative, to stream the writes to the network, is notsuitable as we need to know the complete length that is about to be written so we can construct the MQTT packet header properly. The JSON library you link to provides a a bespoke way of finding out the length, but there is not a generic solution.

Do you have a suggestion for what the publish api would look like to accommodate these things?

skorokithakis commented 8 years ago

@knolleary Would just adding String arguments be more feasible?

knolleary commented 8 years ago

It might be, but I don't want to derail the original issue before we've explored it properly.

skorokithakis commented 8 years ago

I'll open another issue, thanks.