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: