找回密码
 立即注册
首页 业界区 安全 WSL2搭建基于Docker的ESP32开发环境

WSL2搭建基于Docker的ESP32开发环境

颜清华 前天 21:52
WSL2 安装


  • 如何在 Windows 上安装 WSL2
  1. sudo apt install esptool
  2. sudo apt install linux-tools-generic hwdata
  3. sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*-generic/usbip 20
复制代码
Docker安装和配置

一键安装脚本
  1. curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
复制代码
配置
  1. cat /etc/docker/daemon.json
  2. {
  3.   "registry-mirrors" : ["https://docker.registry.cyou",
  4.         "https://docker-cf.registry.cyou",
  5.         "https://dockercf.jsdelivr.fyi",
  6.         "https://docker.jsdelivr.fyi",
  7.         "https://dockertest.jsdelivr.fyi",
  8.         "https://mirror.aliyuncs.com",
  9.         "https://dockerproxy.com",
  10.         "https://mirror.baidubce.com",
  11.         "https://docker.m.daocloud.io",
  12.         "https://docker.nju.edu.cn",
  13.         "https://docker.mirrors.sjtug.sjtu.edu.cn",
  14.         "https://docker.mirrors.ustc.edu.cn",
  15.         "https://mirror.iscas.ac.cn",
  16.         "https://docker.rainbond.cc",
  17.         "https://do.nark.eu.org",
  18.         "https://dc.j8.work",
  19.         "https://dockerproxy.com",
  20.         "https://gst6rzl9.mirror.aliyuncs.com",
  21.         "https://registry.docker-cn.com",
  22.         "http://hub-mirror.c.163.com",
  23.         "http://mirrors.ustc.edu.cn/",
  24.         "https://mirrors.tuna.tsinghua.edu.cn/",
  25.         "http://mirrors.sohu.com/"
  26.         ],
  27.          "insecure-registries" : [
  28.             "registry.docker-cn.com",
  29.             "docker.mirrors.ustc.edu.cn"
  30.             ],
  31.         "debug": true,
  32.         "experimental": false
  33. }
复制代码
  1. sudo systemctl daemon-reload
  2. sudo systemctl restart docker
复制代码
测试Docker
  1. sudo docker pull hello-world
  2. Using default tag: latest
  3. latest: Pulling from library/hello-world
  4. Digest: sha256:ec153840d1e635ac434fab5e377081f17e0e15afab27beb3f726c3265039cfff
  5. Status: Image is up to date for hello-world:latest
  6. docker.io/library/hello-world:latest
复制代码
  1. sudo docker run hello-world
  2. [sudo] password for cmf:
  3. Hello from Docker!
  4. This message shows that your installation appears to be working correctly.
  5. To generate this message, Docker took the following steps:
  6. 1. The Docker client contacted the Docker daemon.
  7. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  8.     (amd64)
  9. 3. The Docker daemon created a new container from that image which runs the
  10.     executable that produces the output you are currently reading.
  11. 4. The Docker daemon streamed that output to the Docker client, which sent it
  12.     to your terminal.
  13. To try something more ambitious, you can run an Ubuntu container with:
  14. $ docker run -it ubuntu bash
  15. Share images, automate workflows, and more with a free Docker ID:
  16. https://hub.docker.com/
  17. For more examples and ideas, visit:
  18. https://docs.docker.com/get-started/
复制代码
设置以非 root 用户身份管理 Docker


  • Docker Engine 的 Linux 安装后步骤
创建docker组。
  1. sudo groupadd docker
复制代码
将您的用户添加到docker组。
  1. sudo usermod -aG docker $USER
复制代码
注销并重新登录WSL2,在Windows Cmd下执行。
  1. wsl --shutdown
复制代码
还可以运行以下命令来激活对组的更改:
  1. newgrp docker
复制代码
验证是否可以在没有sudo的情况下运行命令。
  1. docker run hello-world
  2. Hello from Docker!
  3. This message shows that your installation appears to be working correctly.
  4. To generate this message, Docker took the following steps:
  5. 1. The Docker client contacted the Docker daemon.
  6. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  7.     (amd64)
  8. 3. The Docker daemon created a new container from that image which runs the
  9.     executable that produces the output you are currently reading.
  10. 4. The Docker daemon streamed that output to the Docker client, which sent it
  11.     to your terminal.
  12. To try something more ambitious, you can run an Ubuntu container with:
  13. $ docker run -it ubuntu bash
  14. Share images, automate workflows, and more with a free Docker ID:
  15. https://hub.docker.com/
  16. For more examples and ideas, visit:
  17. https://docs.docker.com/get-started/
复制代码
拉取Docker镜像
  1. cat docker-compose.yml
  2. version: "3.8"
  3. services:
  4.   esp-idf:
  5.     image: espressif/idf
  6.     container_name: esp-idf-builder
  7.     working_dir: /project
  8.     volumes:
  9.       - .:/project
  10.     environment:
  11.       - HOME=/tmp
  12.       - IDF_GIT_SAFE_DIR=/project
  13.     tty: true
  14.     stdin_open: true
复制代码
  1. docker compose up
复制代码
VsCode


  • Visual Studio Code Download
需要安装的插件


  • Dev Containers
  • Remote Development
  • ESP-IDF
Code
  1. git clone https://github.com/espressif/esp-idf.git
复制代码
使用hello_world为示例工程
  1. cp -rf esp-idf/examples/get-started/hello_world .
复制代码
使用vscode打开hello_world工程
  1. cd hello_world
  2. code .
复制代码
在VsCode的菜单栏选择 查看 > 命令面板,在弹出的对话框中输入ESP-IDF: Add docker container configuration。
然后当前项目的 .devcontainer 文件夹就会自动生成了。下一步在点击左下角的连接->在容器中重新打开。
1.png

2.png

在 Docker 容器中使用外部 USB 转串口设备

Windows中下载并安装usbipd-win工具
3.png

4.png
dmesg可以看到wsl2中已经识别到了一个ttyACM0,这时我们在回到VsCode。
5.png

在下面一行配置中,串口配置端口就选择ttyACM0,设备目标自行根据开发板选择,然后就可以点击Build Project,看我这里编译成功了!
接下载再试试烧录。
6.png

我选择的用UART烧录,看打印信息是烧录成功了,具体我们点击Monitor Devict试下。
7.png

优雅!
参考资料


  • 使用Docker容器的ESP32开发环境搭建
  • Docker官方文档
  • 如何在 Windows 上安装 WSL2
  • 如何将USB 设备连接到 WSL 2 上运行的 Linux 分发版

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册