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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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, &quot;Liberation Mono&quot;, 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官网有我们需要的一些资料。可以随时查看。
---------------------
作者:单片小菜
链接:https://bbs.21ic.com/icview-3262948-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。

2022 / 01/ 30: 新版esptool 刷micropython固件指令不是 esptool.py cmd... 而是 esptool cmd... 即可;另外rshell 在 >= python 3.10 的时候出错解决方法可以查看:  已于2022年发布的: 第二章:修复rshell在python3.10出错 免费内容: https://edu.csdn.net/course/detail/29666 micropython语法和python3一样,编写起来非常方便。如果你快速入门单片机玩物联网而且像轻松实现各种功能,那绝力推荐使用micropython。方便易懂易学。 同时如果你懂C语音,也可以用C写好函数并编译进micropython固件里然后进入micropython调用(非必须)。 能通过WIFI联网(2.1章),也能通过sim卡使用2G/3G/4G/5G联网(4.5章)。 为实现语音控制,本教程会教大家使用tensorflow利用神经网络训练自己的语音模型并应用。为实现通过网页控制,本教程会教大家linux(debian10 nginx->uwsgi->python3->postgresql)网站前后台入门。为记录单片机传输过来的数据, 本教程会教大家入门数据库。  本教程会通过通俗易懂的比喻来讲解各种原理与思路,并手把手编写程序来实现各项功能。 本教程micropython版本是 2019年6月发布的1.11; 更多内容请看视频列表。  学习这门课程之前你需要至少掌握: 1: python3基础(变量, 循环, 函数, 常用库, 常用方法)。 本视频使用到的零件与淘宝上大致价格:     1: 超声波传感器(3)     2: MAX9814麦克风放大模块(8)     3: DHT22(15)     4: LED(0.1)     5: 8路5V低电平触发继电器(12)     6: HX1838红外接收模块(2)     7:红外发射管(0.1),HX1838红外接收板(1)     other: 电表, 排线, 面包板(2)*2,ESP32(28)  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值