// https://arduinojson.org/v6/how-to/do-serial-communication-between-two-boards/
#include <ArduinoJson.h>
void setup() {
// Initialize "debug" serial port
// The data rate must be much higher than the "link" serial port
Serial.begin(115200);
while (!Serial) continue;
// Initialize the "link" serial port
// Use a low data rate to reduce the error ratio
Serial1.begin(9600);
// Check if the other Arduino is transmitting
if (Serial1.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, Serial1);
if (err == DeserializationError::Ok)
{
// Print the values
String cmd = 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 (Serial1.available() > 0)
Serial1.read();
}
}
}
void loop() {}