Reading Mixed Serial If/Else ? ?

Well you need to decide what happens if a command does not work - is there another command to send or does the full thing needs reboot (can you control power supply to do a hardware reset).

In one project I created a function testing various ways of resetting my device, all the way to switching off the power of my serial device and then rebooting the arduino (in that last case the call to the function never returns and your full project reboots). Let’s say this function is called progressiveReset() and if it neeeds context to take action you can pass as parameter the last failed command to that function (may be can try again). If it’s too complicated just reboot the whole thing :slight_smile:

I also created a function wrapping the call to waitForString() by also adding a c-String command to issue

boolean runCommand(const char * command, const char * endMarker, unsigned long duration)
{
   ESPSerial.print(command); // using print to not add arbitrarily the \r\n at the end. caller responsibility
   if (! waitForString(endMarker, duration)) progressiveReset(command); // die here possibly
  return true; // if we came back then the progressiveReset resolved the situation 
}

Now all the complexity is hidden in the progressiveReset() function and the main loop can look simple as you know if you come back from the call it means things got sorted out

void loop() {
   Serial.println(); // Clean up Serial Console
   runCommand("ATI\r\n”, “Elm327 v1.3", 5000ul); // Send the 1st command to get things rolling
   runCommand("AT AL\r\n”, “OK\r\n", 5000ul);
   runCommand("AT H1\r\n”, “OK\r\n", 5000ul);
   runCommand("AT MA\r\n”, “OK\r\n", 5000ul);
}

Just an idea