-->
Page 1 of 1

String to const char* payload in mqttClient.publish(

PostPosted: Sun Apr 30, 2017 2:53 am
by GeoffState
I am using AsyncMqttClient and have a String payloadStr which ends up containing a two-word sentence e.g. "Open Open". I have spent several days trying to cast this into payload but have not asked the correct question of Google. I know
mqttClient.publish(MQTT_FEEDBACK_TOPIC, 0, true, "Open Open")

works and is processed by Node-Red correctly but
mqttClient.publish(MQTT_FEEDBACK_TOPIC, 0, true, payloadStr)

(plus many variations I have tried) returns an exit status
no matching function for call to 'AsyncMqttClient::publish(const char [19], int, bool, String&)
which I can't interpret. I understand payload.length computes itself when a string is passed so I have omitted this.

Re: String to const char* payload in mqttClient.publish(

PostPosted: Mon May 01, 2017 11:12 pm
by gbafamily1
You did not mention the data type of payloadStr. However, if it is declared as type String as in
Code: Select allString payloadStr;
then the following might work.

Code: Select allmqttClient.publish(MQTT_FEEDBACK_TOPIC, 0, true, payloadStr.c_str())

Re: String to const char* payload in mqttClient.publish(

PostPosted: Fri Apr 03, 2020 5:06 am
by LovleyLargs
OK, this is an old question, today for me it was new.
Thank You, it worked.