I have some design questions involving what will be an intermittent direct serial connection between an Arduino Mega 2560 and Wemos D1 Mini. They will not always be connected, but when they are connected then data passed via the serial connection.
Serial3 on the Mega. SoftwareSerial on the Wemos. Using a logic level convertor between Mega and Wemos and all grounds are common.
I have questions about is the status/behavior of the serial ports when they are NOT connected.
Question #1:
What happens when I run Serial.begin(57600) in setup() on either device while not connected? Will the ports get initialized even though disconnected and begin working once they are connected?
Question #2:
How to detect when they are connected?
Method A:
Use a ping style test serially sending the ping from one (or both) devices to the other one. When a ping response appears then I know they are connected.
Method B:
Detect electrically when the two are connected using a 3rd pin or perhaps even detecting the serial pins are connected somehow.
My preference would be some implementation of Method B because using the ping test seems a bit brute force to me, plus I don't know if there might be some problems with writing to serial ports for hours on end that aren't connected to anything.
What I have tried:
I tried using a 3rd pin to allow the Wemos to know when its connected (see snippet below). When Mega-pin8 is connected to Wemos-pinD4 then D4 is brought LOW. But when the 3rd pin is connected, the Wemos Tx no longer is detected on the Mega. It somehow screws up just the Tx on the Wemos. I am totally in the dark as to why that happens.
Wemos:
void setup(){
pinMode(D4, INPUT_PULLUP);
...
}
void loop() {
if (digitalRead(D4) == LOW) isConnected = true;
else isConnected = false;
...
}
Mega:
void setup(){
pinMode(8, OUTPUT);
digitalWrite(8,LOW);
...
}