please note: I am able to send and receive data from an Arduino Nano to a Mega. My issue is that the Nano is responding even when only the Rx/Tx lines are connected, but not power.
This bothers me because in the Mega I have a connection watchdog that sends a byte to the Nano and checks if it is bounced back. Now with power disconnected on the Nano I want the watchdog to tell me that the Nano is not connected. But it happily sends back the byte everytime!
Ports used on the Mega: Serial1 pin 18/19
Ports on the Nano 328P: Serial0 pin Rx/Tx
That seems odd. You could have the Nano respond to the Mega with a different byte and if you get back what you sent, you know it's a phantom and the Nano is powered down.
void RequestWatchdog(){
if (!RFID.watchdog.requested){
RFID.watchdog.requestTime = millis();
RFID.watchdog.requested = true;
serialNano.flush();
if (RFID.backDoorClosed) {bitSet(serBuffer,3);} // send the door status to the nano
else {bitClear(serBuffer,3);}
serialNano.write(serBuffer);
debug(F("Requesting rfidWatchdog..."),loglevel.toSerial);
}
}
void serialEvent1() //data from nano received
{
if (serialNano.available() > 0)
{
RFID.watchdog.status = true;
RFID.watchdog.requested = false;
RFID.serialData = serialNano.read();
delay(5);
debug(String(RFID.serialData),loglevel.toSerial);
if (RFID.serialData != serBuffer) // this is not the rfidWatchdog
{
RFID.readerError = bitRead(RFID.serialData,1);
RFID.masterAlarm = bitRead(RFID.serialData,2);
RFID.systemArmed = bitRead(RFID.serialData,3);
RFID.validKey = bitRead(RFID.serialData,4);
RFID.cardRead = bitRead(RFID.serialData, 5);
}
if(RFID.validKey){debug(F("RFID key valid."),loglevel.normal);}
if(RFID.cardRead && !(RFID.validKey)){debug(F("RFID: Invalid key"),loglevel.min);}
if(RFID.masterAlarm){debug(F("RFID Master ALARM"),loglevel.min);}
if(RFID.systemArmed){debug(F("RFID System Armed"),loglevel.min);}
}
}
Nano-Code:
void serialEvent() //Watchdog request from mega
{
byte watchDog;
if (Serial.available() > 0) {
watchDog = Serial.read();
Serial.write(watchDog); //bounce back
backDoorClosed = bitRead(watchDog,3); //Bit 3 holds the door info from the Mega
}
}
OK so now I have changed the response from the Nano to the Mega to:
watchDog = 255; //Serial.read();
Serial.write(watchDog); //bounce back
When the Nano is powered, I get 255. When it is not powered I get what I sent. I probably need to increment the number in the Nano to distinguish between phantom and real response.