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.
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.
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:
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
Serial monitor closed
Upload sketch
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()).
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 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��cn�|���b��og�l��l �ee'od`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