update: 更新环境搭建章节

This commit is contained in:
DuRuofu
2024-12-30 21:38:51 +08:00
parent 06bcb53ab5
commit aa9f2428d9
34 changed files with 370 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
# 一、ESP32是什么
ESP32是一款由乐鑫Espressif公司研发的高性能、低功耗Wi-Fi和蓝牙双模芯片。它专为物联网IoT设计集成了丰富的功能模块和硬件接口是目前最流行的物联网嵌入式开发平台之一。
### 1.1 ESP32的核心特点
### 1.2 ESP32的应用场景
ESP32凭借其功能强大且成本低廉的特点已成为物联网开发的首选平台。
---
# 二、为什么要学习ESP32
学习ESP32不仅是掌握一项技能更是打开物联网开发大门的关键。以下是学习ESP32的几大优势
---
# 三、怎么学习ESP32
学习ESP32可以从以下几个步骤入手循序渐进地掌握从入门到精通的开发技能。
# 四、 相关资源与学习路径
- **官方文档**[Espressif 官方文档](https://docs.espressif.com/)
- **开发者社区**[乐鑫论坛](https://bbs.espressif.com/)
- **教程与示例**[GitHub - ESP-IDF Examples](https://github.com/espressif/esp-idf/tree/master/examples)
- **硬件资源**建议购买官方或认证的ESP32开发板。

View File

@@ -0,0 +1,334 @@
# 一、Windows系统
## 1.1 使用VS Code插件直接安装
在Windows系统下使用VS Code插件直接安装ESP-IDF可以参考官方文档[Install ESP-IDF and Tools](https://docs.espressif.com/projects/vscode-esp-idf-extension/zh_CN/latest/installation.html),下面介绍安装过程:
安装VS Code并安装ESP-IDF插件
![](attachments/20241230173816.png)
按Ctrl+Shift+P打开命令行输入`Configure ESP-IDF Extension`
![](attachments/20241230174252.png)
选择`ESP-IDF Configure ESP-IDF Extension`这一项来配置插件,选择后页面如下图所示:
![](attachments/20241230174358.png)
这里我们选择Express快速安装。
![](attachments/20241230180528.png)
第一项为下载服务器选项,推荐使用 Espressif 下载服务器。
第二项为ESP-IDF版本建议选择V开头的正式发布板本教程使用V5.2.3版本。
第三项ESP-IDF源码位置请选择不带空格的路径且对应文件夹需要为空。
第四项ESP-IDF 工具 位置,要求同上,**但不能与第三项相同**
这里我们选择在D盘根目录下新建`Espressif`文件夹。
然后点击安装即可,如下图所示:
![](attachments/20241230175629.png)
等待十几分钟即可安装完成(网络差可能会久一点),安装完成后如下图所示:
![](attachments/20241230184622.png)
至此开发环境安装完成,我们可以打开一个示例工程编译,验证安装是否完成。
点击shown examples 选择刚才下载的IDF版本
![](attachments/20241230185003.png)
随便选择一个示例工程这里以blink工程为例
![](attachments/20241230184954.png)
点击新建示例项目,选择一个存储位置:
![](attachments/20241230185234.png)
VS Code会自动打开项目目录如下
![](attachments/20241230185528.png)
点击扳手图标构建项目:
![](attachments/20241230185634.png)
编译停止后命令行显示下列内容则开发环境安装无误。至此ESP32的开发环境就准备完毕了。
![](attachments/20241230190036.png)
至于进一步烧录代码至开发板,将在下一节介绍。
## 1.2 手动安装
Windows系统下手动安装可以参考官方文档[Windows 平台工具链的标准设置](https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32/get-started/windows-setup.html#),下面简单介绍安装过程:
打开下面的链接,下载 ESP-IDF 工具安装器
>https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32/get-started/windows-setup.html
![](attachments/20241230181401.png)
选择自己需要的版本这里以V5.2.3版本为例(后续教程也使用该版本)
![](attachments/20241230181701.png)
下载完成后打开esp-idf-tools-setup-offline-x.x.x.exe程序即可开始安装
![](attachments/20241230195856.png)
选择安装路径:
![](attachments/20241230195935.png)
选择需要的组件和驱动程序(建议全选):
![](attachments/20241230200003.png)
等待安装完成即可,安装完成如下图:
![](attachments/20241230200620.png)
下面编译一个示例工程以确保ESP-IDF可以正常工作示例代码位于`安装目录\frameworks\esp-idf-v5.2.3\examples`,如下图:
![](attachments/20241230201034.png)
复制`examples/get-started`下的`blink`点灯工程到桌面(或其他位置),在安装完成后弹出的命令行中切换到`blink`目录,并运行`idf.py build`命令:
![](attachments/20241230201518.png)
运行后编译完成输出如下则开发环境安装无误至此ESP32的开发环境就准备完毕了。
![](attachments/20241230202342.png)
**但如果要使用VS Code插件功能还需要继续下面的步骤可选不使用插件进行开发也完全没有问题**
依然是安装VS Code并安装ESP-IDF插件
![](attachments/20241230173816.png)
按Ctrl+Shift+P打开命令行输入`Configure ESP-IDF Extension`
![](attachments/20241230174252.png)
选择`ESP-IDF Configure ESP-IDF Extension`这一项来配置插件,选择后页面如下图所示:
![](attachments/20241230174358.png)
这里我们选择第三个选项,使用现有设置,设置`Enter ESP-IDF directory (IDF_PATH)`为安装根目录下的`frameworks\esp-idf-v5.2.3`目录,设置`Enter ESP-IDF Tools directory (IDF_TOOLS_PATH)`为安装根目录,如下图(本教程中安装根目录为`c:\Espressif\V523`
![](attachments/20241230203618.png)
点击安装等待十几分钟安装完成即可要注意的是虽然是离线安装但这个步骤依赖网络下载python环境相关的工具容易失败失败很大可能是网络引起的不妨换个网络环境重新尝试😂
![](attachments/20241230203707.png)
安装成功显示如下:
![](attachments/20241230204358.png)
# 二、Linux系统
## 2.1 使用VS Code插件直接安装
这种安装方法和1.1介绍的Windows下使用VS Code插件直接安装没有太大区别参考1.1部分和官方文档中针对Linux系统的提示即可这里就不展开描述了。
建议直接参考官方文档:[Install ESP-IDF and Tools](https://docs.espressif.com/projects/vscode-esp-idf-extension/zh_CN/latest/installation.html)进行安装但相比于在Linux下使用VS code安装笔者更建议直接手动安装。
## 2.2 手动安装
Linux系统下使用ESP-IDF更推荐手动安装因为Linux系统下我们使用更多的是命令行VS Code插件提供的可视化操作可有可无。
这部分同样可以优先参考官方文档:[Linux 和 macOS 平台工具链的标准设置](https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32/get-started/linux-macos-setup.html),下面就安装过程进行简单说明:
### 2.2.1 准备linux的电脑或者虚拟机
教程使用Ubuntu22.04的虚拟机环境以安装v5.1.2为例,**注意本教程后续使用v5.2.3进行教学**,读者下载时建议下载新的`v5.2.3`版本。
![](attachments/20240125114613.png)
### 2.2.2 下载ESP-IDF包环境
下载ESP-IDF包环境这里有两种下载方式直接使用`git clone`命令下载或者手动前往ESP-IDF仓库下载
**使用`git clone`下载 (由于网络问题,容易下载失败)**
在终端运行命令:
```sh
mkdir -p ~/esp
cd ~/esp
git clone -b v5.1.2 --recursive https://github.com/espressif/esp-idf.git
```
**手动下载**
前往ESP-IDF仓库https://github.com/espressif/esp-idf/releases/tag/v5.1.2 ,如下:
![](attachments/20240125120321.png)
找到: `Download an archive with submodules included `一项
![](attachments/20240125120428.png)
下载并解压到 ~/esp/esp-idf 即可
> PS这里也可以安装多个版本在这个目录中不会冲突
### 2.2.3 安装依赖项
编译 ESP-IDF 需要以下软件包。请根据使用的 Linux 发行版本,选择合适的安装命令。
- Ubuntu 和 Debian:
```sh
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
```
- CentOS 7 & 8:
```sh
sudo yum -y update && sudo yum install git wget flex bison gperf python3 python3-setuptools cmake ninja-build ccache dfu-util libusbx
```
- Arch:
```sh
sudo pacman -S --needed gcc git make flex bison gperf python cmake ninja ccache dfu-util libusb
```
### 2.2.3 安装 ESP-IDF 使用的各种工具
这里使用全部安装:
```sh
cd ~/esp/esp-idf
./install.sh all
```
ESP-IDF 工具安装器会下载 Github 发布版本中附带的一些工具,如果访问 Github 较为缓慢,可以设置一个环境变量,从而优先选择 Espressif 的下载服务器进行 Github 资源下载。
```sh
cd ~/esp/esp-idf
export IDF_GITHUB_ASSETS="dl.espressif.com/github_assets"
./install.sh
```
这一步可能会遇到python下载失败的问题
![](attachments/20240125161324.png)
需要修改镜像源:
Linux下修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:
```ini
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
```
> 如果还失败就去修改:~/.config/pip/pip.conf 这个配置文件
### 2.2.4 配置快捷指令
在终端执行`. $HOME/esp/esp-idf/export.sh` 这个脚本就可以在当前终端使用esp-idf工具
但是每次执行都要先使用这个脚本激活环境不太方便,可以为执行 `export.sh` 创建一个别名
打开`.bashrc` 配置文件
```
nano ~/.bashrc
```
写入配置文件:
![](attachments/20240125164001.png)
我这里安装了两个版本,于是写两条命令
![](attachments/20240125164248.png)
### 2.2.5 测试编译
打开历程目录下的hello_world程序
![](attachments/20240125165114.png)
使用`idf.py build `命令编译代码,显示下面的内容则编译成功
![](attachments/20240125165709.png)
至此Linux下开发环境安装完成。
# 三、(进阶)Windows下使用ssh调用Linux系统下的开发环境
在Windows下使用VS Code通过SSH调用Linux系统的开发环境可以有效提升编译速度下面是详细的步骤
## 3.1 安装Remote - SSH插件
打开VS Code点击左侧的扩展市场图标搜索`Remote - SSH`插件并安装。
## 3.2 配置SSH连接
1. 确保Linux服务器已启动并记录其IP地址或主机名。
2. 在Windows上安装OpenSSH客户端默认Windows 10及以上版本自带
3. 打开VS Code按下`Ctrl+Shift+P`,输入`Remote-SSH: Add New SSH Host`并选择该项。
4. 输入SSH连接命令例如
ssh 用户名@服务器IP地址
然后选择一个保存SSH配置的路径默认路径是`~/.ssh/config`)。
5. 如果是首次连接请在终端中手动测试SSH连接
ssh 用户名@服务器IP地址
输入密码以验证连接是否正常。如果有提示接受主机密钥,请输入`yes`
## 3.3 通过Remote - SSH连接到Linux服务器
1. 按下`Ctrl+Shift+P`,输入`Remote-SSH: Connect to Host`并选择该项。
2. 在弹出的列表中选择刚刚添加的SSH主机等待连接。
3. 成功连接后VS Code会显示远程的文件系统。
## 3.4 在Linux系统中配置ESP-IDF环境
1. 在SSH连接的Linux终端中执行以下命令以激活ESP-IDF环境
. ~/esp/esp-idf/export.sh
(如果已在`.bashrc`中配置快捷命令,可以直接执行对应的别名命令,如`source-esp523`)。
2. 确保Linux的开发环境已经安装完毕参考前文2.2部分)。
## 3.5 在VS Code中打开ESP-IDF工程
1. 使用VS Code的`File -> Open Folder`功能选择Linux服务器中的ESP-IDF项目目录
~/esp/esp-idf/examples/get-started/hello_world
2. 打开项目后VS Code会加载远程的文件结构。
## 3.6 编译和调试项目
1. 在终端窗口中,运行以下命令以编译项目:
idf.py build
2. 如果需要烧录代码到开发板确保开发板通过USB连接到Linux服务器然后运行以下命令
idf.py -p /dev/ttyUSB0 flash
(根据实际情况替换正确的串口设备路径。)
3. 编译和烧录完成后,可以通过以下命令监控串口输出:
idf.py -p /dev/ttyUSB0 monitor
## 参考链接
1. https://www.bilibili.com/video/BV1J14y1n7Ve/?spm_id_from=333.337.search-card.all.click&vd_source=ef5a0ab0106372751602034cdd9ab98e
2. https://blog.csdn.net/ZRongZH/article/details/129237476
3. https://www.bilibili.com/video/BV1Mv4y1U7x9/?spm_id_from=333.999.0.0&vd_source=ef5a0ab0106372751602034cdd9ab98e

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB