[ESP32 most complete study notes (basic articles) - 3.ESP32 and ESP8266 VS Code and PlatformIO IDE development platform]

About this tutorial:

ESP32 Basics                                

1. Introduction to ESP32                                                                

2. ESP32 Arduino Integrated Development Environment

3. VS Code and PlatformIO ☑ 

4. ESP32 pins

5. ESP32 input and output

6.ESP32 Pulse Width Modulation

7. ESP32 analog input

8. ESP32 interrupt timer

9. ESP32 deep sleep

ESP32 Protocol

ESP32 web server

ESP32 LoRa

ESP32 BLE

ESP32 BLE Client-Server

ESP32 Bluetooth

ESP32 MQTT

ESP32 ESP-NOW

ESP32 Wi-Fi

ESP32 WebSocket

ESP32 ESP-MESH

ESP32 mailbox

ESP32 SMS

ESP32 HTTP get POST

HTTP GET Web APIs

HTTP POST Web APIs

 ESP32 server articles

Keep updating, follow bloggers and don't get lost! ! !

 ESP32 Sensor Module

Keep updating, follow bloggers and don't get lost! ! !

ESP32 Ultimate Combat

More than 100 ESP32 practical projects, please pay attention! ! !

        Learn how to program ESP32 and ESP8266 NodeMCU boards using VS Code (Microsoft Visual Studio Code) with the PlatformIO IDE extension. We describe how to install the software on Windows, Mac OS X or Ubuntu operating systems.

The Arduino IDE is great for small applications. However, for advanced projects with more than 200 lines of code, multiple files, and other advanced features such as auto-completion and error checking, VS Code with the PlatformIO IDE extension is the best choice.

In this tutorial, we'll cover the following topics:

Table of contents

Install VS Code (Visual Studio Code) on Windows 

 Install Python on Windows

Install the PlatformIO IDE extension on VS Code

VS Code quick interface overview

 PlatformIO IDE overview

create a new project

platformio.ini file

source folder

Use PlatformIO IDE to upload code: ESP32/ESP8266

detect serial port

troubleshooting

Change Serial Monitor Baud Rate – PlatformIO IDE

Install ESP32/ESP8266 library on PlatformIO IDE

Open the project folder

VS Code color themes

shortcut list

Summarize


Install VS Code (Visual Studio Code) on Windows 

        Go to Visual Studio Code - Code Editing. Redefined and download the stable version for your operating system (Windows).

Click the installation wizard to start the installation, follow all the steps to complete the installation. Accept the agreement and press the Next button.

Finally click Finish to complete the installation.

Open VS Code and you'll see a welcome tab with release notes for the latest version.

That's it. Visual Studio Code has been successfully installed. 

 Install Python on Windows

To program ESP32 and ESP8266 boards with PlatformIO IDE, you need Python 3.5 or higher installed on your computer. We are using Python 3.8.5.

Go to python.org/download and download Python 3.8.5 or the latest version.

Open the downloaded file to start the Python installation wizard.

The following window appears.

Important: Make sure to check the Add Python 3.8 to PATH option. You can then click the Install Now button. 

After successful installation, you will receive the following message.

You can click the close button.

Now, go to this section to install the PlatformIO IDE extension.

Install the PlatformIO IDE extension on VS Code

        ESP32 and ESP8266 boards can be programmed using VS Code with the PlatformIO IDE extension. Follow the next steps to install the PlatformIO IDE extension.

Open VS Code:

  1. Click the Extensions icon or press Ctrl  +  Shift  +  X to open the Extensions tab
  2. Search for "  PlatformIO IDE  "
  3. choose the first option
  4. Finally, click the Install button (Note: it may take a few minutes to install)

After installation, make sure the PlatformIO IDE extension is enabled as shown below.

 

After that, the PlatformIO icon should appear on the left sidebar, along with a Home icon that redirects you to the PlatformIO home page.

That's it, the PlatformIO IDE extension has been successfully added to VS Code.

If you don't see the PIO icon and Quick Tools at the bottom, you may need to restart VS Code for the changes to take effect.

Either way, we recommend restarting VS Code before proceeding.

VS Code quick interface overview

Open VS Code. The following print screen shows what each icon in the left column means and its shortcut:

  • file manager
  • Search across files
  • Source code management (using gist)
  • Start and debug your code
  • management extension

        Additionally, you can press Ctrl  +  Shift  +  P or go to View  >  Command Palette... to display all available commands. If you're searching for a command, but you don't know where it is or its shortcut, you just need to go to the command palette and search for it.

        At the bottom, there is a blue bar with PlatformIO commands.

        Here are the icon functions from left to right:

  • PlatformIO home page
  • build/compile
  • upload
  • Clean
  • serial monitor
  • new terminal

If you hover over the icons, it will show what each icon does.

Alternatively, you can click on the PIO icon to view all PlatformIO tasks. 

If the task doesn't show up on your IDE when you click the icon, you may need to click the three-dot icon at the top and enable the PlatformIO task, as shown below.

 PlatformIO IDE overview

        To give you an overview of how PlatformIO works on VS Code, we'll show you how to create, save and upload a "blinking LED" code to your ESP32 or ESP8266 board.

create a new project

On VS Code, click on the PlartfomIO home page icon. Click + New Project to start a new project.

         Give your project a name (eg Blink_LED) and select the board you are using. In our case we are using DOIT ESP32 DEVKIT V1. Framework should be "Arduino" to use the Arduino core.

You can choose a default location to save your projects or a custom location.

The default location is in this path Documents  >  PlatformIO  >  Projects . For this test, you can use the default location. Finally, click Finish.

         For this example we will be using the DOIT ESP32 DEVKIT development board. If you are using the ESP8266 NodeMCU board, the process is very similar, you just need to select your ESP8266 board:

