It took more than a couple minutes to get some sample code going with ethernet, so I figured it would benefit everyone if there was at least one example out there.
This was already the case for the Adafruit_MQTT_Publish constructor, so
this makes things a bit more consistent when using the publish() method
directly.
Arduino supplies the F() macro that allows easily putting strings in
PROGMEM and marking them with a custom type. Essentially,
a __FlashStringHelper* is just the same char*, but cast to a different
type, which allows for example Serial.print() to automatically handle
both PROGMEM and normal strings.
By letting this library accept __FlashStringHelper* as well, you can
use easily use the F() macro and reduce the chance of mixing up normal
and PROGMEM pointers.
This stores the number of valid bytes in lastread. Having access to this
information allows transmitting binary data as well, containing embedded
nul bytes.
The lastread array stores the data bytes of the most recently read
received packet. It was previously declared as an array of pointers to
uint8_t, instead of an array of uint8_t, which obviously was not the
intention. Because the examples explicitely cast to (char*) for printing
(because Print::println(uint8_t[]) is not defined) and a pointer is at
least as big as uint8_t and memcpy uses void*, this problem did not show
up before.
This method contained a loop that checked for an existing subscription,
but it only printed a debug message if so. Now, it immediately returns
true if the subscription is already registered.
When this method succesfully adds a subscription, it was documented to
return true, but the actual code did not return anything in this case,
resulting in a compiler warning. In practice, on AVR, the value of the
first argument would be returned, which likely evaluates as true, so
it is likely it actually seemed to work fine.
The payload data is never modified by the library, so using const is
possible without any further changes. Using const allows using a string
literal, or String::c_str() as the payload.