Sensor data turned into null value after function node

Hi everyone, I'm developing a smart weather monitoring system which I mainly use Arduino Uno microcontroller, DHT11 for collecting temperature and humidity, BMP180 for pressure and altitude, and LDR sensor for light level. I've uploaded my arduino sketch to the microcontroller and its working.
The output will be something like this:

Humidity,Temperature(Celcius): 65.00,24.40
Altitude, Atmospheric Pressure: 65.65,1005.28
Luminousity: 423

I tried to use node red as a platform and I've noticed my sensor data coming serial port got turned into null values after passing through my function node. I have been scratching my head for hours now still don't know how to solve it.

Here is my function node code:

let data = (typeof msg.payload === 'string' ? msg.payload : msg.payload.toString()).split(',');

let Humidity = { payload: parseFloat(data[0]) };

let Temperature = { payload: parseFloat(data[1]) };

return [Humidity, Temperature];

Here is the debug output I connected the serial port node:

And here is the debug output when I connect the debug node to the function node:

I'm trying to solve for temperature & humidity first, I'll do the same thing for pressure & altitude and light levels if the solution have been found.

Hi @mstk_olsen, welcome to the forum.

The data coming into Node-red looks like a string:
Untitled 2
Your function first makes certain that it is a string and then assumes it's an array of strings.
I do not know what data[0] contains but for sure it can't be recognised as a number, so you end up with two messages whose payload is NaN (Not A Number)

You could handle it by splitting the string into an array at the ':' and splitting array[1] into another array at the ','

But it would life much easier if you made your Arduino send the data formatted as a Json string

{
"Humidity": 60.0,
"Temperature": 24.10,
"TemperatureUnits": "Celsius"
}

Then pass the serial output through a Json parser node and the values will be available as msg.payload.Humidity and msg.payload.Temperature.

1 Like

I see... But I suspect that it should be my Arduino Serial.println that messed up. But mind if you can take a look at it and see if it was problem there?

EDIT: I think I managed to seperate somehow after i deleted the description of "Humidity, Temperature(Celcius): "......, but the values is messed up in node red.

That is exactly why @jbudd suggested

Because JavaScript knows EXACTLY how to convert the parts of JSON into string/number without any parseInt calls, without any string splitting or other parsing.


In short: modify your Arduino code to send { "Humidity": 60.0, "Temperature": 24.10, "TemperatureUnits": "Celsius" } and you will have no problems. And if you change the data at some future point to include stationId: 1 it will "just work" (because thats what JSON was designed for) :slight_smile:

Have a look at the ArduinoJson library to format a Json string for you.
Version 6 is I believe the latest released version.
It works really well with my Wemos D1 Minis (ESP8266) sending data to Node-RED via MQTT.

1 Like

Or..... send the data in three or four messages. There's more than one way to skin, filet and make cat soup.

Ok so now i formatted the data into JSON but in my node red is not showing up anything.


Arduino code:

#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>

#define DHTPIN 7 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);  

Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(9600); 
  dht.begin();

  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1);
  }
}

