How to pass a jsonObject or string back and forth between arduino and node mcu? (bi-directional)

I tried some remedies for the same but didn’t worked out well so I need some more help with this in the form of code or anything that eases and helps me with the above question.
arduino.ino

#include <ArduinoJson.h>
#include <SoftwareSerial.h>
// 0 = Rx & 1 = Tx
SoftwareSerial nodemcu(0, 1);
String state = "";

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

void loop()
{
    DynamicJsonDocument doc(1024);
    doc["type"] = "arduino";
    serializeJson(doc, nodemcu);

    delay(3000);
    if (Serial.available()> 1)
    {
        DynamicJsonDocument doc(1024);
        DeserializationError error = deserializeJson(doc, Serial);
        if (error)
        {
          Serial.println("Invalid Json Object");
        }
        else
        {
          String type = doc["led"].as<String>();
          Serial.println("type = " + type);
        }
    }
}

node_mcu.ino

#include <ArduinoJson.h>
#include <SoftwareSerial.h>
// D6 = Rx & D5 = Tx
SoftwareSerial nodemcu(D6, D5);
String state = "";
void setup()
{
   Serial.begin(9600);
   nodemcu.begin(115200);   
}

void loop()
{
    DynamicJsonDocument doc(1024);
    doc["type"] = "nodemcu";
    serializeJson(doc, Serial);
  
    delay(1500);
    if (nodemcu.available())
    {
        DynamicJsonDocument doc(1024);
        DeserializationError error = deserializeJson(doc, nodemcu);
        if (error)
        {
            Serial.println(error.c_str());
        }
        else
        {
            String type = doc["type"].as<String>();
            Serial.println("type = " + type);
        }
    }
}

diagram:

This is an Uno? Pins 0, 1 are the hardware serial pins used by Serial.print() etc. You can't also use them as software serial for another device.

yes

Pick other pins for SoftwareSerial so you don't conflict with Serial.

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