Error sending data from ESP

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

Please wrap code and text output in a pre-formatted text block, it is much easier to read. Thanks.

Also, you should show the raw incoming text so that we can see what is wrong with the data.

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....
Debug Input Node-red

I send a picture with explanations hoping to point out the problem...
thanks, Alf

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.

Please provide a complete copy of your ESP code

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

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

Dear Zenofmud,
here is my ESP code...

/*
 letzte Programmversion mit BMP280und HCSR04 un d JSON Daten vom 09.05.2022 
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ArduinoJson.h>
#include <DHT.h>

// Update these with values suitable for your network.
const char* ssid = "????????";
const char* password = "??????????";
const char* mqtt_server = "192.168.0.110";

// Json Object erstellen
DynamicJsonDocument messwerteJSON(1024);

// DHT Variable deklarieren
#define DHT_TYPE DHT22
const int DHT_PIN = 13;
DHT dht(DHT_PIN,DHT_TYPE);
float dht_humidity;
float dht_temperatur;

// HCSR04 Variablen deklarieren
#define US_SENSOR_TRIG_PIN 10  
#define US_SENSOR_ECHO_PIN 9  
float entfernung;
float dauer;

// BMP280-Sensor Variablen deklarieren...
Adafruit_BMP280 bmp;
float temperatur, luftdruck, hoehe;

void lesenDHT22(){
  dht_humidity = dht.readHumidity();
  dht_temperatur = dht.readTemperature();
  Serial.println("DHT-Humidity: " + String (dht_humidity));
  Serial.println("DHT-Temperatur: " + String (dht_temperatur));
}

void lesenHCSR04(){
  digitalWrite(US_SENSOR_TRIG_PIN, LOW);
  delayMicroseconds(5);
  digitalWrite(US_SENSOR_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(US_SENSOR_TRIG_PIN, LOW);
  dauer = pulseIn(US_SENSOR_ECHO_PIN, HIGH);
  entfernung = dauer*0.034/2;
  Serial.println("Entfernung (cm): " + String (entfernung));
}

void lesenBMP280() {
  temperatur = bmp.readTemperature();
  Serial.println("temperatur: " + String(temperatur) + " *C");
  luftdruck = bmp.readPressure();
  Serial.println("Luftdruck: " + String(luftdruck/100) + " hPa");
  hoehe = bmp.readAltitude(1013.25);
  Serial.println("Hoehe: " + String(hoehe) + " m");
  Serial.println();
}

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(BUILTIN_LED, OUTPUT);          // Initialize the BUILTIN_LED pin as an output
  pinMode(US_SENSOR_TRIG_PIN, OUTPUT);   // 
  pinMode(US_SENSOR_ECHO_PIN, INPUT);   // 
  dht.begin();
  
  // Sensor suchen....
  if (!bmp.begin(0x76))
  {
  Serial.println("kein BMP-Sensor gefunden");
  }
  else
  {
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                  Adafruit_BMP280::SAMPLING_X2,
                  Adafruit_BMP280::SAMPLING_X16,
                  Adafruit_BMP280::FILTER_X16,
                  Adafruit_BMP280::STANDBY_MS_500);
  }
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
    
  // hier wird eine Ausgabe verzoegert... 
  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 50, "Message from ESP8266 #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);

    // DHT Sensor auslesen
    lesenDHT22();
    delay(500);
    
    // HCSR04 auslesen...
    lesenHCSR04();
    delay(500);
    
    // BMP280-Sensor auslesen....
    if (bmp.begin(0x76)){
    lesenBMP280();
    }    
    // Daten ueber JSON-MQTT an Frontend  
    messwerteJSON["ESP_Data"] = ""; 
    messwerteJSON["entfernung"] = String(entfernung,1);  
    messwerteJSON["temperatur"] = String(temperatur,1);
    messwerteJSON["pressure"] = String(luftdruck/100,1);
    messwerteJSON["DHT_humidity"] = String(dht_humidity,1);
    //messwerteJSON["DHT_temperatur"] = String(dht_temperatur,1);   // without this 6th line of values Node-Red accept the JSONized data, 
                                                                    // with the 6th line Node-Red debugger does not show anything!!
    // JSON Daten in eine Variable schreiben
    char ESPDaten[256];
    serializeJson(messwerteJSON, ESPDaten);
    Serial.println(ESPDaten);   
    client.publish("Serra_Messdaten", ESPDaten);
  }
}

NOTE: I edited your code to remove the SSID and PASSWORD.

I see you have
DynamicJsonDocument messwerteJSON(1024);
then

    // JSON Daten in eine Variable schreiben
    char ESPDaten[256];
    serializeJson(messwerteJSON, ESPDaten);

the default maximum size of a packet is 1024. Try changing the size of 'ESPDaten' to 512 and see what happens.

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).

Hi guys,
instead of going deeper into the problem I decided to devide the data into two packages as follows:

 // Daten ueber JSON-MQTT an Frontend  
    BMP280JSON["ESP_Datapack1"] = ""; 
    BMP280JSON["entfernung"] = String(entfernung,1);  
    BMP280JSON["temperatur"] = String(temperatur,1);
    BMP280JSON["pressure"] = String(luftdruck/100,1); 
                                                                    
    // JSON BMP280 Daten in eine Variable schreiben
    char BMP280Daten[256];
    serializeJson(BMP280JSON, BMP280Daten);
    Serial.println(BMP280Daten);   
    client.publish("Serra_Messdaten", BMP280Daten);

    // JSON DHT Daten in eine Variable schreiben
    DHTJSON["ESP_Datapack2"] = "";
    DHTJSON["DHT_humidity"] = String(dht_humidity,1);
    DHTJSON["DHT_temperatur"] = String(dht_temperatur,1);  
    char DHTDaten[256];
    serializeJson(DHTJSON, DHTDaten);
    Serial.println(DHTDaten);   
    client.publish("Serra_Messdaten", DHTDaten);

this works!
Thanks for your effords to help me... I hope to hear from you as soon as I post another problem in the future...
Thanks again, Alf

Glad to hear you got it working, although it would have been interesting to hear if shortening the json names would have had cured the issue.

1 Like

high,
shortening names helped!
Thanks

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.