ESP8266 - if(Serial) is always true

I wanted to detect that the serial monitor has been opened before continuing with the rest of the sketch. So I included this which I got from an example:

void setup() {
Serial.begin(74880);
while(!Serial) {
}

But that didn't appear to be working. It goes breezing through the while loop and starts printing whether the monitor has been enabled or not. In fact, when I disconnect the USB cable, and power the D1 Mini from an external power supply, the entire sketch works perfectly even though nothing is connected to Tx or Rx.

So it seems that after Serial.begin, Serial is always true, and the ESP8266 just transmits all the print commands whether anyone is listening or not.

Is there any automatic way for the sketch to detect whether the monitor has been opened? Something that would involve a handshake perhaps?

Generally, Arduinos and their kin don't care whether there's anything listening on Serial, so you would have to build a mechanism yourself. An easy way would be to wait until something has been sent to the Arduino, so try would open the serial monitor (which resets the board) and send something (anything?) to tell the processor it's time to do some work.

The serial monitor when opened resets the Arduino via the RST or in some cases the DTR line, if you used a FTDI / TTL to Serial converter you could read the RST or DTR ('preferably RST') line and know when a new connection is created.

only MCU with native USB know about USB connection status. on esp8266 board a separate chip handles the USB

You might be seeing log_x, where x can be 'N', 'E', 'W', 'I', 'D', or 'V', entries, depending on your debug settings.

The log_x will print to the serial monitor, even if you have not set up the serial monitor.

A log entry would look like:

[I][ESP32_LIDAR_3.ino:521] fGetIMU():  aXbias = 0.000084, aYbias = 0.000848, aZbias = 0.001423

The indicates the lowest debug level that the log will print, the message source and message are, also, displayed.
If you debug level is set to debug and you are receiving log messages, you may want to read them to see if they mean anything.
The nice thing about using the log_x to print info is that, if you do set up serial, and use the serial plotter, log_x information is not picked up by the serial plotter.
You will, also, find that printing by way of the log_x is faster than using Serial.pintXXXXXX( "XXXXXX", nnn );

Opening the serial monitor does not reset my Lolin D1 Mini. When I open the monitor, it starts displaying whatever the sketch is serial-printing at that point. It doesn't reboot or start over. So that's why I was hoping to find a way to have it loop in a while statement until the monitor is opened. Of course I could use a long delay, or have it wait for input from me, but something automatic would be better. And as far as I know, you can't open the monitor in advance - something has to be plugged in first.

Well I think the answer is there's no way to do it automatically. The ESP8266 sets up the serial connection, and will print to it, but doesn't know or care whether anyone is listening. On the bright side, when I move the Mini into the mailbox -where there is no serial monitor - I don't have to remove all the Serial stuff from my sketch.

KawasakiZx10r:
The serial monitor when opened resets the Arduino via the RST or in some cases the DTR line, if you used a FTDI / TTL to Serial converter you could read the RST or DTR ('preferably RST') line and know when a new connection is created.

I think you meant RTS not RST.
RTS is an output from the host to the device and CTS would be the input from the device back to the host that would be read.

The history of Arduino autoreset is twisted and a bit ugly.
Some of this is intertwined with windows mishandling DTR and RTS in some versions of the OS.
Even linux did some mishandling DTR and RTS on the FTDI chips in early version of the driver years ago.

The early FTDI cable brought out the RTS signal but not the DTR signal.

But DTR is what the Arduino boards originally used for Auto reset because the OS will drop DTR when the serial port is opened, and raise it back up when the serial port is closed, without the application having to do anything.
This allowed autoreset to work for uploading code with no s/w changes needed to avrdude.

Some/newer versions of the IDE toggle RTS during an upload so that autoreset works with devices (like the FTDI cables) that have RTS wired up for auto reset instead of DTR.

While there may be some Arduinos out there that use RTS and control CTS I've not seen one.
And even if they did, it isn't clear how you would read that with the core supplied serial libraries.

--- bill

In the case of the Lolin D1 Mini ESP8266 board, the situation is further complicated because both RTS and DTR are used in a two-NPN arrangement that allows the various programming pins to be set automatically so flashing can take place without having to press a button. So the usual DTR -> capacitor -> Reset circuit isn't there. I don't know what the CH340 driver does to RTS and DTR when the port is opened, but it clearly isn't enough to reset the Mini.

ShermanP:
Is there any automatic way for the sketch to detect whether the monitor has been opened? Something that would involve a handshake perhaps?

Have you looked at the wireless serial/upload/logging interface?
I have used it, but I have not checked to see if it supports an "online"/ready status or not.

--- bill