Hi,
I have a problem sending JSON data to Node-Red. Here is the code which transmitts data...
// Daten formartieren
dtostrf(dht_humidity,5,1, buffer_dht_humidity);
dtostrf(dht_temperatur,5,1, buffer_dht_temperatur);
dtostrf(entfernung,5,1, buffer_entfernung);
dtostrf(temperatur,5,1, buffer_temperatur);
dtostrf(luftdruck/100,5,1, buffer_pressure);
// Daten ueber JSON-MQTT an Frontend
messwerteJSON["ESP_Data"] = buffer_ESP;
messwerteJSON["entfernung"] = buffer_entfernung;
messwerteJSON["temperatur"] = buffer_temperatur;
messwerteJSON["pressure"] = buffer_pressure;
messwerteJSON["DHT_humidity"] = buffer_dht_humidity;
//messwerteJSON["DHT_temperatur"] = buffer_dht_temperatur;
// JSON Daten in eine Variable schreiben
char ESPDaten[256];
serializeJson(messwerteJSON, ESPDaten);
Serial.println(ESPDaten);
client.publish("Serra_Messdaten", ESPDaten);
}
if I send(by means of MQTT) the above data Node red receives it.
But if I uncomment the line "messwerteJSON["DHT_temperatur"] = buffer_dht_temperatur;
Node-Red doesn't accept the data.
My Node-Red function to deserialize the JSON is like:
var ESP_Distance = { payload: msg.payload.entfernung };
var ESP_Temp1 = { payload: msg.payload.temperatur };
var ESP_Pressure = { payload: msg.payload.pressure };
var ESP_DHT_Humidity = { payload: msg.payload.DHT_humidity };
var ESP_DHT_Temperatur = { payload: msg.payload.DHT_temperatur };
return [ESP_Distance, ESP_Temp1, ESP_Pressure, ESP_DHT_Humidity, ESP_DHT_Temperatur];
The above data is then displayed by means of gauges.
The debug window do not display anything if with six JSON values, with five values it works fine.
Some helpfull idea's?
Alf
Hi, thanks for your reply...
please apologize I do not know how to wrap the code and text output in a preformatted text block.. how to do?
I send you the debug text from node-red when it is working. But as soon as I add another "messwert" data line .. as explained in my former message, there is no text in node-red debug window....
I suspect it is something to do with declaring the size of the variable to handle the Json string.
I doubt if the problem is at the Node-RED end as you say it correctly receives a message of 5 lines.
One of my IoT students had a similar problem last week.
Here's a link to the thread on the forum and the Arduino code we created and used successfuly.
We used these Arduino libraries...
PubSubClient
ArduinoJson
Note:
My student's requirement was 'subscribing' to MQTT messages. I'm sure the Arduino sketch can be extended or modified to handle 'publishing' MQTT messages.
hi dynamicdave...
you should have sent a link, but I could not find it...
I tried variuos methods to solve the problem, but each time I JSONize (serialize) more then five values, Node-red will not accept those, no message in debug, nothing!
regards,
Alf
You obviously didn't follow the link to this article/thread (I posted above) as if you had read it you would have seen the link to the Arduino files that work for me and my IoT students.
Here's a direct link to the Arduino files, but I would encourage you to read the article I posted.
I've just found some code to 'publish' values from a temperature/humidity/pressure (thp) BME280 sensor.
You will have to edit the code to remove all the bits (my variables) you don't need. Hope this helps.
void publish_thp()
{
status = "fail";
if (bitRead(status_register, thp_pos))
status = "good";
StaticJsonDocument<256> doc; // The names in doc["xxx"] have been shortened to fit within the char limit !!!
doc["remote"] = "node" + String(remote_node_ref);
doc["src"] = "ws_pcb"; //Tag for Node-RED to indicate the data-source (e.g. "rf24", "lora", "wsv1", "wsv2")
doc["type"] = "bme280";
doc["sensor_st"] = status;
doc["t"] = temperatureValue;
doc["h"] = humidityValue;
doc["p"] = pressureValue;
doc["base"] = "node98";
doc["ssid"] = WiFi.SSID();
doc["rssi"] = WiFi.RSSI();
char topic[30]; // 30 chars wide
strcpy(topic, mqtt_publish_topic);
strcat(topic, "/");
strcat(topic, thisNodeID);
char buffer[256];
size_t n = serializeJson(doc, buffer);
mqtt_client.publish(topic, buffer, n);
} //------------ End of publish_thp()
To overcome the limit on length of MQTT messages we decided to publish readings in sequence.
E.g. BME280, solar panel data, DS18B20.
void transmit_data_to_mqtt_broker()
{
if (newData == true)
{
publish_thp();
publish_solar();
publish_analog();
newData = false;
}
} //============ End of transmit_data_to_mqtt_broker()
Have you tried shortening the names in your Json string to see if that gets over the issue?
e.g. Use ["t"] for temperature, ["p"] for pressure, ["h"] for humidity
One of my IoT students was using really long names (and name-length was his problem).