So I am trying to publish some values to a topic using jsondoc, but unfortunately only some of the values get published. It sends 6 out of the 10. My memory is 256 for the doc here is the code. The output I get in node red is
{"hat_temperature":31.3,"hat_humidity":27.55,"hat_pressure":987.87,"Sen_temperature":null,"sen_humidity":null,"sen_brightness":1
I have an MQTT in node connected to debug node.
StaticJsonDocument<256> doc;
doc["hat_temperature"] = 31.3;
doc["hat_humidity"] = 27.55;
doc["hat_pressure"] = 987.87;
doc["Sen_temperature"] = dht.readTemperature();
doc["sen_humidity"] = dht.readHumidity();
doc["sen_brightness"] = 12;
doc["sen_lights"] = "on";
doc["sen_presence"] = "present";
doc["ID"] = "HAMK_876";
doc["message"] = "Test";
serializeJson(doc, obj);
Serial.print(obj);
mqttClient.beginMessage(topic);
mqttClient.print(obj);
mqttClient.endMessage();
This is what my Arduino publish routine looks like. I had to increase the StaticJsonDocument size to 384 otherwise nothing got publish. It took a long time tracking down the problem. Hope this helps.
void publish_the_values()
{
if (debug) {
Serial.print("Celsius temperature: ");
Serial.print(ds18b20_temp_C);
Serial.print(" - Fahrenheit temperature: ");
Serial.println(ds18b20_temp_F);
}
StaticJsonDocument<384> doc;
doc["report"] = "readings";
doc["loc"] = location;
doc["reason"] = reason;
doc["batt"] = battery_voltage;
doc["bb_C"] = ds18b20_temp_C;
doc["bb_F"] = ds18b20_temp_F;
doc["cf_t"] = cf_temperature;
doc["cf_h"] = cf_humidity;
doc["cf_p"] = cf_pressure;
doc["ssid"] = WiFi.SSID();
doc["rssi"] = WiFi.RSSI();
doc["ip"] = WiFi.localIP();
char buffer[384];
size_t n = serializeJson(doc, buffer);
char topic[40]; // 40 chars wide
strcpy(topic, mqtt_publish_topic);
strcat(topic, "/");
strcat(topic, location);
if (debug) {
Serial.print("Publish topic: ");
Serial.println(topic);
Serial.println(buffer);
}
mqtt_client.publish(topic, buffer, n);
}
//------------ End of publish_the_values()
What did you use for calculating the value of the document?
In my situation nothing was being sent via MQTT, so I just increased the size from 256 to 384 (trial and error). I did try visually counting the characters in the message but gave up.
I'm using version 6 of the ArduinoJson library by Benoit Blanchon. There is an Assistant program on his website that enables an accurate length of the document-string to be calculated.
ok thanks, for your help. Realized my prob was that i used too long value names. Your value names are short so that helped me to realize my issue.