MQTT - concatenazione variabili

Ciao a tutti,
chiedo il vostro gentile aiuto.
Non riesco a capire come costruire dinamicamente una stringa di pubblicazione MQTT con determinate variabili assegnate.
In particolare:
client.publish("domoticz/in", "{"idx":2,"nvalue":1,"svalue":"","Battery":100,"RSSI":7}");

dovendo mettere al posto del valore 2 una variabile x (di tipo stringa, contenente il valore), se provo con la normale concatenazione di stringhe:

client.publish("domoticz/in", "{"idx":" + x + ","nvalue":1,"svalue":"","Battery":100,"RSSI":7}");

ricevo un errore:
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&)'

come posso risolvere?
grazie
Antonio

che libreria stai usando?...posta un link

uso la PubSubClient

quel metodo è così composto:

boolean publish (topic, payload, [length], [retained])

Publishes a message to the specified topic.
Parameters

    topic const char[] - the topic to publish to
    payload const char[], byte[] - the message to publish
    length unsigned int (optional) - the length of the payload. Required if payload is a byte[]
    retained boolean (optional) - whether the message should be retained
        false - not retained
        true - retained

Returns

    false - publish failed, either connection lost or message too large
    true - publish succeeded

quindi...perchè invece di scrivere direttamente nel metodo cosa vuoi inviare non ti crei la tua bella array di char dove al posto giusto ci metti il valore giusto e poi passi l'array?...la cose da fare sono:

  • sapere quale sarà la lunghezza massima necessaria per definire l'array (attenzione al valore delle variabili numeriche)
  • usare la "sprintf" o la "snprintf" per popolare la tua array.

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