Serial Monitor displays data out of sequence. Mega, mDot.

I'm using a Mega with an mDot Lora radio. When I send commands and requests to the mDot, the data is displayed in random order. Network Join is the last command in Setup, but all of the other Setup commands are displayed AFTER the Join and after the Loop has started. Any ideas?


Mega code

String response="";
String joinResponse="";
String message1="AT+SEND=Test Arduino";
String message2="AT+SEND=2.35 mgd";

void setup(){
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial.println("Begin setup");
  Serial1.println("AT+NJM=1");
  Serial1.flush();
  Serial1.println("AT+FSB=1");  //Sub band=1
  Serial1.flush();
  Serial1.println("AT+TXDR=DR2");
  Serial1.flush();
  Serial1.println("AT+NI=0,AABBCCDDEEFF0011");    
  Serial1.flush();
  Serial1.println("AT+NK=0,00112233445566778899AABBCCDDEEFF");   
  Serial1.println("AT+NK=1,MTCDT-19544846");
  Serial1.flush();
  Serial1.println("AT+JD=5");  //Join delay=5 for private LoRaWAN
  Serial1.flush();
  Serial.println("\n\nJoin network");
  Serial1.println("AT+JOIN");
  Serial1.flush();
  delay(15000);
}
  
void loop(){
  Serial.println("Start loop");
  Serial.println("Network Joined?");
  Serial1.println("AT+NJS?");
  Serial1.flush();
  delay(1000);
  while (Serial1.available()){
     joinResponse=Serial1.readString();
     delay(5000);
     Serial.print("Join Response:   ");
     Serial.println(joinResponse);
     Serial.flush();
     joinResponse="";
  }
  delay(1000);
  Serial1.println("AT+RSSI");
  Serial1.flush();
  delay(5000);
  while (Serial1.available()){
     response=Serial1.readString();
     Serial.flush();
     delay(5000);
     Serial.print("RSSI Response:   ");
     Serial.println(response);
     response=""; 
  }
  Serial.println("\n\nSend messages");
  Serial1.println(message1);
  Serial1.flush();
  Serial1.println(message2);
  Serial1.flush();
  Serial.println("End Loop");
  delay(60000);
}
  
------------------------------------------------------------------------------------------------------------
Serial Monitor output

Begin setup

Join network
Start loop
Network Joined?
Join Response:   AT+NJM=1

OK

AT+FSB=1

OK

AT

Send messages
End Loop
Start loop
Network Joined?
Join Response:   AT+SEND=Test Arduino

Network not joined

ERROR

AT+SEND=

Send messages
End Loop
[\Code]

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

…R

Yes, your code is very difficult to read, however, you are checking for presence of data in while ()
consequently skipping the while() until you get data.
Decide to continue let the loop() manage your code flow or take over.
With so many delays I would suggest YOU take charge of repeating the "loop".
And NO, at this point you do not need millis() , just different process flow.

Thanks. I never would have found it. I reformatted the code.

Robin2:
To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

…R

When I highlight all the instances of Serial.print in your program the messages in the serial monitor seem to occur in the proper order.

Can you post the order in which you think they should appear so that I can understand what is in your mind.

By the way it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R