I use the PubSubClient im my project to transmit temperature from an DHT22 sensor with an ESP01 via MQTT.
All works fine, all my tests and so on. But I bitterly fail in bulding a variable to use with client.publish.
My cloud cumulocity where I have to transmit the template with the temperature.
e.g. client.publish ("s/us", "211,25") //uses the template 211 for temperature and transmits 25 *C
I've a working function to read the DHT22 but when I try to build the client.publish ("s/us", "211,"+temperatureTemp); with template and the temperature, I'll get the following error.
Temp_Hum_Vcc-ESP01:155: error: invalid operands of types 'const char [5]' and 'char [7]' to binary 'operator+'
client.publish("s/us", "211,"+temperatureTemp);
^
exit status 1
invalid operands of types 'const char [5]' and 'char [7]' to binary 'operator+'
This is the code of the function. I can't fugure it out how I can combine them.
now = millis();
// Publishes new temperature and humidity every 10 seconds
if (now - lastMeasure > 10000) {
lastMeasure = now;
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
client.publish("s/us", "211,25");
return;
}
// Computes temperature values in Celsius
float hic = dht.computeHeatIndex(t, h, false);
static char temperatureTemp[7];
dtostrf(hic, 6, 2, temperatureTemp);
// Uncomment to compute temperature values in Fahrenheit
// float hif = dht.computeHeatIndex(f, h);
// static char temperatureTemp[7];
// dtostrf(hic, 6, 2, temperatureTemp);
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
// Publishes Temperature and Humidity values
client.publish("s/us", "211,"+temperatureTemp);
//client.publish("s/us", humidityTemp);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
}
Using client.publish ("s/us", temperatureTemp); works fine, but adding the template "211," before temperature is where I'm failing.
PS: I'm a rookie in programming with Arduino IDE and I'm desperate stranded