[SOLVED] MQTT working with 2 variables but not with more?

I am using the STEM account. I have one device with 5 variables sending data every 10 seconds via MQTT. So that should be within the limits. But nothing seems to be arriving. If I comment out any 3 of the 5 variables, it works… in any combination… but not if I try to submit 3 or more variables. This is the payload I would like to send:

{ “airtemperature”: {“value”: 2.00 }, “airhumidity”: {“value”: 6.00 }, “airpressure”: {“value”: 0.04 }, “rateofrain”: {“value”: 5.00 }, “waterlevel”: {“value”: 6.00 } }

Code below…

Any help to this newbee would be much appreciated… :slight_smile:

Best regards,

zeGerman

float airTemperature = random(0, 9);
float airPressure = random(0, 9);
float airHumidity = random(0, 9);
rateofrain = random(0, 9);
lastsmoothwaterlevel = random(0, 9);

/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(airTemperature, 4, 2, str_temp);
dtostrf(airPressure/100, 4, 2, str_pres);
dtostrf(airHumidity, 4, 2, str_hum);
dtostrf(lastsmoothwaterlevel, 4, 2, str_waterlevel);
dtostrf(rateofrain, 4, 2, str_rateofrain);

sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_WELL);

sprintf(payload, "{%s", ""); // opening braket

sprintf(payload, "%s \"%s\":", payload, VARIABLE_TEMP); // Adds the variable label   
sprintf(payload, "%s {\"value\": %s", payload, str_temp); // Adds the value
//sprintf(payload, "%s, \"context\":{\"lat\": %s, \"lng\": %s}", payload, str_lat, str_lng); // Adds context
sprintf(payload, "%s }", payload); // closes variable
sprintf(payload, "%s, ", payload); // next variable

sprintf(payload, "%s \"%s\":", payload, VARIABLE_HUM); // Adds the variable label   
sprintf(payload, "%s {\"value\": %s", payload, str_hum); // Adds the value
sprintf(payload, "%s }", payload); // closes variable
sprintf(payload, "%s, ", payload); // next variable

sprintf(payload, "%s \"%s\":", payload, VARIABLE_PRES); // Adds the variable label   
sprintf(payload, "%s {\"value\": %s", payload, str_pres); // Adds the value
sprintf(payload, "%s }", payload); // closes variable
sprintf(payload, "%s, ", payload); // next variable

sprintf(payload, "%s \"%s\":", payload, VARIABLE_RATEOFRAIN); // Adds the variable label   
sprintf(payload, "%s {\"value\": %s", payload, str_rateofrain); // Adds the value
sprintf(payload, "%s }", payload); // closes variable
sprintf(payload, "%s, ", payload); // next variable

sprintf(payload, "%s \"%s\":", payload, VARIABLE_WATERLEVEL); // Adds the variable label   
sprintf(payload, "%s {\"value\": %s", payload, str_waterlevel); // Adds the value
sprintf(payload, "%s }", payload); 

sprintf(payload, "%s }", payload); // Closes the dictionary brackets

mqttclient.publish(topic, payload);
mqttclient.loop();

Hi there, please refer to this topic: [SOLVED] Only one variable will publish using ESP32 and revised sample sketch

All the best

Hey there, thank you for the cross post… It was not quite the solution I was looking for as I am using a wemos so I should be able to send more than one variable at a time… But in the end I gave up (as I was just being a perfectionist I guess) and am doing it exactly like you said (except that I use the same topic over and over as I don’t see why I should not?)… So thank you… :slight_smile: