A Quick Overview of ESP8266 and NodeMCU

Thiago Valentin
11 min readOct 10, 2020

--

If you are a fan of IoT, microcontrollers and all that cool stuff, you may have stumbled across an ESP device, either an ESP8266 or the more recent version ESP32.

ESP8266 is a low-cost chip containing an MCU (microcontroller unit) with a built-in WiFi module. This is a perfect device for IoT — you’ve got controllability and connectivity in one piece.

ESPs are manufactured by Chinese company Espressif Systems. The first chip of the family came out in the second half of 2014, but today there are many many of them, most recently the ESP32 family.

ESP8266: a MCU + WiFi chip

Let’s focus on the ESP8266 today! This is the main chip shipped with the so called NodeMCU development board. This board usually goes for $5 - $10 CAD online and, as you can see, it is really cheap for what it offers. Resources are, of course, more limited than other MCUs in the market. My board comes with the following specs [1]:

  • Model: NodeMCU Amica
  • Onboard USB to Serial: CP2102
  • Clock: 80 MHz
  • Flash memory: 4 MB
  • SRAM: 64 KB
  • Power: 3.3 V
NodeMCU: a development board based on the ESP8266 chip

Here’s the catch: NodeMCU is the name of both the development board you’ve just seen and the firmware (one of them) that we can run on it.

While ESP8266 comes with 17 GPIOs, the NodeMCU dev boards do not expose all of them (some are not even recommended to be used). Take a look at the pinout below. This is the device I have at home and I have been playing with.

NodeMCU Pinout

Now that you’ve bought your NodeMCU, what can you do? There are various ways to program / control your device, so I will list just a few to start us off:

  • Arduino: a C++ based firmware that utilizes the easy-to-use Arduino interface, which in turns, is based of the Processing programming language;
  • NodeMCU: the aforementioned firmware, inspired by Node.js and implemented in Lua, while having an async event-driven architecture;
  • Tasmota: a firmware with HTTP and MQTT capabilities, very commonly used in home automation projects;
  • ESPHome: an alternative for controlling ESP8266 devices in home automation projects;
  • MicroPython: an embedded implementation of Python, exclusive to the family of ESP8266 devices.

Today we will cover the first three. Keep in mind that these are very different approaches and they will require different kinds of knowledge.

1. Arduino

By far, the easiest way to get started with ESP8266 / NodeMCU is using the Arduino IDE [3]. If you have used it before (to program Arduinos or any other devices), it will be a piece of cake! Even if you haven’t, it’s gonna be pretty quick to get it started.

Not only the NodeMCU dev board comes with both MCU and WiFi module, it also has an USB-to-UART converter (CP2102), which allows us to connect it directly to our computer through an USB cable. Besides, through the Arduino IDE, we can run a serial monitor, where we can communicate with the microcontroller using a serial port.

Here’s the step by step on how to run your code on your NodeMCU. As for our program, we will take on the hardware version of the “Hello World”: blink an LED.

  1. Download and install the Arduino IDE, if you haven’t done this yet. https://www.arduino.cc/en/Main/software
  2. Open the IDE and go to Arduino -> Preferences -> Additional Boards Manager URLs, click the button by the text box and add the following:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

3. Hit OK twice.

4. Go to Tools -> Board -> Boards Manager and search for “ESP8266”.

5. Find and install the ESP8266 package. At the time of this writing, I have the package esp8266 by ESP8266 Community version 2.7.4 installed.

6. Connect the NodeMCU board to your computer using an USB cable.

7. Go to Tools -> Board and find your module. In my case, I have a “NodeMCU 1.0 (ESP-12E Module)”.

8. Go to Files -> Examples and, under “Examples for NodeMCU 1.0 (ESP-12E Module)”, look for ESP8266 -> Blink.

9. Take a look at the sample code: it sets the LED_BUILTIN (= 12) as an output pin. Then, it executes the loop function indefinitely, turning the LED on for 1s and off for 2s. Note that the LED is activated when LED_BUILTIN is set to the LOW state (logic level 0).

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
}

10. Hit the Upload button on the top left corner (the right-pointing arrow). Wait until you are done uploading.

