ESP8266 - Code Structure
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | (Optional) 5V Power Adapter for ESP8266 | |
1 | × | (Optional) Screw Terminal Expansion Board for ESP8266 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Basic Structure
The ESP8266 code consists of two sections: the setup code and the loop code. The setup code is executed once, when the program starts. The loop code is executed continuously until the program is stopped.
Setup Code
- Code in the setup() function is executed right after power-up or reset. It is only executed one time and is used for initializing variables, pin modes, and starting to use libraries, etc.
Loop Code
- Code in the loop() function is executed right after the setup code. It is executed repeatedly, infinitely, and is used to do the main task of the application.
Example
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Copy the code and open it with the Arduino IDE.
- Click the Upload button in the IDE to transfer the code to the ESP8266.
- Open the Serial Monitor.
- Check out the output on the Serial Monitor.
You can observe that “This is the setup code” is printed only once, however “This is loop code” is printed multiple times. This implies that the setup code is run once, and the loop code is executed repeatedly. The setup code is executed first.
※ NOTE THAT:
The setup() and loop() functions MUST be included in ESP8266 code. Failure to do so will result in an error.
Optional Parts
: used to declare variables that can be used in all the sketch
Apart from setup and loop code, an ESP8266 sketch may comprise of the following sections:
- Block comment: generally used to include details about the author, wiring instructions, license etc. which are ignored by ESP8266.
- Libraries inclusion: used to include libraries in the sketch.
- Constant definition: used to define constants.
- Global variables declaration: used to declare variables which can be accessed throughout the sketch.
Detailed Instructions
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Copy the code and open it with the Arduino IDE.
- Click the Upload button in the IDE to send the code to the ESP8266.
- Open the Serial Monitor.
- Check the output on the Serial Monitor.
We do not require comprehension of code line-by-line at present. We just need to be familiar with the code structure. The line-by-line code will be elucidated in subsequent tutorials.