Hi,
i´m using the library pubsubclient for MQTT. Now i´m trying to concate a string to calculate the variable topic. The MQTT_ROOT is changeable, the "MQTT_GetTemperature" is fix and not changable
The problem is, expected is a "const char*", but i have only a string.
Can anyone give me a hint for an code example to solve this?
String MQTT_ROOT = "/home/dg/";
const String MQTT_GetTemperature = "/temperature/get/value";
char msg[50];
snprintf (msg, sizeof(msg), "%.1f", bme.readTemperature());
String topic = sprintf("%s/%s", MQTT_ROOT, MQTT_GetTemperature);
client.publish(topic, msg);
error: cannot convert 'String' to 'const char*' for argument '2' to 'int sprintf(char*, const char*, ...)'
error: no matching function for call to 'PubSubClient::publish(String&, char [50])'
In file included from test.ino:21:0:
C:\Programme\arduino-1.6.5-r5\MySketchbook\libraries\PubSubClient-2.6.0\src/PubSubClient.h:130:12: note: candidate: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Programme\arduino-1.6.5-r5\MySketchbook\libraries\PubSubClient-2.6.0\src/PubSubClient.h:130:12: note: no known conversion for argument 1 from 'String' to 'const char*'
Thanks works, but unfortunally only for the first call
after that, the char content is corrupted??
char msg[50];
char topic[50];
char* MQTT_ROOT = "/home/dg/";
const char* MQTT_GetTemperature = "temperature/get/value";
const String MQTT_GetPressure = "pressure/get/value";
const String MQTT_GetHumidity = "humidity/get/value";
snprintf (msg, sizeof(msg), "%.1f", bme.readTemperature());
snprintf (topic, sizeof(topic), "%s%s", MQTT_ROOT, MQTT_GetTemperature);
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
Serial.println(msg);
snprintf (msg, sizeof(msg), "%.1f", bme.readHumidity());
snprintf (topic, sizeof(topic), "%s%s", MQTT_ROOT, MQTT_GetHumidity);
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
Serial.println(msg);
snprintf (msg, sizeof(msg), "%.1f", bme.readPressure() / 100.0F);
// snprintf (topic, sizeof(topic), "%s%s", MQTT_ROOT, MQTT_GetPressure);
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
Serial.println(msg);
Result:
Topic: /home/dg/temperature/get/value
Message: 23.2
Topic: /home/dg/ô‹ü?
Message: 57.6
Topic: /home/dg/ô‹ü?
Message: 1010.4
Edit: sorry, reason found by myself during reading my written post 
Changing the other Strings to char* and all works well 
const char* MQTT_GetPressure = "pressure/get/value";
const char* MQTT_GetHumidity = "humidity/get/value";