#申请原创# @21小跑堂 @21小跑堂
Python挺酷的 | PyCharm MicroPython ESP32 开发环境搭建
引:
手头上有一个ESP32的开发板,由于项目需要,准备进行评估测试一下。
之前安装过PyCharm,现在准备使用Python进行开发,从网络上搜罗了一些资料。大概有了一些眉目,说干就干,走起来。
如果想要进行MicroPython开发,必然要先更新固件bin文件。
一、先把ESP32的固件bin更新一下
1.下载固件
最稳妥的就是官网下载了。
官网地址:https://micropython.org/download/esp32/
找到最新的版本。
进行下载:
我们下载完了之后,怎么用这个bin文件呢?需要找到一个ESP32的bin烧录工具,这个就叫ESPTOOL,厂家做的很贴心,直接cmd指令就可以进行安装了。
2. EspTool安装
之前说过Python的库文件安装,这个和库文件的安装大同小异,都是使用的是pip指令。
pip install --user esptool
3. 设备连接
将ESP32通过micro USB连接到电脑上,可以查看识别的串口号以备后用。
我们查看设备管理器
通过命令连接设备
esptool.py -pPORTflash_id
将其中的PORT更换为COM5在命令提示符中进行输入,相信很多人遇到和我一样的问题,我总结一下,不是有效的内部命令问题。
C:\Users\Administrator>esptool.py -p COM5 flash_id
'esptool.py' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
这个是因为没有进行路径的设置PACH没有设置,我们可以进入到esptool的安装路径使用命令即可。
<pre class="code-snippet__js" data-lang="properties" style="padding-top: 1em; padding-right: 1em; padding-bottom: 1em; outline: 0px; max-width: 1000%; overflow-x: auto; white-space: normal; flex: 1 1 0%; color: rgb(51, 51, 51); font-size: 14px; letter-spacing: 0.544px; text-align: justify; box-sizing: border-box !important;"><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">C</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">\Users\Administrator>cd c:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">c</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">\Users\Administrator\AppData\Roaming\Python\Python310\Scripts>dir</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"> <span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">驱动器</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">C 中的卷没有标签。</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"> <span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">卷的序列号是</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">9247-BA3C</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">c</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">\Users\Administrator\AppData\Roaming\Python\Python310\Scripts 的目录</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">
</span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 <DIR> .</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 <DIR> ..</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 1,050 espefuse.py-script.py</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 74,752 espefuse.py.exe</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 1,052 espsecure.py-script.py</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 74,752 espsecure.py.exe</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 1,048 esptool.py-script.py</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 74,752 esptool.py.exe</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 106,383 pyserial-miniterm.exe</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">2022/10/31</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">15:43 106,385 pyserial-ports.exe</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"> <span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">8</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">个文件 440,174 字节</span></span></code><code style="outline: 0px; max-width: 1000%; text-align: left; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; box-sizing: border-box !important;"><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"> <span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">2</span> <span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;">个目录 325,530,390,528 可用字节</span></span></code></pre>
然后我们再输入命令,返回的命令格式正确,却找不到COM5端口
c:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts>esptool.py -p COM5 flash_id
esptool.py v4.3
Serial port COM5
Connecting......................................
A fatal error occurred: Failed to connect to Espressif device: No serial data received.
For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
这个原因是因为我之前打开了虚拟串口造成的,还记得当时用的VSPD软件吧。小技巧 | 工程师神器之虚拟串口软件VSPD
打开这个软件将COM5进行关闭,重新试验一下。
c:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts>esptool.py -p COM5 flash_id
esptool.py v4.3
Serial port COM5
Connecting..........
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting.........
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision v1.0)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 3c:71:bf:c7:ff:3c
Uploading stub...
Running stub...
Stub running...
Manufacturer: 20
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin...
至此,连接ESP32成功。
4. 固件擦除
我们擦除原有的固件代码
esptool.py --chipesp32erase_flash
大家知道在烧录的时候,经常用到boot按键,在这里也是一样的,需要长按boot按键才是可以的。
我们可以看见提示符:
c:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts>esptool.py --chip esp32 erase_flashesptool.pyv4.3Found 2 serial portsSerialport COM5Connecting....Chip is ESP32-D0WDQ6 (revision v1.0)Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme NoneWARNING: Detected crystal freq 41.01MHz is quite different to normalized freq 40MHz. Unsupported crystal in use?Crystalis 40MHzMAC: 3c:71:bf:c7:ff:3cUploading stub...Running stub...Stub running...Erasing flash (this may take a while)...Chip erase completed successfully in 9.2sHard resetting via RTS pin...
5. 更新固件
就要将刚刚的bin文件下载到ESP32的板子中。
使用下面的命令:
esptool.py --chip esp32 --port COM5 --baud 460800 write_flash -z 0x1000 esp32-20220618-v1.19.1.bin
我们查看一下:
c:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts>esptool.py --chip esp32 --port COM5 --baud 460800 write_flash -z 0x1000 esp32-20220618-v1.19.1.binesptool.pyv4.3Serial port COM5Connecting.........Chip is ESP32-D0WDQ6 (revision v1.0)Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme NoneWARNING: Detected crystal freq 41.01MHz is quite different to normalized freq 40MHz. Unsupported crystal in use?Crystalis 40MHzMAC: 3c:71:bf:c7:ff:3cUploading stub...Running stub...Stub running...Changing baud rate to 460800Changed.Configuring flash size...Flash will be erased from 0x00001000 to 0x0017efff...Compressed 1560976 bytes to 1029132...Wrote 1560976 bytes (1029132 compressed) at 0x00001000 in 24.4 seconds (effective 511.4 kbit/s)...Hash of data verified.Leaving...Hard resetting via RTS pin...
这部分是不需要按下boot按键的。
至此,我们的固件部分的安装就已完成。下面是配置PyCharm。
二、配置Pycharm
安装MicroPython插件
在设置选项卡里面进行操作
这样就安装完成了,由于之前安装过串口的包文件,所以不再安装,另外有这样的提示:
‘adafruit-ampy’要求安装的版本>=1.0.5,<1.1
文件的版本过低,会提示错误,我们通过pycharm直接升级即可,并不需要额外去安装。
三、编写代码并下载
板子上的蓝色灯接的是P2引脚,那么我们直接控制P2即可,也就是编写一个灯光闪烁的代码。
importtimefrom machine import PinP2 = Pin(2,Pin.OUT)whileTrue: P2.on() time.sleep_ms(200) P2.off() time.sleep_ms(200)
闪烁的频率为200ms亮、200ms灭,即可完成操作。
这些代码都是放到了main.py文件中进行。
下载的时候,可以使用鼠标右键,弹出菜单的形式进行编译和下载,也可以直接点击右上角的绿色运行按钮,都是可以的。
在信息栏中会看见是否下载成功的标识。
Connecting to COM5Uploading files: 0% (0/1)D:\My Project\Python\01\main.py -> main.pyUploadingfiles: 100% (1/1)Soft reboot
四、查看灯光闪烁的效果
结:
microPython官网有我们需要的一些资料。可以随时查看
官网:http://docs.micropython.org/en/latest/esp32/quickref.html
另外:CSDN网站上也有很多优秀的文章,可以进行参考。
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
打赏榜单
21小跑堂 打赏了 30.00 元 2022-11-02 理由:恭喜通过原创审核!期待您更多好的作品~
共1人点赞
|
不错,看看!
MicroPython是基于Python实现的简易版本,用于在嵌入式系统中运行, 作者文章过程详细,图文并茂,介绍软件MicroPython的安装实现,对后来者使用大有帮助。