一、ESP8266/ESP32的MicroPython二进制文件的获取

通过下方的链接从MicroPython官网下载已经编译完成的.bin文件

1、ESP8266有Flash大小分别为512K1M2M的三个版本,Flash大小与其中包含的功能多少有关,大家结合自己手中板子的Flash大小以及预期实现的功能进行权衡选择。

2、ESP32的板子型号分类明确,大家可以根据自己板子的型号进行下载

二、下载esptool烧录Flash

1、使用pip install命令进行下载

pip install esptool

2、查看所有的端口,并找到对应的端口

ls /dev/tty*

3、擦除原有的Flash,将</dev/ttyUSB0>替换为实际的ESP8266/ESP32端口

esptool.py --port /dev/ttyUSB0 erase_flash

4、烧录Flash,同样将</dev/ttyUSB0>替换为实际的ESP8266/ESP32端口,将<Flash.bin>替换为实际烧录的.bin文件名

esptool.py --port </dev/ttyUSB0> --baud 460800 write_flash --flash_size=detect 0 <Flash.bin>

esptool.py write_flash 的具体用法及参数说明如下:

usage: esptool write_flash [-h] [--erase-all] [--flash_freq {keep,40m,26m,20m,80m}]
                           [--flash_mode {keep,qio,qout,dio,dout}] [--flash_size FLASH_SIZE]
                           [--spi-connection SPI_CONNECTION] [--no-progress] [--verify] [--encrypt]
                           [--encrypt-files <address> <filename> [<address> <filename> ...]]
                           [--ignore-flash-encryption-efuse-setting] [--compress | --no-compress]
                           <address> <filename> [<address> <filename> ...]

positional arguments:
  <address> <filename>  Address followed by binary filename, separated by space

optional arguments:
  -h, --help            show this help message and exit
  --erase-all, -e       Erase all regions of flash (not just write areas) before programming
  --flash_freq {keep,40m,26m,20m,80m}, -ff {keep,40m,26m,20m,80m}
                        SPI Flash frequency
  --flash_mode {keep,qio,qout,dio,dout}, -fm {keep,qio,qout,dio,dout}
                        SPI Flash mode
  --flash_size FLASH_SIZE, -fs FLASH_SIZE
                        SPI Flash size in MegaBytes (1MB, 2MB, 4MB, 8MB, 16M) plus ESP8266-only (256KB, 512KB, 2MB-c1,
                        4MB-c1), detect, or keep
  --spi-connection SPI_CONNECTION, -sc SPI_CONNECTION
                        ESP32-only argument. Override default SPI Flash connection. Value can be SPI, HSPI or a comma-
                        separated list of 5 I/O numbers to use for SPI flash (CLK,Q,D,HD,CS).
  --no-progress, -p     Suppress progress output
  --verify              Verify just-written data on flash (mostly superfluous, data is read back during flashing)
  --encrypt             Apply flash encryption when writing data (required correct efuse settings)
  --encrypt-files <address> <filename> [<address> <filename> ...]
                        Files to be encrypted on the flash. Address followed by binary filename, separated by space.
  --ignore-flash-encryption-efuse-setting
                        Ignore flash encryption efuse settings
  --compress, -z        Compress data in transfer (default unless --no-stub is specified)
  --no-compress, -u     Disable data compression during transfer (default if --no-stub is specified)

烧录成功后,用电脑或手机扫描WiFi看到一个名为Micropython-xxxxx的热点,可以通过初始密码:micropythoN(注意N是大写的)接入。

三、使用REPL循环交互解释器

1.、通过串口工具使用REPL

Windows可以使用TeraTerm 软件进行串口通信;在MacLinux上可以在终端使用screenpicocomminicom等命令。串口的波特率115200。例如:

picocom /dev/ttyUSB0 -b 115200

连接后键入回车出现>>>符号则说明已经连接成功,如下图:

8266怎么用python esp8266 python_8266怎么用python_03


输入help()可获得如下图提示:

8266怎么用python esp8266 python_嵌入式硬件_04

2.、通过WiFi使用REPL

要通过WiFi使用REPL必须先经过上边的串口通信方式,需要用串口通信方式将通过WiFi使用的开关打开。在串口通信中输入以下命令:

import webrepl_setup

8266怎么用python esp8266 python_micro_05


输入e后,会要求设置一个允许通过网络访问的密码,然后选择重启。也可使用CTRL-D进行软重启,重启后如下图:

8266怎么用python esp8266 python_8266怎么用python_06


可以看到比原来多了两个网址,说明WebREPL已经开启,我们可以通过这两个的网址通过WiFi连接到ESP8266/ESP32(在ESP8266/ESP32没有配置过连接到局域网时,只会出现一个网址)

接下来使用Chrome浏览器或Firefox浏览器(官方建议)打开WebREPL的网址。对照上图,根据自己和板子所在的同一局域网修改相应的网址,如果接入的ESP8266/ESP32作为热点的网络,则不需要改动下图的网址。

8266怎么用python esp8266 python_单片机_07


最后,点击connect后输入刚才设置的密码即可使用WebREPL