arduinoJSON Parsing data garbage

Hi! im trying to sending data from arduino uno r3 to nodemcu using arduinoJSON 6.15.1 but its always failed, the arduino didnt sent the data.

i use this tutorial https://engineeringprojectshub.com/serial-communication-between-nodemcu-and-arduino/ and [Fully Explained]--Multiple Data--Serial Communication between Arduino and NodeMCU (ESP8266) - YouTube but the tutorial using arduinoJSON 5 and then i implemented to arduinoJSON 6. When i try use arduinoJSON 5 i got failed too, so i try to implemented arduinoJSON 6, but again, im failed too :frowning:

the connection is very ok! bcs when i tried to sent single data without arduinoJSON its working nicely, but when using arduinoJSON 6 for send multiple data its always failed.

can u help me?

here’s my arduino code

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial s(5,6);
 
void setup() {
  s.begin(1200);
}
 
void loop() {
  const size_t capacity = JSON_OBJECT_SIZE(2);
  DynamicJsonDocument doc(capacity);
  doc["data1"] = 100;
  doc["data2"] = 200;

  serializeJson(doc, s);
  delay(3000);
}

My nodeMCU code

#include <SoftwareSerial.h>
SoftwareSerial s(D1,D2);
#include <ArduinoJson.h>
 
void setup() {

  Serial.begin(9600);
  s.begin(1200);
  while (!Serial) continue;
 }
 
void loop() {
    const size_t capacity = JSON_OBJECT_SIZE(2) + 20;    
    DynamicJsonDocument doc(capacity);

    delay(3000);
    deserializeJson(doc, s);
   
    int data1 = doc["data1"]; // 100
    int data2 = doc["data2"]; // 200
    serializeJson(doc, Serial);
    
    Serial.println();
    }

and my result in serial monitor

null
{"data1":100,"data2":2000}
{"data1":100,"data2":2000}
{"data1":100,"data2":2000}
{}
null
100
"data2"
null
2000
{"data1":100,"data2":200}
{"data1":100,"data2":200}
{"data1":100}
null
{"data1":100,"data2":200}
{"data1":100,"data2":200}
{"data1":100}

Is there a reason you’re using JSON? It might be easier if you stored all your info in a struct. You could then send the struct between Arduinos reliably using a packetizing/parsing library such as SerialTransfer.h. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

Power_Broker:
Is there a reason you’re using JSON? It might be easier if you stored all your info in a struct. You could then send the struct between Arduinos reliably using a packetizing/parsing library such as SerialTransfer.h. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
myTransfer.begin(Serial1);
}

void loop()
{
char buff = “hi”;

myTransfer.txObj(buff, sizeof(buff));
myTransfer.sendData(sizeof(buff));
delay(100);
}





Example RX Arduino Sketch:


#include “SerialTransfer.h”

SerialTransfer myTransfer;

void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
myTransfer.begin(Serial1);
}

void loop()
{
if(myTransfer.available())
{
char buff[40];

myTransfer.rxObj(buff, sizeof(buff));

Serial.println("New Data: ");
Serial.write(buff, sizeof(buff));
Serial.println();

}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");

if(myTransfer.status == -1)
  Serial.println(F("CRC_ERROR"));
else if(myTransfer.status == -2)
  Serial.println(F("PAYLOAD_ERROR"));
else if(myTransfer.status == -3)
  Serial.println(F("STOP_BYTE_ERROR"));

}
}





For theory behind robust serial communication, check out the tutorials [Serial Input Basics](https://forum.arduino.cc/index.php?topic=396450.0) and [Serial Input Advanced](https://forum.arduino.cc/index.php?topic=662346.0).

i want to send multiple data using arduinoJSON to nodemcu, i will implement that code to my project which have two sensor, and then the data will be send to my nodemcu.

ok, i will try and learn about SerialTransfer.

btw sorry for my english, thank you sir!


i hve tried SerialTransfer, but i got an error when i try send data with variable

Arduino Code (tx)

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;
int data1 = 1;
int data2 = 2;

void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}


void loop()
{
  myTransfer.txBuff[0] = data1;
  myTransfer.txBuff[1] = data2;
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(2);
  delay(100);
}

my nodemcu code (rx)

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(D6, D5); // RX, TX
SerialTransfer myTransfer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}


void loop()
{
  if(myTransfer.available())
  {
      Serial.print("Data 1 ");
      Serial.write(myTransfer.rxBuff[0]);
      Serial.println();
      Serial.print("Data 2 ");
      Serial.write(myTransfer.rxBuff[1]);
      Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}