// https://arduinojson.org/v6/how-to/do-serial-communication-between-two-boards/
#include <ArduinoJson.h>
String cmd2;
void setup() {
// The data rate must be much higher than the "link" serial port
Serial.begin(9600);
while (!Serial) continue;
// RECEIVING MODE:
// Check if the other Arduino is transmitting
if (Serial.available())
{
// Allocate the JSON document
// This one must be bigger than the sender's because it must store the strings
StaticJsonDocument<300> doc;
// Read the JSON document from the "link" serial port
DeserializationError err = deserializeJson(doc, Serial);
if (err == DeserializationError::Ok)
{
String cmd2 = doc["cmd"];
//Serial.print("cmd = ");
//Serial.println(cmd);
}
else
{
// Print error to the "debug" serial port
//Serial.print("deserializeJson() returned ");
//Serial.println(err.c_str());
// Flush all bytes in the "link" serial port buffer
while (Serial.available() > 0)
Serial.read();
}
}
if (cmd2 =="get_status") {
// SENDING MODE:
// Create the JSON document
//String json; // no need for this code
StaticJsonDocument<300> doc;
JsonObject obj = doc.createNestedObject("status");
obj["Camera"]= "tCam-Mini-EFB5";
obj["Model"]=262402;
obj["Version"]="2.0";
obj["Time"]="17:33:49.0";
obj["Date"]="2/3/21";
// Send the JSON document over the "link" serial port
serializeJson(doc, Serial);
// another way for Sending the JSON document:
/*
String json;
// Send the JSON document over the "link" serial port
serializeJson(doc, json);
Serial.println(json);
*/
}
}
void loop() {}