2018年就已经在使用ESP32开发一款语音交互的电子产品了。不过,以前没有在csdn写博客的习惯,没有留下什么记录。但是,好记性不如烂笔头,写博客、记笔记,把开发日志整理到这里,总是个好事情。从现在开始,在csdn上安下这处小窝,记录开发旅程上的点点滴滴。

0001. 搭建开发环境

参考资料:

  • 乐鑫官方文档: https://docs.espressif.com/projects/esp-idf/en/latest/
  • 乐鑫Get Started : https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html

一、安装 虚拟机

开发环境: 华为 MagicBook,Windows10。
安装虚拟机 VMware® Workstation 15 Pro

二、在虚拟机里安装 Ubuntu-18.04

从 Ubuntu官网下载 64位版Ubuntu-18.04:ubuntu-18.04.3-desktop-amd64.iso 文件进行安装。

三、在Ubuntu-18.04安装开发环境

参考资料:
https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html

帮助文档中给出了一份安装过程的详细路线图,共有10个步骤,下面逐步来做。

Installation Step by Step
This is a detailed roadmap to walk you through the installation process.

Setting up Development Environment

  • Step 1. Install prerequisites for Windows, Linux or macOS
  • Step 2. Get ESP-IDF
  • Step 3. Set up the tools
  • Step 4. Set up the environment variables

Creating Your First Project

  • Step 5. Start a Project
  • Step 6. Connect Your Device
  • Step 7. Configure
  • Step 8. Build the Project
  • Step 9. Flash onto the Device
  • Step 10. Monitor

Step1. Install prerequisites for Linux

参考资料: https://docs.espressif.com/projects/esp-idf/en/latest/get-started/linux-setup.html To compile with ESP-IDF you need to get the following packages:
为了编译ESP-IDF,我们需要预先安装下面这些软件包。

打开终端,执行:
sudo apt-get install git wget flex bison gperf python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools cmake ninja-build ccache libffi-dev libssl-dev

Step2. Get ESP-IDF

参考资料:
https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html#get-started-get-esp-idf

打开终端,执行:
cd ~/espgit clone --recursive https://github.com/espressif/esp-idf.git ESP-IDF will be downloaded into ~/esp/esp-idf

Step3. Set up the tools

Aside from the ESP-IDF, you also need to install the tools used by ESP-IDF, such as the compiler, debugger, Python packages, etc.
The scripts introduced in this step install compilation tools required by ESP-IDF inside the user home directory: $HOME/.espressif on Linux and macOS, %USERPROFILE%.espressif on Windows. If you wish to install the tools into a different directory, set the environment variable IDF_TOOLS_PATH before running the installation scripts.

安装 ESP-IDF需要使用的工具,例如编译器、调试器、Python包等。
在终端中执行:
cd ~/esp/esp-idf./install.sh

Step 4. Set up the environment variables

The installed tools are not yet added to the PATH environment variable. To make the tools usable from the command line, some environment variables must be set.

设置PATH环境变量,在终端中执行:
. $HOME/esp/esp-idf/export.sh

Step 5. Start a Project

You can start with get-started/hello_world project from examples directory in IDF.

ESP32-IDF的编译工具至此就安装好了。现在可以开始调试ESP32应用了。首先,用esp-idf示例包中的 hello_world 项目开始调试。
在终端中执行:
cd ~/espcp -r $IDF_PATH/examples/get-started/hello_world . 将 hello_world项目文件夹拷贝到 ~/esp/ 目录下。

Step 6. Connect Your Device

Now connect your ESP32 board to the computer and check under what serial port the board is visible.

使用USB线缆把ESP32电路板连接到PC电脑,连接前和连接后,分别在在终端中执行一次:
ls /dev/tty* 通过比较两次的输出结果,可以看到esp32电路板占用的串口号是:/dev/ttyUSB0

Step 7. Configure

Navigate to your hello_world directory from Step 5. Start a Project and run the project configuration utility menuconfig.

在终端中执行:
cd ~/esp/hello_worldidf.py menuconfig

Step 8. Build the Project

Build the project by running:

在终端中执行:idf.py build

This command will compile the application and all ESP-IDF components, then it will generate the bootloader, partition table, and application binaries. If there are no errors, the build will finish by generating the firmware binary .bin file.

Step 9. Flash onto the Device

Flash the binaries that you just built onto your ESP32 board by running:
idf.py -p PORT [-b BAUD] flash Replace PORT with your ESP32 board’s serial port name from Step 6. Connect Your Device.

在终端执行:
idf.py -p /dev/ttyUSB0 flash

Step 10. Monitor

To check if “hello_world” is indeed running, type idf.py -p PORT monitor (Do not forget to replace PORT with your serial port name).
This command launches the IDF Monitor application:
After startup and diagnostic logs scroll up, you should see “Hello world!” printed out by the application.
To exit IDF monitor use the shortcut Ctrl+].

Step 8+9+10. 组合命令

You can combine building, flashing and monitoring into one step by running:
idf.py -p PORT flash monitor

可以将 building、flashing、monitoring组合到一条命令里:
idf.py -p /dev/ttyUSB0 flash monitor

四、开发过程中遇到的问题及解决方法

1. /dev/ttyUSB0 的访问权限

在上述 Step 9. Flash onto the Device步骤中,遇到 /dev/ttyUSB0 无访问权限的问题。
解决方法:
参考:https://docs.espressif.com/projects/esp-idf/en/latest/get-started/establish-serial-connection.html

The currently logged user should have read and write access the serial port over USB. On most Linux distributions, this is done by adding the user to dialout group with the following command:
sudo usermod -a -G dialout $USER Make sure you re-login to enable read and write permissions for the serial port.

应该确保当前用户拥有对 /dev/ttyUSB0 串口的读写权限,解决方法是:将当前用户添加到 dialout用户组中。
在终端中执行:sudo usermod -a -G dialout lake 退出登录,然后再重新登录。