Browse over 10,000 Electronics Projects

ESP8266 Programming Basics

ESP8266 Programming Basics


In this post we will have a look at the building blocks of an Arduino sketch. This will help you to build your own sketch quickly. The post covers the serial console, digitalRead and digitalWrite, interrupts, analogRead and finally WiFi, http and https.


[ Featured image Courtesy: Wikimedia under CC License ]


In this post we will work with exercises which you can download from GitHub. They contain several Arduino projects for the
ESP8266. For an exercise open the related project in your Arduino IDE and try to solve the given task. If you get stuck or want
to see an alternative solution open the project which ends with “_Solution”:

  • Exercise_04_01: contains the first exercise in chapter 4
  • Exercise_04_01_Solution: contains a possible solution

Now download the zip file from GitHub and extract it in a place you will find it later. There is a green “Clone or download”
button which lets you download a zip file:


The Arduino Sketch

The Arduino platform was built with the beginner in mind. Compared to a normal C program the Arduino IDE hides a few things from you to simplify the setup. First of all you do not have to create a make file to build your code into an executable binary. The Arduino IDE also includes a default header file for you: #include “Arduino.h”. This contains all definitions needed for a regular Arduino program.

Another important change compared to a regular C/C++ program are the two default functions setup() and loop(). The first will be only called once during startup while the loop() method will be called repeatedly. On a normal Arduino hardware (Atmega chip) you can theoretically write code and never leave the loop() method again. The ESP8266 is a bit different here. If your operations run for too much time a so called watchdog will reset the ESP8266. You can prevent this by allowing the controller to do important operations while you are still in the main loop. Calling yield() or delay(ms) will do this.


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 not more than printing these two words somewhere on the screen. But we are programming a micro controller 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 micro controller is connected to the computer 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:

1 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 this:

1 Serial.print("Hello "); 2 Serial.println("World");

Please have a look at a 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 later is adding a line break to the output.

Pages: 1 2 3 4