2

I am trying to send a Json Object from Arduino UNO to NodeMCU using SoftwareSerial. I am using Arduino IDE. I am getting garbage value on Serial monitor of NodeMCU. Tried it with different garbage values, couldn't receive data from Arduino UNO. Below is the code. PLease help. Thank you in advance. I haven't made a common ground. I have referred this webpage here.

Arduino UNO code:

 #include <SoftwareSerial.h>
 #include <ArduinoJson.h>
 SoftwareSerial s(5,6);
 void setup() {
 s.begin(9600);
 }
 void loop() {
 StaticJsonBuffer<1000> jsonBuffer;
 JsonObject& root = jsonBuffer.createObject();
 root["data1"] = 100;
 root["data2"] = 200;
 if(s.available()>0)
 {
 root.printTo(s);
 }
 }

NodeMCU code:

 #include <SoftwareSerial.h>
 SoftwareSerial s(D6,D5);
 #include <ArduinoJson.h>
 void setup() {
 // Initialize Serial port
 Serial.begin(9600);
 s.begin(9600);
 while (!Serial) continue;
 }
 void loop() {
 StaticJsonBuffer<1000> jsonBuffer;
 JsonObject& root = jsonBuffer.parseObject(s);
 if (root == JsonObject::invalid())
 return;
 Serial.println("JSON received and parsed");
 root.prettyPrintTo(Serial);
 Serial.print("Data 1 ");
 Serial.println("");
 int data1=root["data1"];
 Serial.print(data1);
 Serial.print(" Data 2 ");
 int data2=root["data2"];
 Serial.print(data2);
 Serial.println("");
 Serial.println("---------------------xxxxx--------------------");
 }
asked Apr 26, 2020 at 10:55
2
  • 1
    I haven't made a common ground. -- they maybe you should? majenko.co.uk/blog/importance-sharing-grounds Commented Apr 26, 2020 at 11:04
  • 1
    I tried making common ground too, same result Commented Apr 26, 2020 at 11:57

1 Answer 1

1

The problem with ArduinoJson.h is that it doesn't ensure robust and reliable transfer over UART serial.

Instead, you should use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace.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"));
 }
}
answered Apr 27, 2020 at 3:27
1
  • 1
    Thank you so much. It solved my problem. The transmission is proper now. Commented May 2, 2020 at 11:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.