C. Hello World: The serial console
Every self-respecting programming tutorial starts with a "Hello World" program. And I don't
want to break with this tradition here. A Hello-World program usually does no more than
printing these two words somewhere on the screen. But we are programming a
microcontroller which does not have a screen yet. So where can we display the text? We will
use the Serial object to do that. While you are developing a program on the ESP8266, the
microcontroller is connected to the computer that the Arduino IDE is running on. We use this
connection to write a new binary onto the flash memory of the ESP8266. And while our
program is running we can also use it to write messages from the ESP8266 back to our
computer.
Using the Serial object is fairly easy. You have to initialize it first:
Serial.begin(115200);
This tells the Serial object that you want to communicate with a baud rate of 115200.
Remember to set the same transfer rate later in the serial console on your computer. Both
partners in the communication need to have the same speed settings or you will just see
garbage. If you want to send a message from your program to your computer you just do
this:
Serial.print("Hello ");
Serial.println("World");
Please have a look at the little difference between the first and the second line. The first uses
a method called
print()
and the second println(). The only difference is that the latter adds a
line break to the output.
Exercise 04.01: Hello world!
Now it is time to write our first program. Open the project Exercise_04_01 in your Arduino IDE
and fill in the required code to print "1. Hello World", "2. Hello World" etc. Remember that
you only need to initialize the Serial object once, while you'll have to print "<number>. Hello
world" as long as the code runs. Once you are happy with your solution upload the sketch to
your Arduino by clicking
If that was successful open the serial console by clicking on the magnifying glass:
Need help?
Do you have a question about the ESP8266 and is the answer not in the manual?