ESP8266MCU and the Seral Monitor

I am programminig a LOLIN D1 Mini ESP8266MCU with the IDE 2.3.2. All Sketches so far have worked exxcept for a problem with the serial monitor. I have the Serial monitor set at a baud rate of 115200 Baud, the same as the sketch, and have experimented with different baud rates. The problem is that any Serial.print command in the Setup results in garbage in the monitor but prints fine if the Serial.print command appears in the Loop. The following sketch results in the following monitor output:

#include <ESP8266WiFi.h>
void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Hello World!");
}
void loop() {
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  delay(2000);
}

�������������������������ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8
ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8
ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8
ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8
ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8

I have tried delaying the after Serial.begin() and while! plus lots of other stuff suggested on the interweb, but nothing so far will allow Serial.print in the setup function.

Hi @steveinaustria. This is actually expected. When the ESP8266 starts up, its ROM bootloader prints some diagnostic information. This data is printed at 74880 baud. So when you have the Serial Monitor set to a different baud rate (which is the correct thing to do in this case since your sketch communicates at 115200 baud) it is displayed as some garbage. You can simply ignore this garbage as the diagnostic information is only useful in special cases where the ESP8266 is misbehaving (e.g., resetting due to a watchdog timer timeout).

Yes, I realise that and the data sent at 74880 doesn't bother me. What does bother me is that the other Serial.print data in the setup function is totally ignored, for example the "Hello World!" in the example above never appears in the serial monitor.

Edit. I have just noticed that after uploading the sketch I only get garbage in the monitor and no "Hello World!" but if I then reset the ESP8266 I get the garbage and then the "Hello World!" But I don't want to reset after every upload.

Edit2. Today the reset trick doesn't work. I will try and work out how I got that working and report back. Found it, see my reply to ptillisch below.

I understand now.

What happens if you add a line break before the problematic Serial.println call?:

  Serial.println();
  Serial.println("Hello World!");

Without that, the "Hello World!" is printed at the end of the garbage line. Maybe there is something about that line that causes the text to go missing. If so, adding a sacrificial Serial.println call before the important one should cause the "Hello World!" to be printed in Serial Monitor as expected.

It makes no difference, "Hello World" doesn't appear in the monitor which jumps straight to "ESP8266 Board MAC Address:,,,,,"

Try waiting for Serial to be ready. I use the following, not perfect but normally ok

while (!Serial && (millis() < 5000)) ;   // Wait up to 5 secs for the Serial device to be ready

This only works for boards that have a native USB CDC serial port. It doesn't do anything on boards like the ESP8266 boards that use a separate USB to serial bridge chip:

Ok, what about

while (!Serial.availableForWrite() && (millis() < 5000)) ;   // Wait up to 5 secs for the Serial device to be ready

Serial.availableForWrite() is not useful for this application:

https://www.arduino.cc/reference/en/language/functions/communication/serial/availableforwrite/

Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation.

Since you haven't written anything, the serial buffer is empty and so the function will return the size of the buffer. Any non-zero value is evaluated as true, so (in the intended usage context) that code will be equivalent to:

while (!true && (millis() < 5000)) ;   // Wait up to 5 secs for the Serial device to be ready

I can reproduce your problem under the following conditions

  1. Serial monitor closed
  2. Upload sketch
  3. Open serial monitor

If the serial monitor is open while uploading, the problem does not occur and I see the "Hello world" message.

At the end of an upload you get the following message

Leaving...
Hard resetting via RTS pin...

When serial monitor is opened, it asserts DTR; I'm not sure if it asserts RTS as well. And RTS seems to be the signal that reset the ESP8266.

So if RTS is not asserted when serial monitor is opened, the board will not reset and the output that you see will be from where the code is (somewhere in loop()).

Board package used: 3.1.2
IDE used: 1.8.19

Following up, the following code demonstrates

#include <ESP8266WiFi.h>
void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("Hello World!");
}

void loop()
{
  Serial.println(millis());
  delay(2000);
}

Output if serial monitor is opened after the upload

09:40:26.203 -> ⸮⸮  ⸮   ⸮⸮ ⸮ ⸮⸮⸮ ⸮   ⸮ ⸮⸮ ⸮ ⸮⸮             ⸮  ⸮   ⸮   ⸮⸮ ⸮ ⸮⸮  ⸮19078
09:40:29.936 -> 21079
09:40:31.930 -> 23079

Output after upload if serial monitor was open during the upload

09:38:57.040 -> ⸮⸮  ⸮   ⸮⸮ ⸮ ⸮⸮⸮ ⸮   ⸮ ⸮⸮ ⸮ ⸮⸮             ⸮  ⸮   ⸮   ⸮⸮ ⸮ ⸮⸮  ⸮Hello World!
09:38:57.859 -> 1078
09:38:59.856 -> 3078

Note the first printed value of millis() in both scenarios; in the first output I did wait a little before opening serial monitor.

Good moring, thanks for your help with this. I just found out that if I include the line break as you suggested, the "Hello World!" is stil missing after an upload BUT after then pressing the reset button on the ESP8266 I still get the 74880 garbage but it is followed by the "Hello World!" message. This doesn't happen without the line break.

I can't help thinking this is a big clue to those that know the workings of the ESP8266 and the serial monitor better than I do.

I could see that you deleted a reply. I guess that you found the "Hello world" in the second output :slight_smile:

Yes, my bad!

I cannot replicate sterretje's experience with having the monitor open on upload but I have found a sort of solution to my problem. If I upload the sketch and then reset the ESP8266 the Serial.print() in the setup function appears in the serial monitor. Below is a sketch and its output after upload and then after reset.

#include <ESP8266WiFi.h>

// After upload press the RESET button on the ESP8266 to read the MAC Address.

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
void loop() {
}

�������������������������{ll��|�d�|�l�#|����s�b�c��go�dog���cx��l;l{lp�n��l��cn�|���b��og�l��l �ee'od`ags�ۓn#l�adp�g�r��ܜ��#g�|�#��ng�d`�eno$`g{���o#��`;��gc�� ���d`��o�ad����o�{��o<�$�l b�e�<s�d�g��g�l`��{�l�le��
ESP8266 Board MAC Address:  C4:D8:D5:12:B8:B8

I am able to reproduce this. The problem was solved for me by increasing the delay from 1000 ms to 5000 ms.

The serial port control signal assertion behavior of Serial Monitor can be configured for each board:

https://arduino.github.io/arduino-cli/latest/platform-specification/#serial-monitor-control-signal-configuration

The Serial Monitor control signal assertion is disabled in all the ESP8266 board definitions: