I’m setting up a mqtt client on an Arduino with PubSubClient. However I’m struggling with the right way
to publish a topic with a retain flag. I can’t get it right. I’ve been looking in the PubSubClient API
but I cannot find what I’m doing wrong
In the future, please paste code as text, not as screenshot.
Regarding the problem: These are the possible overloads of the function
Then you’ll notice that the second parameter needs to be a const uint8_t* when you want to send some data – not an integer. You seem to want to transmit the value 5 as a 2-byte value (aka short), directly in binary form, not as an ASCII string. Then write (uint8_t*)&conMsg as the second parameter.
Also, in case it compiles but you don’t see the correct data being sent, you might actually want to send a string instead of the pure integer. You could do that with e.g.
client.publish(conTopic, "5", true);
note that there also C stdlib functions which convert from integer values to string values (when supplied a buffer), e.g. itoa or snprintf() and friends functions with e.g.
int valueToSend = 5;
char valueAsString[32] = {0};
snprintf(valueAsString, sizeof(valueAsString), "%d", valueToSend);
// send valueAsString as ASCII string as above