MQTT - variable concatenation

Hi all,
I need your help in building the MQTT publish string. I don't know how to concatenate the variables in the string.
For example:
client.publish("domoticz/in", "{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}");
Having: mqtt_index=2,
I would like to put it in the string like this:
client.publish("domoticz/in", "{"idx":" + mqtt_index + ","nvalue":1,"svalue":"","Battery":100,"RSSI":7}");

But I receive an error.

Keeping the error a secret?

const char* someDangVariable = "domoticz/in";

client.publish( someDangVariable, ("{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}").c_str() );

Here is the error:
error: no matching function for call to 'PubSubClient::publish(const char [12], StringSumHelper&)'
client.publish("domoticz/in", "{"idx":" + mqtt_idx + ","nvalue":1,"svalue":"","Battery":100,"RSSI":7}");
^
C:\Users\joyce\Desktop\desktop sony\ESP8266_eep6\ESP8266_eep6.ino:485:121: note: candidates are:
In file included from C:\Users\joyce\Desktop\desktop sony\ESP8266_eep6\ESP8266_eep6.ino:6:0:
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:151:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:151:12: note: no known conversion for argument 2 from 'StringSumHelper' to 'const char*'
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:152:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:152:12: note: candidate expects 3 arguments, 2 provided
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:153:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:153:12: note: candidate expects 3 arguments, 2 provided
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:154:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
C:\Users\joyce\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:154:12: note: candidate expects 4 arguments, 2 provided
exit status 1
no matching function for call to 'PubSubClient::publish(const char [12], StringSumHelper&)'

What I need actually is to put a variable containing the value "2" as you can see in red.
Thanks again

const char* someDangVariable = "domoticz/in";

client.publish( someDangVariable, String(2).c_str() );

Thanks for replying but I don't understand.
How should I modify:
client.publish( someDangVariable, ("{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}").c_str() );

If all you want to send is a 2 then send client.publish( someDangVariable, String(2).c_str() );

If you don't want to hard code it

then

int someIntStuffForMQTT = 2;

client.publish( someDangVariable, String(someIntStuffForMQTT ).c_str() );

Ok, and if I would send:
client.publish("domoticz/in", "{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}");
With the"2" inside a variable?

String someIntStuffForMQTT = "{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}";

client.publish( someDangVariable, someIntStuffForMQTT.c_str() );

Thanks! It works

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.