Want to make an IoT project, but doesn’t know how to start? Here is the answer.
NodeMCU is a five-dollar open-source IoT platform based on the ESP8266 Wi-Fi system on a chip. Version 3 runs on the ESP-12E (ESP8266MOD) module and it’s easy to use development board equipped with analog and digital pins, a USB-to-serial adapter based on the CH340g module, and a micro USB socket.
By default, it runs Lua scripts, however, the Arduino ecosystem is more suitable for beginners because of tons of information and libraries available. Or, if you’ve already started building a device with Arduino and need some Wi-Fi capabilities, it’d be easier to migrate your codebase to the ESP8266.
Regular coding for Arduino happens in the Arduino IDE, despite there is a more powerful solution: PlatformIO — a free and open-source ecosystem for IoT development. Here are some pros:
- Faster compiling
- Powerful library manager
- A convenient way to organize your code
- IDE customization and plugins
Having these three whales: hardware, software ecosystem, and IDE — will help you to a quick start with IoT and go beyond :)
NodeMCU v3 (ESP8266)
The development board comes with built-in firmware providing the possibility to control the Wi-Fi chip with AT commands. Just unwrap your brand new NodeMCU board and plug it into your computer with a USB cable, it will blink with blue LED a couple of times.
It uses a CH340g chip to convert the serial interface to the USB, which means your computer needs drivers to work with the board. Windows 10 machine can automatically find and install appropriate drivers, but if you need here is a driver to download (WCH is a Chinese company that designed this chip).
To check that the board working, we’ll pass some commands. You can use Serial Monitor from Arduino IDE or whatever serial terminal you like, for example, Bray’s Terminal.
Find port detected and connect to it with
9600 baud rate. Tap on the RST button to reset the development board and some “junk” should appear in the receive window:
Play with baud rate (e.g.
115200) to get a line saying “ready”. My boards worked with
115200 by default:
CR+LF line endings and send
AT command, it should reply with
Now we are able to play with other commands such as:
AT+RSTrestarts the module
AT+GMRchecks version information
AT+CWMODE?returns current mode: 1 — station, 2 — soft access point, 3 — station + soft AP
AT+CWMODE=3sets station + soft AP mode (restart module with
qwerty12password, replies with
WIFI GOT IPmessages after the connection established
AT+CIFSRreturns connection info
You can also establish a TCP connection with other devices in your network or find more commands in the ESP8266 AT Command Examples document. The board will say
ERROR, if the command you sent is not supported.
Let’s move on to the IDE. Proceed with the simple steps suggested on the PlatformIO website: Installation.
PlatformIO, in fact, is a Visual Studio Code extension, so what you need is to install the VS Code and add the
Give it a time to do some installation magic, it’ll also pull the C/C++ extension (
ms-vscode.cpptools), restart VS Code in the end and that’s it — you’re able to code!
Despite some tutorials say it’s needed to erase/flash the board with appropriate tools, I found it unnecessary when working with PlatformIO. You also not required to mess with Arduino IDE board manager as well!
Just click on the PlatformIO icon on the left bar and select New project:
Name your project, select NodeMCU 1.0 (ESP-12E Module) board and Arduino framework, use default project location or not:
Click on Finish and let it download all dependencies and build the project structure. Check the
It is the main configuration file you’ll be interested in:
framework parameters tell PlatformIO compiler what to use to compile the project. You can also specify here project dependencies (libraries and versions), serial monitor baud rate, and many other things. More details available here: Project Configuration File.
Let’s try to build the project, find the PlatformIO: Build button (checkmark icon) on the bottom bar, and click it to launch the build process. If everything is fine, you’ll get the same output in the terminal window:
I haven’t managed to make built-in LED work on my boards, but perhaps you’ll be luckier: it should be attached to the
LED_BUILTIN pin. Let’s blink the world:
Click on the PlatformIO: Upload button (right arrow) to build and upload firmware to the board. Blue LED will blink until the process completed.
External LED wiring scheme, if you need:
Now we can test Wi-Fi capabilities and find out how to add libraries. I suggest the WiFiManager library which provides a simple API to configure your module connection from the browser.
Go to the Libraries registry and search for
Click on the WifiManager by tzapu library and then on the Installation tab, it will show ways to require the library in your
lib_deps = tzapu/WifiManager @ ^0.16.0 to the
platformio.ini file and modify the
Upload the project and open the PlatformIO: Serial Monitor window, it will initialize the Wi-Fi manager and start HTTP server:
Go to Wi-Fi settings on your device and connect to the access point created by the board.
If it’s an Android device, you’ll be asked to “sign in to Wi-Fi network”. In fact, it’ll move you to the HTML page served by the module. If not, you can open a browser and type in
AP IP address received in the Terminal window (
Type SSID and password of your local network, click on Save and check the Terminal window:
If the connection is successful, you’ll see the
Idle message, which means the
loop() function started its work. From that point, you’re good to go with the Internet capabilities you need in the Arduino-friendly environment!
The final project you can find on GitHub: https://github.com/loginov-rocks/NodeMCU-Arduino-PlatformIO
Good luck with IoT projects!