Upload code to NodeMCU using the Arduino IDE

11. Note that the console displays the log of the upload, including program storage space (24%), crystal frequency (26 MHz) and upload time (17.1 s). Also, at the bottom right corner, the IDE displays both the board name (Node MCU 1.0 / ESP-12E Module) and the serial port (/dev/cu.SLAB_USBtoUART) being used.

12. Voilà! The built-in LED on your NodeMCU starts blinking once your code is up and running!

2. NodeMCU

In order to flash NodeMCU (the firmware) onto your NodeMCU (the board), you need to build the binaries first. So let’s follow this three-stage approach:

  • build NodeMCU binaries;
  • flash NodeMCU binaries onto NodeMCU;
  • program NodeMCU using Lua.

As before, we shall follow a few steps.

Build NodeMCU

  1. Essentially, there are three ways to build the NodeMCU firmware binaries [4], but we will cover only one, the Linux Build Environment. For that you will need a machine running Linux, either local or remote.
  2. Clone NodeMCU Firmware project using your Linux machine:
$ cd ~/Documents$ git clone --recurse-submodules https://github.com/nodemcu/nodemcu-firmware.git

3. Change to git repository:

$ cd nodemcu-firmware

4. Activate Python Virtual Environment, if you are using one:

$ source ~/venv/bin/activate

Note: I am using Python 3.8. You may need to install / update python and its virtual environment, whose instructions might change slightly depending on the Linux distribution you are using.

5. Install esptool using pip:

$ pip install esptool

6. Compile the firmware into binaries using a Makefile:

$ make

Note: you may need to install further dependencies if this step fails. Take a look at the logs printed on the console to figure out what is missing at this point.

This step will create two binaries under the bin folder: 0x00000.bin and 0x10000.bin.

Flash NodeMCU

7. If you are using the same Linux machine to flash the firmware, skip to Step 11. If you are not, you have to transfer the file to your local machine (using SFTP, for example). First, go to your local machine:

$ cd ~/Documents/$ mkdir -p nodemcu-firmware/bin$ cd nodemcu-firmware

8. Download the recently compiled firmware pieces from your remote machine. You will need your username, hostname (or IP) and password for that:

$ sftp username@hostname-or-ip> cd Documents/nodemcu-firmware/bin> get *> CTRL + D$ mv 0x* bin/

9. As before, activate your virtual environment:

$ source ~/venv/bin/activate

10. Install esptool using pip. This is a Python-based tool to communicate with ESP’s ROM bootloader [5]:

$ pip install esptool

11. Connect the NodeMCU board to your computer using an USB cable.

12. Get the serial port name using esptool [6]:

$ esptool flash_id
Expected output:
esptool.py v2.8 Found 2 serial ports Serial port /dev/cu.SLAB_USBtoUART Connecting........ (...)

13. Flash the firmware onto ESP8266 [7]. Since we are using a NodeMCU dev board (and not the ESP8266 directly), there is no need to worry about putting the device into flash mode. Flashing the firmware consists of two parts (because there are two binaries):

$ esptool.py --port /dev/tty.SLAB_USBtoUART --baud 115200 write_flash -fm qio 0x00000 bin/0x00000.bin$ esptool.py --port /dev/tty.SLAB_USBtoUART --baud 115200 write_flash -fm qio 0x10000 bin/0x10000.bin

Program NodeMCU in Lua

14. Make sure that you have the latest version of Java (I am using Java 8 - OpenJDK 1.8):

$ java -version
Expected output: openjdk version "1.8.0_265" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed
mode)

15. You will need to install some dependencies as well. On my MacOS X, this is what I had to install using brew:

$ brew install telnet flex bison gperf libncurses5-dev libncursesw5-dev wget

16. Download and unzip ESPlorer from https://esp8266.ru/esplorer/#download -> ESPlorer Downloads -> ESPlorer.zip.

17. Run the ESPlorer.jar program:

$ cd ~/Downloads/ESPlorer$ java -jar ESPlorer.jar

18. ESPlorer’s interface is quite different from other IDEs, if you are not used to it. So let’s cover the very basics only.

