Hi, I'm using an Arduino Mega and an Arduino Nano 33 IoT. I'm trying to get them to communicate using Serial1 on Mega and Serial on Nano but with no luck. I managed to get communication in the same way with a nodemcu/esp8266 but applying exactly the same method doesn't seem to work.
This is my simplified code on Nano:
#include <ArduinoJson.h>
#include <ezTime.h>
void setup() {
Serial.begin(115200);
while (!Serial and millis() < 5000);
}
void loop() {
int traincount=0;
char buffer[400];
// Print Time
sprintf(buffer, "%i\t%i\t%i\t%i\t%i\t%i", year(myTZ.now()), month(myTZ.now()), day(myTZ.now()), hour(myTZ.now()), minute(myTZ.now()), second(myTZ.now()));
Serial.print(buffer);
JsonArray depart = doc["departures"];
// Print the values in Json body from http request
for (JsonObject train : depart) {
if ( Direction_correct( train["display_informations"]["headsign"].as<String>() ) ){
String time = train["stop_date_time"]["arrival_date_time"];
sprintf(buffer, "\t%s\t%f\t%f", train["display_informations"]["direction"].as<const char*>(), CompareTime(time, 1), Compare2Times(time, train["stop_date_time"]["base_arrival_date_time"], 1)/5);
Serial.print(buffer);
traincount++;
if (traincount >= 3) {break;}
}
}
Serial.print("\n");
delay(30000);
}
This prints the correct data to my computers monitor, skipping lines and with correct tabs ("\t")
This is my code on the Mega which works with the nodemcu
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
while(!Serial or !Serial1);
}
void loop() {
receivefromwifimodule();
}
void receivefromwifimodule() {
while (Serial1.available() > 0) {
static String message;
char inByte = Serial1.read();
Serial.print(inByte);
if ( inByte != "\n") {
message += inByte;
}
//Full message received...
else
{
Serial.println();
Serial.println(message);
message = "";
}
}
}