,Starting with ESP32 (Lint, PlatformIO,
FreeRTOS, etc)
Infos note
Références
Liens
ESP32 + Arduino (by SimplyExplained) - YouTube
Driver download links:
CH340 (Windows & Linux): How to Install CH340 Drivers - SparkFun Learn
CP210x (Windows, macOS & Linux): CP210x USB to UART Bridge VCP Drivers
- Silicon Labs
Arduino IDE:
Software | Arduino
Arduino Framework for ESP32 :
Video: GitHub - espressif/arduino-esp32: Arduino core for the ESP32
Espressif: Installing - - — Arduino ESP32 latest documentation
FreeRTOS API reference:
FreeRTOS API categories
Concepts clés / Synthèse
Sommaire de la note :
1. Infos note
1.1. Références
1.2. Liens
1.3. Concepts clés / Synthèse
2. ,RefactorInstall drivers for ESP32
3. ,RefactorArduino IDE set for ESP32
4. ,RefactorPlatform IO (with Visual Studio Code)
5. ,RefactorAdd a CC++ linter (static code analysis)
6. ,RefactorUse libraries
7. ,RefactorFreeRTOS
8. ,RefactorWiFi
9. ,RefactorOther
,Refactor_Install drivers for ESP32
,Refactor_Install drivers for ESP32
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Install drivers for ESP32
Install Serial Drivers for ESP32 (macOS, Windows, Linux) - YouTube
ESP32 has USB to UART
Depending on board : CH340 / CP210x
To install the drivers :
Download the latest drivers on the appropriate website (see links)
Go to Peripheral manager
Find the peripheral (shown with a warning sign as the driver is not
installed)
Choose Update the driver / Find and install the driver manually /
Navigate to where you extracted the zip file.
,Refactor_Arduino IDE set for ESP32
,Refactor_Arduino IDE set for ESP32
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Arduino IDE set for ESP32
Setting up Arduino IDE for ESP32 development (ESP32 + Arduino series) -
YouTube
Install the Arduino IDE (see links).
Install the Arduino Framework for the ESP32:
Video: On the GitHub page
Search for the "Latest Stable Release" (in Development Status)
Copy the link to package_esp32_index.json (in "Assets" on the
bottom of the page)~~
Open the Arduino IDE
Add the ESP32 framework link to the boards manager:
File / Preferences
Click on "Additional Boards Manager URLs"
Paste the following link into the box:
(Cf. video): The copied link.
*Arducam board (cf. Icam Obsidian):*
[Link]
age_ArduCAM_ESP32_PSRAM_index.json
*Espressif board (cf. Espressif):*
[Link]
esp32/package_esp32_index.json
Install the ESP32 board in the Arduino IDE:
Tools / Boards / Boards manager
Search ESP32 (in the "Filter your search" box)
Install the appropriate ESP32 board
Select the appropriate board and port:
Click on the dropdown menu and click on "Select other board
and port"
or :
Tools / Boards / ESP32: Select the appropriate board
Tools / Ports / Click on the appropriate port.
,Refactor_Platform IO (with Visual Studio Code)
,Refactor_Platform IO (with Visual Studio Code)
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Platform IO (with Visual Studio Code)
Note: When starting VSCode, you will have to click on the PlatformIO icon
to reload it.
Installation
Download and install VSCode : Download Visual Studio Code - Mac,
Linux, Windows
Open VSCode
Click on the Extension button on the right, search for "PlatformIO IDE",
click on the little blue "Install" button.
Configuration
Once installed, click on the PlatformIO icon on the left, and on Open
(under Quick Access / PIOHome):
Click on Platforms, then Embedded:
Enter Espressif in the Filter box, click on Espressif 32 then Install:
Click on Home, then New project:
Enter the project name, board (for instance uPesy ESP32 Wroom
DevKit), Framework (Arduino) and location:
Click Finish and wait...
SG: For my information:
What you get
The source files are in the src folder, the header files are in the include
folder:
To change the serial monitor baudrate, add this line to the
[Link] file:
1 monitor_speed = 115200
To upload the program, click on the Upload button (bottom):
If not visible, try clicking on the PlatformIO icon on the sidebar to
refresh the environment.
The serial monitor can be accessed by clicking on this icon:
,Refactor_Add a CC++ linter (static code analysis)
,Refactor_Add a CC++ linter (static code analysis)
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Add a C/C++ linter (static code analysis)
Default code analyser for Visual Studio Code (highly
recommended!)
Click on the Extension button on the right, search for "C/C++ Extension
Pack", click on the little blue "Install" button :
If not done, install the "C/C++ for Visual Studio Code" extension.
Click on the "Extensions" icon in the left-hand sidebar, search for
"C/C++", and click on the "Install".
If the default code analyser (Clang) keeps displaying an error due to
unfound header files (eg: "'stdbool.h' file not found"):
Click on the error.
Right click on the file not found (eg: <stdbool.h> in #include
<stdbool.h> ), and click on "Go to definition"
Copy the folder of the opened file (by doing "File / Save as ..." and
copying the folder in which the file is stored). Click cancel
Open the settings (File / Preferences / Settings).
Type clang args in the "Search settings" box.
Click on "Add Item" next to "C_Cpp > Code Analysis > Clang Tidy:
Args" :
Paste : --include-directory "{copied directory}"
eg: --include-directory
"C:\Users\[Link]\.platformio\packages\toolchain-xtensa-
esp32\lib\gcc\xtensa-esp32-elf\8.4.0\include"
Click OK and close the settings.
Additional code analyser : CppLint (up to you)
To go further with static analysis (and detect more potential bugs)
Install cpplint
Run in your terminal:
pip install cpplint
Copy the path where cpplint is installed (should be indicated in
the message: "WARNING: The script [Link] is installed in ...").
In case it is installed in a folder that is not in the path, do the
following:
Open the Start menu (Windows) or the terminal
(Linux/macOS) and search for "Environment Variables".
In the System Properties window, click on the "Environment
Variables" button.
In the Environment Variables window, under "User variables",
scroll down and find the "Path" variable.
Click on "Edit".
In the Edit Environment Variable window, click on "New" and
enter the path to the cpplint installation directory.
Click "OK" on all the windows to save the changes.
*Restart VSCode.*
To setup and run the linter on single files
Create the related task:
In VSCode, click on the "Terminal" menu and select "Configure
Tasks".
Type "create" then click on the "Create [Link] file from
template" option.
Select the "Others" option.
Paste:
{
"version": "2.0.0",
"tasks": [
{
"label": "lint-file",
"type": "shell",
"command":
"C:\\Users\\[Link]\\AppData\\Roaming\\Python\\Python311\\
Scripts\\cpplint",
"args": [
"--filter=-build/header_guard,-
whitespace/braces,-whitespace/blank_line,-whitespace/comments,-
readability/braces",
"--linelength=120",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "never"
},
"problemMatcher": [
{
"owner": "cpp",
"fileLocation":
["absolute"],
"pattern": {
"regexp": "^(.*):
(\\d*):\\s*(.*)\\[(\\d*)].*$",//^(.*):(\\d*):\\s*(.*)\\[\\d*]$
"file": 1,
"line": 2,
"message":3,
"severity": 4,
}
}
]
}
Replace
C:\Users\[Link]\AppData\Roaming\Python\Python311\Scri
pts\ by the actual path to cpplint.
Save it to your project's .vscode folder
Run the linter:
Open the file in the VSCode.
Open the command palette ("Ctrl+Shift+P"), type "task" and select
"Tasks: Run Task" from the list.
Select "lint-file" and press "Enter".
The linter will run and any issues it finds will be displayed in the
"Problems" tab in the bottom panel.
You can click on each issue to navigate to the corresponding line of
code.
To setup and run the linter on the whole project
Create the related task:
- Open the [Link] file.
- Add a new task (here is the file with both single file & whole project
tasks):
```
{
"version": "2.0.0",
"tasks": [
{
"label": "lint-file",
"type": "shell",
"command":
"C:\Users\[Link]\AppData\Roaming\Python\Python311\Script
s\cpplint",
"args": [
"--filter=-build/header_guard,-whitespace/braces,-
whitespace/blank_line,-whitespace/comments,-whitespace/parens,-
readability/braces",
"--linelength=120",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "never"
},
"problemMatcher": [
{
"owner": "cpp",
"fileLocation": ["absolute"],
"pattern": {
"regexp": "^(.):(\d):\s(.)\[(\d*)].$",//^(.):(\d):\s(.)\[\d]
"file": 1, "line": 2, "message":3, "severity": 4, } } ] }, { "label": "lint-project", "type": "sh
{workspaceFolder}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "never"
},
"problemMatcher": [
{
"owner": "cpp",
"fileLocation": ["absolute"],
"pattern": {
"regexp": "^(.):(\d):\s(.)\[(\d*)].*$",
"file": 1,
"line": 2,
"message":3,
"severity": 4,
}
}
]
}
]
}
Replace
C:\Users\[Link]\AppData\Roaming\Python\Python311\Scripts
\ by the actual path to cpplint.
To run the linter automatically on each file save:
Click on the Extension button.
Type "Trigger" and click on "Trigger Task on Save". Install it.
In the ".vscode" folder, create a "[Link]" file.
Paste:
{
"[Link]":
"C:\\Users\\[Link]\\AppData\\Roaming\\Python\\Python311\\
Scripts",
"[Link]": {
"lint-project": [
//"src/!ModuleVersion.h", // = except this
file
"src/*.cpp",
"include/*.h"
],
"[Link]": true,
}
}
Replace
C:\Users\[Link]\AppData\Roaming\Python\Python311\Scri
pts\ by the actual path to cpplint.
If not working
If the lines in [Link] are faded:
Pb: The [Link] project file might be overwritten by higher
scope settings in the workspace.
Solution: Try opening only the project (File / Open folder).
,Refactor_Use libraries
,Refactor_Use libraries
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Use libraries
Using the PlatformIO Library Manager (ESP32 + Arduino series) - YouTube
1. Accessing the Library Manager:
In VSCode, click on the PlatformIO icon in the sidebar.
Click on "PIO Home/Libraries".
There, you can search for, install, and manage libraries easily.
5. Searching for Libraries:
In the search bar, you can find specific libraries by name or
functionality.
PlatformIO has a vast repository of libraries for various purposes,
from hardware interfacing to network protocols.
8. Installing Libraries:
- To install it: Click "Install" to add it to your project.
- Ensure the installed libraries are correctly referenced in your
[Link] configuration file:
- When you install a library using the Library Manager, PlatformIO
typically updates the [Link] file automatically. It adds the
library to the lib_deps section, ensuring it is included in the build
process.
- The Library Manager helps manage library dependencies, ensuring
that all required libraries for a project are installed and up-to-date.
- Manually Adding Libraries to [Link] :
- You can edit the [Link] file directly. Add the library under the
lib_deps section like this:
`` ini [env:your_environment] platform = espressif32 board = esp32dev
framework = arduino lib_deps = adafruit/Adafruit SSD1306 @ ^2.4.7
adafruit/Adafruit GFX Library @ ^1.10.7
- *Replace `your_environment` with your environment
name, and ensure you use the correct library name and version.*
- *You can find the correct library names and versions
on the PlatformIO Library Registry or directly within the VSCode
Library Manager.*
4. **Using Installed Libraries**:
- Include the libraries in your source code by using `#include`
directives, such as:
```cpp
`#include <Adafruit_SSD1306.h>`
```
,Refactor_FreeRTOS
,Refactor_FreeRTOS
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
FreeRTOS
Installation
If using PlatformIO, there is no need to install anything (FreeRTOS files
are included within the platformIO package).
Use
Refer to the template file OS_tasks.cpp . In a word:
Create a task (= special function) that will be called upon a specific
period for instance.
eg in the template file: ```void OS_TSK_LedBlink(void
parameters)```*
Configure it in a function called during the global setup() function.
eg in the template file: xTaskCreate(
,Refactor_WiFi
,Refactor_WiFi
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
WiFi
Step-by-Step Guidelines for Connecting ESP32 to
WiFi
1. Install WiFi Library:
The WiFi library is typically included by default in the ESP32 Arduino
core.
If needed, add it to your [Link] :
1 [env:esp32dev]
2 platform = espressif32
board = esp32dev
3
framework = arduino
4
lib_deps =
5
WiFi
6
1.
Include WiFi Library in Your Code:
1 #include <WiFi.h>
2.
Define WiFi Credentials:
1 const char* ssid = "your_SSID";
2 const char* password = "your_PASSWORD";
1. Create Web Server Instance:
1 `WebServer server(80);`
1.
Setup WiFi Connection in setup() Function:
```cpp
void setup() {
[Link](115200);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(1000);
[Link]("Connecting to WiFi...");
}
[Link]("Connected to WiFi");
[Link]([Link]());
// Define routes
[Link]("/", handleRoot);
[Link]("/data", handleData);
// Start server
[Link]();
[Link]("HTTP server started");
```
2.
Define Request Handlers:
1 void handleRoot() {
2 [Link](200, "text/plain", "Hello, world!");
3
}
4
void handleData() {
5 String message = "ESP32 Data";
6 [Link](200, "text/plain", message);
7 }
1. Handle Client Requests:
1 void loop() {
2 [Link]();
}
3
Example Full Code
ESP32 code
1 #include <WiFi.h>
2 #include <WebServer.h>
3
const char* ssid = "your_SSID";
4
const char* password = "your_PASSWORD";
5
6 WebServer server(80);
7
8 void handleRoot() {
9 [Link](200, "text/plain", "Hello, world!");
}
10
11
void handleData() {
12 String message = "ESP32 Data";
13 [Link](200, "text/plain", message);
14 }
15
void setup() {
16
[Link](115200);
17
[Link](ssid, password);
18
19 while ([Link]() != WL_CONNECTED) {
20 delay(1000);
21 [Link]("Connecting to WiFi...");
22 }
23
[Link]("Connected to WiFi");
24
[Link]([Link]());
25
26 [Link]("/", handleRoot);
27 [Link]("/data", handleData);
28
[Link]();
29
[Link]("HTTP server started");
30
}
31
32 void loop() {
33 [Link]();
34 }
35
Accessing the Server
1. Connect your computer or device to the same WiFi network.
2. Open a web browser and enter the ESP32’s IP address (shown in the
Serial Monitor).
3. Access different routes:
For the root: [Link]
For data: [Link]
This setup creates a simple web server on the ESP32 that can send and
receive data through WiFi. You can expand this by adding more endpoints,
handling POST requests, and parsing JSON data for more complex
interactions.
,Refactor_Other
,Refactor_Other
Refactored note
Note source : ,Starting with ESP32 (Lint, PlatformIO, FreeRTOS, etc)
Date générée : 30/05/2024 @16:11
Other
An AI assistant
The extension Cody AI from Sourcegraph gives you an AI as an assistant
for your code. The assistant is aware of your code to help you thoroughly.
But be careful: it both consumes CPU power ... and a lot of energy (where
the AI is executed). So you use it to help you more efficiently (with the
planet in mind).