当前位置: 首页 > 工具软件 > PlatformIO > 使用案例 >

利用VSCode+platformio学习esp32开发

沃弘图
2023-12-01

step1:安装platformio,新建文件,找到对应esp32型号,然后完成等待生成文件(过程有一点长)。如果使用的开发版为ESP32 DEVKIT V1要在Board中找到DOIT  ESP32 DEVKIT V1。

step2:找到main函数,setup为初始化函数,loop实际上为while(1)函数。

step3:编写好程序后,以此点击下方的build,upload此时应该用usb连接esp32。

问题1:因为我用的系统为Ubuntu,所以其中出现了串口不可用。需要修改串口权限。

Processing esp32doit-devkit-v1 (platform: espressif32; board: esp32doit-devkit-v1; framework: arduino)
--------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32doit-devkit-v1.html
PLATFORM: Espressif 32 (5.1.1) > DOIT ESP32 DEVKIT V1
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.20004.220825 (2.0.4)
 - tool-esptoolpy @ 1.30300.0 (3.3.0)
 - tool-mkfatfs @ 2.0.1
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Retrieving maximum program size .pio/build/esp32doit-devkit-v1/firmware.elf
Checking size .pio/build/esp32doit-devkit-v1/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.9% (used 16192 bytes from 327680 bytes)
Flash: [==        ]  16.9% (used 221225 bytes from 1310720 bytes)
Configuring upload protocol...
AVAILABLE: cmsis-dap, esp-bridge, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = esptool
Looking for upload port...

Warning! Your `/etc/udev/rules.d/99-platformio-udev.rules` are outdated. Please update or reinstall them.
More details: https://docs.platformio.org/en/latest/core/installation/udev-rules.html

Auto-detected: /dev/ttyUSB0
Uploading .pio/build/esp32doit-devkit-v1/firmware.bin
esptool.py v3.3
Traceback (most recent call last):
Serial port /dev/ttyUSB0
  File "/home/yang/.platformio/penv/lib/python3.10/site-packages/serial/serialposix.py", line 322, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
PermissionError: [Errno 13] Permission denied: '/dev/ttyUSB0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yang/.platformio/packages/tool-esptoolpy/esptool.py", line 5387, in <module>
    _main()
  File "/home/yang/.platformio/packages/tool-esptoolpy/esptool.py", line 5380, in _main
    main()
  File "/home/yang/.platformio/packages/tool-esptoolpy/esptool.py", line 4687, in main
    esp = esp or get_default_connected_device(ser_list, port=args.port, connect_attempts=args.connect_attempts,
  File "/home/yang/.platformio/packages/tool-esptoolpy/esptool.py", line 114, in get_default_connected_device
    _esp = chip_class(each_port, initial_baud, trace)
  File "/home/yang/.platformio/packages/tool-esptoolpy/esptool.py", line 320, in __init__
    self._port = serial.serial_for_url(port)
  File "/home/yang/.platformio/penv/lib/python3.10/site-packages/serial/__init__.py", line 90, in serial_for_url
    instance.open()
  File "/home/yang/.platformio/penv/lib/python3.10/site-packages/serial/serialposix.py", line 325, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'
*** [upload] Error 1
========================================= [FAILED] Took 2.10 seconds =========================================

 *  终端进程“platformio 'run', '--target', 'upload'”已终止,退出代码: 1。
 *  终端将被任务重用,按任意键关闭。

解决方法1:不要把usb拔掉,打开终端输入sudo chmod -R 777 /dev/ttyUSB0,重新upload,即可烧录成功。

 类似资料: