ESP32开发(使用gitee镜像)

第一章 配置esp-idf开发环境

esp32官方编程指南链接
笔者使用esp32开发板
esp32原理图

第零步 无脑复制配置

Ubuntu系统选择

在选择Ubuntu系统时,尽量选择ubuntu 14以上版本。
因为乐鑫官方SDK中有部分工具使用Python实现,经实验,在Ubuntu14版本进行配置时,会有部分依赖项,造成配置麻烦。故请选择高版本的Ubuntu系统。
本文使用Ubuntu20,Ubuntu16有同事也配置过可行

无脑配置

以下无脑复制到linux终端窗口中执行。(最好一次执行一行)
如果没有出现问题,第一步到第四步就不用看了。出现问题就去第一步到第四步看看有没有对应方法吧!


# 升级源
sudo apt-get upgrade
# 更新源
sudo apt-get update
# 下载esp-idf需要的软件
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
# 默认将esp-idf下载到~/esp/
mkdir ~/esp/
cd ~/esp/
# 下载esp-idf库
git clone https://gitee.com/EspressifSystems/esp-idf.git
# 下载esp-idf的tool库
git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git
cd ~/esp/esp-idf
# 更新submodule
$HOME/esp/esp-gitee-tools/submodule-update.sh
# 下载编译需要的工具
$HOME/esp/esp-gitee-tools/install.sh
# 设置环境变量
. $HOME/esp/esp-idf/export.sh

无脑建立helloworld工程

cd ~/esp
cp -r ~/esp/esp-idf/examples/get-started/hello_world .
cd ~/esp/hello_world
idf.py set-target esp32
idf.py menuconfig # 按q退出
idf.py build
# 板子连接电脑的虚拟机上,按一下ESp-32上的en键
sudo chown xuen /dev/ttyUSB0 # 这里的xuen是我的用户名,改成你自己的
idf.py -p /dev/ttyUSB0 -b 460800 flash
idf.py -p /dev/ttyUSB0 monitor

第一步 安装准备(Linux)

编译ESP-IDF需要以下软件包

1. git (版本控制软件) 2. wget (下载文件工具) 3. flex (词法分析工具) 4. bison (语法分析器的生成器)
5. gperf (性能测试工具) 6. python3 (python3) 7. python3-pip (Python下载工具) 8. python3-setuptools (setuptools模块)
9. cmake (跨平台编译工具) 10. ninja-build (小型构建系统) 11. ccache (高速编译工具) 12. libffi-dev (允许以一种语言编写的代码调用另一种语言的代码)
13. libssl-dev (OpenSSL加密软件库的一个分支) 14. dfu-util (pc host端 usb dfu 协议的实现) 15. libusb

命令如下

sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
如果出现无法安装的情况
1. 更新源
sudo apt-get update
2. 升级
sudo apt-get upgrade

版本问题

cmake 需安装CMake 3.5及以上的版本
python需安装Python 3.6及以上版本

第二步 获取esp-idf

官方安装(舍弃 太难下载)

mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git

git安装很难成功,故用国内gitee镜像

git clone https://gitee.com/EspressifSystems/esp-idf.git

注意:不需要带下载子模块参数 – recursive。我们自己下载

第三步 下载子模块

# 下载子模块
git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git
# 更新子模块 submodules
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/submodule-update.sh
# 安装工具
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/install.sh

出现如下提示则安装成功

All done! You can now run:
  . /home/xuen/esp/esp-idf/export.sh

出现如下问题:

1. 文件不存在
/home/xuen/esp/esp-gitee-tools/tools/tools.json does not exist!
答:切换esp-idf目录下运行esp-gitee-tools/install.sh脚本!!!
2. 没有找到python文件
Installing ESP-IDF tools
/usr/bin/env: ‘python’: No such file or directory
答:将python3 链接为 python。执行 sudo ln -s /usr/bin/python3 /usr/bin/python
3. 编译不成功
Installing ESP-IDF tools
  File "/home/tianmao/esp/esp-idf/tools/idf_tools.py", line 1524
    info(f'Skipping the download of {constraint_path} because it was downloaded recently. If you believe '                                        ^
SyntaxError: invalid syntax
答:Python3可能链接错误。
which python3
将python3的真实路径链接到/usr/bin/python

安装工具 及 更新submodules 的其他方法

# 安装工具 方法1
cd ~/esp/esp-gitee-tools
export EGT_PATH=$(pwd)
cd ~/esp/esp-idf
$EGT_PATH/install.sh

# 安装工具 方法2
cd ~/esp/esp-gitee-tools
./install.sh ~/esp/esp32-sdk/esp-idf

# 更新submodules 方法1
cd ~/esp/esp-gitee-tools
export EGT_PATH=$(pwd)
cd ~/esp/esp-idf
$EGT_PATH/submodule-update.sh

# 更新submodules 方法2
cd ~/esp/esp-gitee-tools
./submodule-update.sh ~/esp/esp32-sdk/esp-idf

第四步 设置环境变量

# 方法1:用esp-idf的脚本添加
cd ~/esp/esp-idf
. $HOME/esp/esp-idf/export.sh
# 方法2:自己添加(可能有添加不全等其他问题吧)
vim ~/.bashrc
在打开文件最后添加
export IDF_PATH=~/esp/esp-idf
# 重启终端 或 刷新配置文件
source ~/.bashrc

出现如下则表示安装成功

Done! You can now compile ESP-IDF projects.
Go to the project directory and run:
  idf.py build

第二章 创建工程及编译运行

第一步 复制helloword例程

用 sep-idf/examples/get-started/hello_world 开始第一个试验

cd ~/esp/
cp -r ~/esp/esp-idf/examples/get-started/hello_world .

第二步 连接设备

请将您的 ESP32 开发板连接到 PC
权限问题 /dev/ttyUSB0

使用某些 Linux 版本向 ESP32 烧录固件时,可能会出现 Failed to open port /dev/ttyUSB0 错误消息。
1. 将用户添加至 Linux Dialout 组
sudo usermod -a -G dialout $USER

第三步 配置

cd ~/esp/hello_world
idf.py set-target esp32
idf.py menuconfig

如果运行失败

# 更新子模块 submodules
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/submodule-update.sh
# 安装工具
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/install.sh
# 设置环境变量
cd ~/esp/esp-idf
. $HOME/esp/esp-idf/export.sh
cd ~/esp/hello_world

第四步 编译工程

idf.py build

运行以上命令可以编译应用程序和所有 ESP-IDF 组件,接着生成 bootloader、分区表和应用程序二进制文件。

$ idf.py build
Running cmake in directory /path/to/hello_world/build
Executing "cmake -G Ninja --warn-uninitialized /path/to/hello_world"...
Warn about uninitialized values.
-- Found Git:/usr/bin/git (found version "2.17.0")
-- Building empty aws_iot component due to configuration
-- Component names: ...
-- Component paths: ...

... (more lines of build system output)

[527/527] Generating hello_world.bin
esptool.py v2.3.1

Project build complete. To flash, run this command:
../../../components/esptool_py/esptool/esptool.py -p (PORT) -b 921600 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x10000 build/hello_world.bin  build 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin
or run 'idf.py -p PORT flash'

第五步 烧录到设备

idf.py -p /dev/ttyUSB0 -b 460800 flash

第六步 监视器

sudo chown xuen /dev/ttyUSB0 
# 用esp-idf提供的监视器
idf.py -p /dev/ttyUSB0 -b 460800 flash
idf.py -p /dev/ttyUSB0 monitor
# 退出esp-idf监视器
Ctrl + ]
# 用picocom监视器
sudo apt-get install picocom
picocom -b 115200 /dev/ttyUSB0
# 退出picocom
Ctrl + a 后 Ctrl + Q

关于picocom

可以设置一个别名,如 
alias pc='picocom -b 115200 /dev/ttyUSB0'
这样在终端输入 sudo pc 就可以打开终端了

第二章 开始板子各个功能调试

1. LED

cd ~/esp/
cp -r ~/esp/esp-idf/examples/get-started/blink .
cd ~/esp/blink
idf.py set-target esp32
idf.py menuconfig
idf.py build
idf.py -p /dev/ttyUSB0 -b 460800 flash

问题1:CONFIG_BLINK_GPIO一直是5,亮的都是D5

idf.py menuconfig
选择 Example Configuration
选择 Blink GPIO number
改成 2

第三章 连接天猫精灵

基于原生阿里生活物联网平台 SDK 开发,支持 ESP8266/ESP32
阿里生活物联网平台创建项目 -> 点击登录生活物联网控制平台
SDK 准备

cd ~/esp/esp-idf/
git checkout v4.2
# 1. 编译 ali-smartliving-device-sdk-c 库
cd ali-smartliving-device-sdk-c
make reconfig (选择SDK平台)
make menuconfig (选择相关功能配置,默认不需要修改,该步骤可以省略)
make (生成相关头文件和库文件)
# 编译 demo 示例
cd ~/esp/esp-ali-smartliving/examples/solutions/smart_light/
make defconfig
make menuconfig
# 生成最终 bin
make -j8
# 连接板子
make erase_flash
make flash

烧录 三元组
erase_flash后需重新烧录NVS分区

# 单个 bin 生成
cd ~/esp/esp-ali-smartliving/config/mass_mfg
# 生成对应的 NVS 分区
$IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate my_single_mfg_config.csv my_single_mfg.bin 0x4000
# 使用 esptool 工具将生成的包含秘钥的 NVS 分区烧入对应的 sector,针对 example 中默认提供的 partitions,esp32 和 esp8266 将烧写到不同的分区,其中 esp32 的默认烧录地址为 0x210000,esp8266 的默认烧录地址为 0x100000。
# 连接板子
$IDF_PATH/components/esptool_py/esptool/esptool.py write_flash 0x210000 my_single_mfg.bin

问题1:编译 ali-smartliving-device-sdk-c 库 时make失败

# 更新子模块 submodules
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/submodule-update.sh
# 安装工具
cd ~/esp/esp-idf
$HOME/esp/esp-gitee-tools/install.sh
# 设置环境变量
cd ~/esp/esp-idf
. $HOME/esp/esp-idf/export.sh

问题2:按照官方的步骤会出错,因为idf v4.0 上 python的命令格式发生了改变

官方步骤如下
$IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py --input my_single_mfg_config.csv --output my_single_mfg.bin --size 0x4000
$IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate my_single_mfg_config.csv my_single_mfg.bin 0x4000

失败显示如下
usage: nvs_partition_gen.py [-h] {generate,generate-key,encrypt,decrypt} …
nvs_partition_gen.py: error: argument command: invalid choice: ‘my_single_mfg_config.csv’ (choose from ‘generate’, ‘generate-key’, ‘encrypt’, ‘decrypt’)

成功显示如下
Creating NVS binary with version: V2 - Multipage Blob Support Enabled
Created NVS binary: ===> /home/xuen/esp/esp-ali-smartliving/config/mass_mfg/my_single_mfg.bin

linux下串口工具

知乎文章:用乐鑫国内镜像构建ESP8266_RTOS_SDK开发环境
不错的学习

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值