Programming ESP µC - Setup and program NodeMCU esp8266 using Visual Studio Code extension for Arduino

in utopian-io •  11 months ago 

Repository

https://github.com/esp8266/Arduino
https://github.com/Microsoft/vscode-arduino

What Will I Learn?

  • You will learn how to setup and configure Arduino VSCode extension for esp8266
  • You will learn how to check NodeMCU esp8266 specification directly. Useful if you want to avoid buying the wrong device.
  • You will learn program NodeMCU esp8266 using Arduino framework
  • You will learn how to do OTA update in esp8266 using Arduino VSCode extension

Requirements

Difficulty

  • Intermediate

Description

In this ESP video tutorial, we take our first steps into setup an effective environment for developing a firmware for esp8266 using Visual Studio Code extension for Arduino. We also take a look on how to check the board specification directly to see if there is a defects in the device we bought. At the end of this tutorial we try to upload our firmware over WiFi connection.

🤔 What is esp8266?

ESP8266 is a microcontroller chip for IoT use case. It base on Xtensa architecture which is pretty configurable and can operate on low power environment. There is many esp8266 development board out there but in this tutorial, I use NodeMCU v3 which is made by LoLin. When we bought it, we need to check if the board is accoding to the specification that is written in the board. To do that, we need to use esptool which will get the info about the flash memory, mac address, and the chip type.

access premission problems + esptool output

When we try to execute the esptool.py flash_id in the Linux base OS, we will have a problems accessing the USB port. This is because the USB port we are connected was registered in tty or dialout system groups which only root user allowed to read/write into the port. To change the access premission, we need to execute chmod 666 /dev/<connected USB port> to allow read/write into that port. Another way is adding us into dialout user by using usermod which will give us permanent access to the USB serial port even if we try to restart the OS. If you have more problems with esptool that are specific to Linux, you can consult it here.

🛠 Configuring Arduino Visual Studio Code extension

Arduino VSCode extension is Visual Studio Code extension to make using Arduino framework easy to develop, build, deploy and debug with a rich set of functionalities like autocompletion and integrated into Arduino debugging. To use vscode-arduino extension, first we need to install Arduino toolchain or IDE. Since we use Linux base OS (in my case is Ubuntu), we can install it using snap package manager by:

snap install arduino-mhall119 --classic

That's will install Arduino IDE for v1.8.5. There is a note that the vscode-arduino only support Arduino IDE v1.6.x to v1.8.6 and not later. Also, the Windows Store's version of the Arduino IDE is not supported because of the sandbox environment that the application runs in.

After we have install both Arduino IDE and Arduino VSCode extension, we need to configure the VSCode extension to point out where is the Arduino IDE located and this is mandatory if you use Windows version. We also need to add 3rd package url which point out to the index board of Arduino core for ESP8266 WiFi chip. In summary, we need to add this into our VSCode settings (settings.json):

{
    "arduino.path": "/snap/arduino-mhall119/current",
    "arduino.additionalUrls": "http://arduino.esp8266.com/stable/package_esp8266com_index.json"
}

Then, we can install esp8266 in the Arduino Board Manager 👇
Arduino Board Manager

Now we can open/create a new project and after we have open/create a new project, we need to set the board configuration to use NodeMCU v1 board. You can choose depend on the board you are using. In my case, though NodeMCU v3 LoLin is not same as NodeMCU v1/v2, we can still use NodeMCU v1 configuration because the only differences is in the pin layout and the usb2serial chip it use.

VSCode Arduino board configuration for NodeMCU v3 LoLin

This will generate .vscode/arduino.json file and it should look like this:

{
    "sketch": "<YOUR SKETCH NAME>.ino",
    "port": "",
    "board": "esp8266:esp8266:nodemcuv2",
    "configuration": "CpuFrequency=80,VTable=flash,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=115200"
}

🛰 OTA Update

Sometimes it can be quite painful to plug the device into USB port everytime we want to upload the sketch. Luckily, there is a way to upload it via wireless connection which called as OTA update. OTA (Over the Air) update is the process of loading the firmware to device using Wi-Fi connection rather than a serial port. Such functionality became extremely useful in case of limited or no physical access to the module. To enable OTA, we need to upload a firmware for basic OTA from Arduino example. After we have enable it, we also need to edit the arduino.json to use the IP Address of the device instead of the USB port.

{
    "sketch": "Blink.ino",
    "port": "192.168.100.4",
    "board": "esp8266:esp8266:nodemcuv2",
    "configuration": "CpuFrequency=80,VTable=flash,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=115200"
}

After we have edit it, the port status in the VSCode will show the IP Address instead of the USB port 👉 Selection_028.png

As long as the device is power on and connected to the WiFi, we can upload our sketch anywhere as long as the PC/Laptop connected to the same Access Point. The power supply of 3~5V with 1A or more is more than enough to power this device. In my case for NodeMCU v3 LoLin, for the sake of simplicity, I power it using powerbank with output 5V and the current is 1A.

NodeMCU powered with powerbank for OTA update

Video Tutorial

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hello @drensor,

Thank you for your contribution to the video tutorial. I enjoyed watching your video because I can see that you put effort in doing a good presentation. Here are some advice:

  • Try to make your tutorial more structured in the video teaching. This is often the most difficult part for a lecturer. You need to think from the learners point of view and create a very easy outline for them to follow from the beginning to the end or else they lose interest.

  • Take the students step by step in a clear manner so that they understand the concepts behind the steps that lead to the next one.

  • For the coding, it would be good if you can make the font sizes bigger because I had to squint my eyes to see the codes.

I look forward to your next video.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Hi @drsensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @drsensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!