ESPlorer for NodeMCU with program in Lua

19. On the right side panel, choose the right serial port (/dev/cu_SLAB_USBtoUART) and baud rate (115200), then click on the “Open” button to open a connection through the serial port.

20. Press the reset button on the dev board. The message displayed in the image above will show up. Note that it states cannot open init.lua. It means there is no Lua script file loaded at boot.

21. Copy and paste the Lua code below on the text editor on the left side panel (as illustrated above) [8]. This code, once running, will toggle the status of pin 4 every 1s.

status=0
pin=4
gpio.mode(pin, gpio.OUTPUT)tmr.create():alarm(1000, 1, function()
status = 1 - status
gpio.write(pin, 1 - status)
end)

21. At the bottom left, click “Send to ESP” and you should see the LED on your NodeMCU start blinking right away. Enjoy the show!

Note: if you send the code multiple times you will be creating “alarms” (event-based functions) multiple times, which will cause the blinking pattern to be altered. Instead, you have to press the reset button on the dev board before you upload a new snippet of code.

3. Tasmota

Now that you have two options to program your NodeMCU (C/C++ using the Arduino IDE or Lua using ESPlorer), let’s take a look at an alternative firmware called Tasmota that gives you some cool tricks out-of-the box, like accepting HTTP requests and implementing the MQTT protocol [9].

If you are dealing with home automation, this approach will come quite handy. And if you have ever used a Sonoff device, you will see the process to flash Tasmota is basically the same.

Without further ado, let’s get to our steps.

Flash Tasmota Firmware

  1. Download tasmotizer from GitHub [10] or install it using pip. Tasmotizer is the program we will use to flash Tasmota [11]. I will pick the latter option and install tasmotizer inside my virtual environment:
$ source ~/venv/bin/activate$ pip install tasmotizer

2. Run tasmotizer (in my case, I was able to find it under my virtual environment):

$ python ~/venv/lib/python3.8/site-packages/tasmotizer.py

3. In the Tasmotizer GUI, select the correct port to which your device is connected and select image “Release 8.5.1” (I chose the first option from the dropdown menu: tasmota.bin [596kB]). Make sure the option “Erase before flashing” is checked.

Tasmotizer is used to flash Tasmota on ESP8266 / NodeMCU

Note: an alternative at this point is to flash an image from a “BIN file” instead of “Release”. You would have to download the compiled version of Tasmota or download the project and compile it yourself.

4. Click “Tasmotize!” and the Tasmota firmware will be flashed onto your NodeMCU.

Note: esptool could also be used to flash Tasmota, as a replacement for Tasmotizer.

WiFi Configuration

5. Press the reset button on the NodeMCU board; it will reboot and create a WiFi network named tasmota_XXXXXX-####.

6. Use your computer, smartphone or tablet to connect to that network.

7. Open http://192.168.4.1 in a web browser.

8. Configure WiFi SSID and Password to match your WLAN credentials, then save it. NodeMCU will reboot and connect to your WiFi.

Configure WiFi for Tasmota using my smartphone

9. Back to Tasmotizer, hit “Get IP” to obtain the IP of the ESP8266 on your NodeMCU - it should be something like 10.10.10.37.

10. Now you can navigate to that IP using your browser to find this simple but friendly UI:

The Tasmota UI for Sonoff device

It’s cool to hit the toggle button and see your device switching on and off. As it turns out, this doesn’t do much for us. How cool would that be if we were actively controlling the power of a device?

Well, as you can see, “Sonoff Basic Module” reads on top. And that’s exactly what this firmware is intended for - to run on Sonoff modules, which are WiFi-controllable switches (an article on Sonoff is coming up soon). So get yourself a couple of them and play around with it. Try to send some HTTP requests, use terminal or even set up a MQ cluster that supports the MQTT protocol. As per the NodeMCU alone, what can you do with it?

Have fun with it. Be creative. Come up with nice projects. And let me know what cool ideas you made into reality…

My email is tvovalentin@gmail.com. Feel free to reach out! And I hope you’ve liked this article.

--

--