The Blink_LED project should be accessible from the explorer tab.

        The folder structure of VS Code and PlatformIO is different from standard .ino projects. If you click on the explorer tab, you'll see all the files it created under the project folder. Seems like a lot of files to process. But don't worry, usually you only need to deal with one or two of these files.

WARNING: You should not delete, modify or move the folder and platformio.ini file. Otherwise, you will no longer be able to compile your project with PlatformIO.

platformio.ini file

The platformio.ini file is the PlatformIO configuration file for your project . It shows the platforms, boards and frames of your project. You can also add other configurations like libraries to include, upload options, change serial monitor baud rate and other configurations.

  • platform : Corresponds to the SoC used by the development board.
  • board : The development board you are using.
  • framework : The software environment that will run the project's code.

For ESP32 and ESP8266, if you want to use a baud rate of 115200 in the serial monitor, just add the following line to the platformio.ini file.

monitor_speed = 115200

Afterwards, make sure to press Ctrl  +  S to save the changes you made to the file.

In this file, you can also use the lib_deps directive, which we'll see later.

source folder

        The src folder is your working folder. Under the src folder, there is a main.cpp file. That's where you write your code. Click on the file. Arduino programs should be structured with setup() and loop() functions.

 In PlatformIO, all your Arduino code should start with #include <Arduino.h> .

Use PlatformIO IDE to upload code: ESP32/ESP8266

Copy the following code into your main.cpp file. 


#include <Arduino.h>

#define LED 2

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED, HIGH);
  Serial.println("LED is on");
  delay(1000);
  digitalWrite(LED, LOW);
  Serial.println("LED is off");
  delay(1000);
}

This code blinks the onboard LED every second. It works for both ESP32 and ESP8266 boards (both have onboard LEDs connected to GPIO 2). 

We recommend that you copy this code manually so you can learn about the IDE's autocompletion and other interesting features. Also, if there is a syntax error somewhere in your program, it will even be underlined in red before compilation.

After that, press Ctrl  +  S or go to File  >  Save to save the file.

Now you can click the "Upload" icon to compile and upload the code. Alternatively, you can go to the PIO Project Tasks menu and select Upload .

If the code uploaded successfully, you should receive the following message.

After uploading the code, the onboard LED of the ESP32 or ESP8266 should blink once every second.

Now, click on the serial monitor icon and you should see it printing the current LED status.

NOTE: If you don't see a Terminal window, go to the menu Terminal > New Terminal.

detect serial port

PlatformIO will automatically detect which port your board is connected to. To check connected devices, you can go to the PIO home page and click on the device icon.

troubleshooting

If you get the following error when trying to upload code: "Failed to connect to ESP32: Timed out waiting for packet headers ", it usually means that your board was not in blink mode when you uploaded the code. 

When this happens, you need to press the ESP32 onboard BOOT button when you start seeing a lot of dots in the debug window.

If you don't want to have to press the BOOT button every time you upload new code, you can follow this guide: [SOLVED] Failed to connect to ESP32: Timed out waiting for packet headers.

Change Serial Monitor Baud Rate – PlatformIO IDE

The default baud rate used by PlatformIO is 9600. However, different values ​​can be set as previously described. Under your project folder in File Explorer, open the platformio.ini file and add the following line:

monitor_speed = baud_rate

For example: 

 monitor_speed = 115200

 After that, save the file.

Install ESP32/ESP8266 library on PlatformIO IDE

If you need to install libraries in PlatformIO IDE, follow the next procedure.

Click the home icon to go to the PlatformIO home page. Click on the library icon in the left sidebar .

Search for the library to install. For example Adafruit_BME280 .

Click on the library to include in the project. Then, click Add to Project .

Select the project to use the library.

This will use the lib_deps directives on the platformio.ini file. If you open your project's platformio.ini file, it should look like the image below.

Alternatively, in the library window, if you select the Install tab and scroll a bit, you'll see the library's identifier. You can choose any of these identifiers depending on the options you want to use. Library identifiers are highlighted in red.

 Then, go to your project's platformio.ini file and paste the library identifier into it, like this:

lib_deps = adafruit/Adafruit BME280 Library@^2.1.0

 If you need multiple libraries, you can separate their names with commas or put them on separate lines. For example:

 lib_deps =

arduino-libraries/Arduino_JSON @ 0.1.0

adafruit/Adafruit BME280 Library @ ^2.1.0

adafruit/Adafruit Unified Sensor @ ^1.1.4

 PlatformIO has a built-in powerful library manager which allows you to specify custom dependencies for each project in the project configuration file platformio.ini , using lib_deps . This will tell PlatformIO to download automatically when you save the configuration file or compile the project library and all its dependencies.

Open the project folder

To open an existing project folder on PlatformIO, open VS Code, go to PlatformIO Home and click Open Project . Browse for the files and select your project folder.

PlatformIO will open all files in the project folder.

VS Code color themes

VS Code allows you to choose between different color themes. Go to the Admin icon and select Color Themes . You can then choose from several different light and dark themes.

shortcut list

For a complete list of VS Code shortcuts for Windows, Mac OS X or Linux, check out the next tutorial:

  • VS Code keyboard shortcuts reference.

Summarize

        In this tutorial, you learned how to install and prepare Visual Studio Code for use with the ESP32 and ESP8266 development boards. VS Code with the PlatformIO IDE extension is a great alternative to the classic Arduino IDE, especially if you are developing more advanced code for larger applications.

Guess you like

Origin blog.csdn.net/m0_46509684/article/details/129105522