On this tutorial, I’ll present you the best way to Program ESP8266 utilizing VS Code and PlatformIO. I’ll take you thru all the required steps to efficiently program ESP8266 NodeMCU with VS Code. If you’re an Embedded Software program Developer engaged on totally different platforms, then this tutorial will probably be very helpful to you.
IMPORTANT NOTE: All of the set up steps that I offered listed here are for Home windows Working System. If you’re excited about studying the process for different working techniques like macOS and Linux, then I’ll attempt to add them.
Putting in VS Code (Visible Studio Code)
Visible Studio Code or just (and popularly) generally known as VS Code, is a free and open-source Code Editor from Microsoft which runs on all of the three main working system platforms: Home windows, Linux and MacOS.
If you’re a Software program Developer, then VS Code is without doubt one of the greatest instruments on the market with assist for various languages like C++, C#, Python, Java and so forth., and quite a few extensions for keymaps, debuggers, code evaluation instruments and so forth.
To obtain VS Code, go to “https://code.visualstudio.com/Download” and choose the installer in your OS. I chosen the Home windows 64-bit installer and downloaded an executable known as “VSCodeUserSetup-x64-1.53.0”. On the time of scripting this tutorial, the most recent (and steady) model of VS Codes was 1.53.0.
Double click on on the downloaded executable to start out the set up process. First, the license web page pops up and it’s important to comply with the license phrases.
Click on on subsequent and on the following web page, you possibly can choose the vacation spot folder for the set up. I left it as default, which was “C:UsersTraiBlazerAppDataLocalProgramsMicrosoft VS Code”. If you wish to set a unique location, you possibly can change it right here after which click on on subsequent.
The subsequent web page lets you add some further duties like including desktop icon, including actions to home windows explorer and so forth. I counsel you to verify the “Add to PATH (requires shell restart)” possibility. You too can create a desktop icon if you need. Click on on subsequent after choosing the required choices.
VS Code installer will verify all of the choices you chose and if you wish to modify any of them, then you possibly can return and make the adjustments. If not click on on “Set up” to start set up.
Now, the set up of VS Code will begin.
As soon as the installer completes the set up of VS Code, it’ll show a message saying “Setup has completed putting in Visible Studio Code in your pc”. You possibly can click on on “End” to launch the VS Code App.
You have got efficiently put in Visible Studio Code and that is how the Residence Display seems like with a “Welcome” web page. You possibly can reduce or shut the app in the meanwhile.
Putting in Python
In an effort to program ESP8266 utilizing VS Code and PlatformIO, you must set up Python in your pc. Nicely, this not completely true. Your pc will need to have Python put in if you wish to use the PlatformIO Core.
As we’re going to use the PlatformIO IDE Extension for Visible Code Studio, you don’t have to put in Python solely. However none-the-less, will clarify the steps to put in Python on a Home windows machine, simply in case.
Go to “https://www.python.org/downloads/” and click on on “Obtain Python 3.9.1”. It’s going to obtain the 64-bit executable for Home windows. PlatformIO Core requires Python Model 3.5 or larger. On the time of scripting this tutorial, the most recent model of Python was 3.9.1. If you wish to obtain for an additional OS, then choose the suitable OS talked about within the obtain web page.
Double click on on the executable file, which was “python-3.9.1-amd64” in my case to start the set up process. Within the first display itself, you get a “Set up Now” button. However earlier than that be sure that to verify the “Add Python 3.9 to PATH” possibility on the backside. Now you possibly can click on on the Set up Now button.
This can provoke the provoke the set up course of.
As soon as the Python set up is full, you get a “Setup was profitable” message. Optionally, if you need, you possibly can disable the trail size restrict after which click on on shut.
If you wish to verify whether or not Python was efficiently put in in your pc, then click on on Begin in Home windows and kind “cmd” to seek for Command Immediate. Click on on the Command Immediate App.
Within the command immediate, simply sort python to provoke the interpreter. If Python was efficiently put in as the trail is efficiently added, then you’ll get a response with the Python Model quantity and “>>>”.
You efficiently put in Python.
NOTE: To exit the Python, click on Ctrl+z after which hit enter.
Putting in PlatformIO IDE Extension on VS Code
PlatformIO is skilled software for Embedded Software program Builders. It’s a cross-platform, cross-architecture IDE with assist for various SDKs, debuggers, unit testing, static code evaluation and plenty of different options.
It’s a very great tool for embedded engineers who work on totally different platforms for business merchandise because it reduces the time-to-market.
PlatformIO IDE is IoT centered built-in improvement atmosphere that comes as an extension to many code editors and different improvement instruments like Eclipse, Atom, Vim, Visible Studio and VS Code.
As we’re utilizing the VS Code in our tutorial, allow us to see the steps for putting in PlatformIO IDE in VS Code.
First, open VS Code (if it’s not already open). Choose the 5th icon within the left facet bar, which is a brief lower to open Extensions. Now, within the search tab, sort “platformio ide” and choose PlatformIO IDE from the outcomes. A brand new editor opens on the suitable associated to PlatformIO IDE. Click on on Set up button.
On the time of this tutorial, the most recent model of PlatformIO IDE was 2.2.1. You’ll get an Output window on the backside saying “Putting in PlatformIO IDE. . .”. Wait till it downloads and installs the required recordsdata.
After few seconds, you’ll get a message saying “PlatformIO IDE has been efficiently put in”. I strongly suggest you to click on on “Reload Now” to reload the window and in addition restart VS Code.
When you restart the VS Code, you’ll get a brand new icon on the left facet bar. That is the alien brand of PlatformIO IDE. Click on on this icon and click on on Open underneath PIO Residence choice to open the PlatformIO Residence. Alternately, it’s also possible to click on on the “Residence” icon on the backside standing bar.
A Transient Overview of PlatformIO IDE
Now, allow us to perceive the structure of PlatformIO IDE. The primary is the PlatformIO Fast Entry Panel on the left. This comprises all of the actions associated to PlatformIO IDE like Boards, Units, Debugging, Undertaking associated choices like Construct, Add, Serial Monitor and so forth.
The second is the shortcut icons on the PlatformIO Residence like Residence, Tasks, Platforms, Units and so forth. Lastly, the final merchandise is the precise editor itself.
Tips on how to Program ESP8266 utilizing VS Code and PlatformIO?
Making a New Undertaking
We’ve got efficiently put in VS Code and Platform IO on our pc. However the enjoyable half i.e., the precise programming of ESP8266 utilizing VS Code and PlatformIO hasn’t even begun but. So, let’s proceed with that.
First, within the PlatformIO Residence (open it if it’s not open already) click on on “+ New Undertaking” button on the suitable.
This can open a Undertaking Wizard, the place you possibly can customise your mission. First, present an acceptable identify to your mission. I named it “PlatformIO_Demo”. Subsequent is the board. PlatformIO helps over 960 boards from varied producers. As we’re working with ESP8266 and that too NodeMCU model of it, choose that board by typing NodeMCU.
You’ll get two choices NodeMCU 0.9 (ESP-12) and NodeMCU 1.0 (ESP-12E). Choose NodeMCU 1.0 (ESP-12E) as most newer boards are upgraded. Lastly, the framework. Choose Arduino framework and click on on end.
The PlatformIO extension will now attempt to obtain all the required frameworks, SDKs, Toolchains required in your board. It is a time taking course of and is perhaps a few minutes. Sit again and wait.
As soon as the recordsdata are downloaded and the mission is setup, you’ll get a mission explorer menu within the explorer tab on the left.
Construction of PlatformIO Undertaking in VS Code
A typical PlatformIO Undertaking consists of three principal parts:
- “lib” folder containing all of the mission particular libraries
- “src” folder containing all of the supply recordsdata
- “platformio.ini” file containing mission configuration
There are additionally different recordsdata and folders however these three are vital. Your software code is written in “principal.cpp” file within the src folder. This file comprises the setup () and loop() features of your Arduino sketch.
The platformio.ini is a Undertaking Configuration File containing mission associated
- Construct choices like construct flags, supply filter
- Add choices like customized add port, pace and additional flags
- Library choices like dependencies, additional library storages
- Superior choices like additional scripting
For our mission, the platformio.ini file is initialized with the next:
- platform = espressif8266
- board = nodemcuv2
- framework = arduino
WARNING: Don’t delete platformio.ini file. Modify it if and provided that you recognize what you’re doing. This file is vital for PlatformIO IDE to work correctly.
One potential modification to platformio.ini file you possibly can carry out is so as to add the baud fee for serial communication. By default, it’s set to 9600. However ESP8266 boards, together with NodeMCU works at 115200 baud fee. In an effort to change it, append the next line on the finish of platformio.ini file and save the file (Ctrl+s).
Add Program to ESP8266 NodeMCU utilizing PlatformIO IDE
Open the principle.cpp file from the src folder and modify the code with the next code.
IMPORTANT NOTE: You’re required to incorporate Arduino header file in the principle supply code (principal.cpp)
On this code, I’m blinking the on-board LED linked to GPIO16. Additionally, utilizing serial monitor, I’m printing the standing of the LED (simply to check the Serial Monitor in PlatformIO IDE).
As a substitute of copying and pasting the above code, attempt to write the code as you possibly can expertise the VS Code Editor and the way it helps in auto-filling and ideas.
After writing the code and saving it (Ctrl+s), open PlatformIO (by clicking on the PlatformIO icon on the left). A PROJECT TASKS will seem on the left. Broaden the env:nodemcuv2 possibility and in addition the Basic tab.
You possibly can see mission associated choices like Construct, Add, Monitor (to open Serial Monitor) and so forth. Click on on “Construct” choice to compile the code and verify for any errors.
If the PROJECT TASKS possibility shouldn’t be seen, then click on on three dots subsequent to PLATFORMIO on the highest and choose PROJECT TASKS.
One other strategy to Construct (or carry out different mission associated choices) is to make use of the PlatformIO shortcuts within the blue standing bar within the backside.
These shortcuts within the order are:
- Serial Monitor
- New Terminal
NOTE: The primary two are Errors and Warnings.
If in case you have not linked the ESP8266 NodeMCU board to the pc, then now’s the time to take action. After connecting the board, you possibly can add the code both by clicking on the Add shortcut on the backside or from the PROJECT TASKS.
PlatformIO will as soon as once more compile the code, generate a binary file and add the binary file to the flash reminiscence. You possibly can see the progress within the terminal.
NOTE: If terminal shouldn’t be open otherwise you closed it and need to open it once more, then within the menu bar, click on on Terminal choose New Terminal possibility. You too can use the shortcut on the backside.
In our program, we set the board to print out the standing of the LED to the Serial Monitor. Click on on the shortcut on the backside or choose Monitor from the PROJECT TASKS possibility on the left to open serial monitor.
NOTE: Should you haven’t modified the baud fee in platformio.ini file, change it to 115200. After making adjustments, restart the terminal to see the output.
In Arduino IDE, it’s important to set the COM port of the board to efficiently add this system. However PlatformIO IDE will mechanically handle COM port. You possibly can verify the COM port by going to PIO Residence editor and choosing the Units icon on the underside.
Moreover, you possibly can manually set the COM port by appending “upload_port = COM#” to the platformio.ini file.
It is a full step-by-step tutorial on the best way to program ESP8266 utilizing VS Code and PlatformIO IDE extension. You discovered all of the steps associated to putting in VS Code and PlatformIO, creating a brand new mission and configuring PlatformIO in your mission and in addition importing the code to NodeMCU board.