void loop() {
   float Temperature = dht.readTemperature();   
   float Humidity = dht.readHumidity();  

 

    if (isnan(Temperature) || isnan(Humidity)){
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
    float bmppressure = bmp.readPressure()/100.00;
    float bmpaltitude = bmp.readAltitude();

    unsigned int ldrValue;
    ldrValue = analogRead(A0);
    StaticJsonDocument<200> jsonDoc;
    jsonDoc["dht"]["humidity"] = Humidity;
    jsonDoc["dht"]["temperature"] = Temperature;
    jsonDoc["bmp"]["altitude"] = bmpaltitude;
    jsonDoc["bmp"]["pressure"] = bmppressure;
    jsonDoc["ldr"]["lightLevel"] = ldrValue;
    serializeJson(jsonDoc, Serial);
  
    /*String dhtData = String(Humidity) + "," + String(Temperature);
    String bmpData = String(bmpaltitude) + "," + String(bmppressure);
    String ldrData = String(ldrValue);
    
    Serial.println(dhtData);
    Serial.println(bmpData);
    Serial.println(ldrData);*/
    delay(3000); 
}

This is my node red flow:

[
    {
        "id": "187b0bb9eda06271",
        "type": "tab",
        "label": "Smart Weather Monitoring System",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d884e680153f7ef9",
        "type": "serial in",
        "z": "187b0bb9eda06271",
        "name": "Arduino Maker Uno",
        "serial": "f8031aa692189f38",
        "x": 110,
        "y": 220,
        "wires": [
            [
                "298062ff1bce572c"
            ]
        ]
    },
    {
        "id": "43b64526193c24c0",
        "type": "function",
        "z": "187b0bb9eda06271",
        "name": "Data",
        "func": "let data = (typeof msg.payload === 'string' ? msg.payload : msg.payload.toString()).split(',');\n\nlet Humidity = { payload: parseFloat(data[0]) };\nlet Temperature = { payload: parseFloat(data[1]) };\n\nlet Altitude = { payload: parseFloat(data[2]) };\nlet Pressure = { payload: parseFloat(data[3]) };\n\nlet LightLevel = { payload: parseInt(data[4]) }\n\nreturn [Humidity,Temperature,Altitude,Pressure,LightLevel];\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 220,
        "wires": [
            [
                "cb2b8668573a675c",
                "3c5c16bcb2531195"
            ],
            [
                "9521c4917a80a9a0",
                "a83551e53e6e8cf9"
            ],
            [
                "fe061fa0a7a3a932",
                "efb25ca3a309af9a"
            ]
        ]
    },
    {
        "id": "cb2b8668573a675c",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "rules": [
            {
                "t": "set",
                "p": "fields.humidity",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "tags.region",
                "pt": "msg",
                "to": "KL",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.temperature",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 60,
        "wires": [
            [
                "f6c309b7549c21c0"
            ]
        ]
    },
    {
        "id": "3c5c16bcb2531195",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 770,
        "y": 100,
        "wires": []
    },
    {
        "id": "f6c309b7549c21c0",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "topic": "sensor/dht11/HumidANDTemp",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1190,
        "y": 60,
        "wires": []
    },
    {
        "id": "1c98f6a210d9a5cb",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "topic": "sensor/dht11/HumidANDTemp",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 120,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "0327f64d58edc99b",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Humidity & Temperature",
        "measurement": "HumidTemp",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 470,
        "y": 420,
        "wires": []
    },
    {
        "id": "b1d8a857d8b58b2b",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Humidity & Temperature",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 770,
        "y": 420,
        "wires": [
            [
                "f317d195f17e386f"
            ]
        ]
    },
    {
        "id": "f317d195f17e386f",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Humidity & Temperature",
        "query": "select * from HumidTemp",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1130,
        "y": 420,
        "wires": [
            [
                "9b67bc942370f8f1",
                "95e5f54cb3cb3f0c"
            ]
        ]
    },
    {
        "id": "9b67bc942370f8f1",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Humidity",
        "group": "73e85ccf4436dcc5",
        "order": 1,
        "width": 6,
        "height": 4,
        "gtype": "gage",
        "title": "Humidity",
        "label": "%rh",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1460,
        "y": 400,
        "wires": []
    },
    {
        "id": "95e5f54cb3cb3f0c",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Temperature",
        "group": "73e85ccf4436dcc5",
        "order": 2,
        "width": 6,
        "height": 4,
        "gtype": "gage",
        "title": "Temperature",
        "label": "Celcius",
        "format": "{{value}}°C",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1470,
        "y": 440,
        "wires": []
    },
    {
        "id": "9521c4917a80a9a0",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "rules": [
            {
                "t": "set",
                "p": "fields.altitude",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "tags.location",
                "pt": "msg",
                "to": "indoors",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.pressure",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 790,
        "y": 180,
        "wires": [
            [
                "ea005c1d37e31c5e"
            ]
        ]
    },
    {
        "id": "ea005c1d37e31c5e",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "topic": "sensor/bmp180/AltitudeANDPressure",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1210,
        "y": 180,
        "wires": []
    },
    {
        "id": "a83551e53e6e8cf9",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 220,
        "wires": []
    },
    {
        "id": "fe061fa0a7a3a932",
        "type": "change",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "rules": [
            {
                "t": "set",
                "p": "tags.status",
                "pt": "msg",
                "to": "Good",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "fields.lightLevel",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$append(fields,tags)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 300,
        "wires": [
            [
                "8e1747b1f9b24bef"
            ]
        ]
    },
    {
        "id": "8e1747b1f9b24bef",
        "type": "mqtt out",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "topic": "sensor/ldr/luminousity",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "262512044f4f61ac",
        "x": 1150,
        "y": 300,
        "wires": []
    },
    {
        "id": "efb25ca3a309af9a",
        "type": "debug",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 730,
        "y": 340,
        "wires": []
    },
    {
        "id": "de3c5ea50f1fea9e",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "topic": "sensor/bmp180/AltitudeANDPressure",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 150,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "7e7eb7b086be8aea",
        "type": "mqtt in",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "topic": "sensor/ldr/luminousity",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "262512044f4f61ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 90,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "077cdcf90a88e107",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Altitude & Atmospheric Pressure",
        "measurement": "AltiPres",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 490,
        "y": 480,
        "wires": []
    },
    {
        "id": "107ef1e8df34e717",
        "type": "influxdb out",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Luminousity",
        "measurement": "luminousity",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 430,
        "y": 540,
        "wires": []
    },
    {
        "id": "19c0a1e84ad3b9f3",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Altitude & Atmospheric Pressure",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 800,
        "y": 500,
        "wires": [
            [
                "a66fd1da4f53e1b6"
            ]
        ]
    },
    {
        "id": "a66fd1da4f53e1b6",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Altitude & Atmospheric Pressure",
        "query": "select * from AltiPres",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1150,
        "y": 500,
        "wires": [
            [
                "8a575bbfbfd7d159",
                "655c9885536327a3"
            ]
        ]
    },
    {
        "id": "8a575bbfbfd7d159",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Altitude",
        "group": "73e85ccf4436dcc5",
        "order": 3,
        "width": 6,
        "height": 4,
        "gtype": "wave",
        "title": "Altitude",
        "label": "meters",
        "format": "{{value}}",
        "min": "500",
        "max": "1200",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1460,
        "y": 480,
        "wires": []
    },
    {
        "id": "655c9885536327a3",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Atmospheric Pressure",
        "group": "73e85ccf4436dcc5",
        "order": 4,
        "width": 6,
        "height": 4,
        "gtype": "wave",
        "title": "Atmospheric Pressure",
        "label": "Pa",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1500,
        "y": 520,
        "wires": []
    },
    {
        "id": "a64c47964ffb2163",
        "type": "inject",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 730,
        "y": 580,
        "wires": [
            [
                "d7cda6c27c50de2c"
            ]
        ]
    },
    {
        "id": "d7cda6c27c50de2c",
        "type": "influxdb in",
        "z": "187b0bb9eda06271",
        "influxdb": "921764b5c0c308e9",
        "name": "Luminousity",
        "query": "select * from luminousity",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 1090,
        "y": 580,
        "wires": [
            [
                "4ea5d6334f76f042"
            ]
        ]
    },
    {
        "id": "4ea5d6334f76f042",
        "type": "ui_gauge",
        "z": "187b0bb9eda06271",
        "name": "Luminousity",
        "group": "73e85ccf4436dcc5",
        "order": 6,
        "width": 6,
        "height": 4,
        "gtype": "donut",
        "title": "Luminousity",
        "label": "lx",
        "format": "{{value}}",
        "min": 0,
        "max": "1000",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1470,
        "y": 580,
        "wires": []
    },
    {
        "id": "298062ff1bce572c",
        "type": "json",
        "z": "187b0bb9eda06271",
        "name": "JSON to String/Object",
        "property": "payload",
        "action": "obj",
        "pretty": false,
        "x": 320,
        "y": 220,
        "wires": [
            [
                "43b64526193c24c0"
            ]
        ]
    },
    {
        "id": "326407df7896fa4f",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 5,
        "width": 3,
        "height": 1
    },
    {
        "id": "ab4e8ece052ee7df",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 7,
        "width": 3,
        "height": 1
    },
    {
        "id": "7118a611b5e78c39",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 8,
        "width": 3,
        "height": 1
    },
    {
        "id": "5bd96b67d74d8666",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 9,
        "width": 3,
        "height": 1
    },
    {
        "id": "25d2d5596d5348c2",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 10,
        "width": 3,
        "height": 1
    },
    {
        "id": "a8ed22c0c0383de1",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "dcee4deb41a081e3",
        "order": 11,
        "width": 3,
        "height": 1
    },
    {
        "id": "76569fc26d088856",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 5,
        "width": 3,
        "height": 1
    },
    {
        "id": "958aabaf0ed9df4d",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 7,
        "width": 3,
        "height": 1
    },
    {
        "id": "df3831187c0a8804",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 8,
        "width": 3,
        "height": 1
    },
    {
        "id": "10eae83ae4ef8890",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 9,
        "width": 3,
        "height": 1
    },
    {
        "id": "86724259dec5773c",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 10,
        "width": 3,
        "height": 1
    },
    {
        "id": "56da73672c15615f",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 11,
        "width": 3,
        "height": 1
    },
    {
        "id": "552fe493d28b50b5",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 12,
        "width": 3,
        "height": 1
    },
    {
        "id": "3572c60d2d1c408d",
        "type": "ui_spacer",
        "z": "187b0bb9eda06271",
        "name": "spacer",
        "group": "73e85ccf4436dcc5",
        "order": 13,
        "width": 3,
        "height": 1
    },
    {
        "id": "f8031aa692189f38",
        "type": "serial-port",
        "serialport": "COM7",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "false",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    },
    {
        "id": "262512044f4f61ac",
        "type": "mqtt-broker",
        "name": "",
        "broker": "broker.emqx.io",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "921764b5c0c308e9",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "SWMS",
        "name": "SWMS",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "rejectUnauthorized": true
    },
    {
        "id": "73e85ccf4436dcc5",
        "type": "ui_group",
        "name": "Assignment",
        "tab": "c9feb0502bf4a2fb",
        "order": 1,
        "disp": true,
        "width": 12,
        "collapse": false
    },
    {
        "id": "dcee4deb41a081e3",
        "type": "ui_group",
        "name": "Assignment",
        "tab": "13ff37f4e3992732",
        "order": 1,
        "disp": true,
        "width": 12,
        "collapse": false
    },
    {
        "id": "c9feb0502bf4a2fb",
        "type": "ui_tab",
        "name": "SWMS",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "13ff37f4e3992732",
        "type": "ui_tab",
        "name": "Smart Weather Monitoring System",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

If your JSON is valid, there is no need for splitting and parsing. Just pass the JSON through a JSON node. That is all.

There are lots of online JSON checkers to verify.

So where in your code are you actually sending the data? I see a lot of code commented out....

I don't think it's MQTT it looks like it's going serial.

So that we can see your actual data, put a debug node on the output of your serial node. copy the value and paste it here.

ps I am not very familiar with Arduino code but it looks like you might need to add this after your serializeJson call
jsondoc.printTo(Serial)

That commented lines i just want to see if the JSON format is good to go, then i can straight away erase these lines.

It seems that my output on the first line is stacked together, I checked the output with a JSON editor online and it gives an error about I not quite understand what it means. Mind if you can point it out where my code should be changed or added?


#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>

#define DHTPIN 7 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);  

Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(9600); 
  dht.begin();

  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1);
  }
}
  
  
void loop() {
   float Temperature = dht.readTemperature();   
   float Humidity = dht.readHumidity();  

    if (isnan(Temperature) || isnan(Humidity)){
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
    float bmppressure = bmp.readPressure()/100.00;
    float bmpaltitude = bmp.readAltitude();

    unsigned int ldrValue;
    ldrValue = analogRead(A0);
    
    StaticJsonDocument<200> jsonDoc;
    jsonDoc["DHT11"]["Humidity"] = Humidity;
    jsonDoc["DHT11"]["Temperature"] = Temperature;
    jsonDoc["DHT11"]["TemperatureUnits"] = "Celcius"; 
    jsonDoc["BMP180"]["Altitude"] = bmpaltitude;
    jsonDoc["BMP180"]["Pressure"] = bmppressure;
    jsonDoc["LDR"]["LightLevel"] = ldrValue;
    serializeJson(jsonDoc, Serial);
    serializeJsonPretty(jsonDoc,Serial);
    delay(3000); 
}

Since you have a 3s delay between transmissions, you should set the serial config in node-red to release data after a delay. 500ms or 1000ms should easily be enough to split the strings correctly and only output 1 valid json string at a time.

Ps. Please don't ever post pictures of text. I have no ability to copy and paste and check it the json is valid for myself. Instead, always post code and logs in a ``` code fence ```

How do I do that? I only found the Default response timeout ___ms in the serial port node. I also getting errors in node red:
Ps. Thanks for the tip, Sorry for making everything messy.

[error] [json:JSON to String/Object] Unexpected end of JSON input
[error] [json:JSON to String/Object] Unexpected end of JSON input
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 9
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 17
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 10
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 14
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 7
[error] [json:JSON to String/Object] Unexpected token : in JSON at position 
[error] [json:JSON to String/Object] Unexpected token } in JSON at position 2

While the JSON output in Arduino im not sure whether is there anything is wrong after I input these into my void loop():

    StaticJsonDocument<200> jsonDoc;
    JsonObject dhtObject = jsonDoc.createNestedObject("DHT11");
    dhtObject["Humidity"] = Humidity;
    dhtObject["Temperature"] = Temperature;

    JsonObject bmpObject = jsonDoc.createNestedObject("BMP180");
    bmpObject["Altitude"] = bmpaltitude;
    bmpObject["Pressure"] = bmppressure;

    JsonObject ldrObject = jsonDoc.createNestedObject("LDR");
    ldrObject["LightLevel"] = ldrValue;
    Serial.println("\n");

    serializeJsonPretty(jsonDoc,Serial);

And my Arduino output will be like this:



{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 99.63486,
    "Pressure": 1001.33
  },
  "LDR": {
    "LightLevel": 836
  }
}

{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 100.1395,
    "Pressure": 1001.3
  },
  "LDR": {
    "LightLevel": 835
  }
}

{
  "DHT11": {
    "Humidity": 83,
    "Temperature": 26.3
  },
  "BMP180": {
    "Altitude": 99.21474,
    "Pressure": 1001.3
  },
  "LDR": {
    "LightLevel": 835
  }
}

"Will be" is ambiguous. I assume you mean this is what you are getting.

So with default response timeout 10000ms you are getting three copies of your data stacked up, which is invalid Json

If you set the default response timeout to 1000 do you get one copy and only one copy of the data in Node-red?

Please show us the data arriving in Node-red along with any errors.

Yeah i'm constantly getting this output in Arduino.

I've set the default response timeout to 1000ms and still getting the same number of errors in node-red

Show us a screenshot of your serial config in Node-red

So as I said, because you only send data every 3 seconds, try setting "Split input" option to "after silence" 500ms. Sorry, I don't remember the exact wording of the option.