最近在看nodemcu相关的一些东西,对于我这样不会C的人来讲似乎天生就跟硬件有一道障碍,索性现在基于esp8266的nodemcu可以被Lua和Micropython很好地支持,我且来试着刷一下micropython的固件,废话不多说,进入正题,注意本文中的nodemcu是以安信可公司产品为样件,其他公司产品仅供参考。
一、原材料
1.基于esp8266的nodemcu一片
2.安卓手机数据线一根
3.Python 2.7或Python2.7+,下载地址详见https://www.python.org/downloads/mac-osx/
4.Brew
5.pip或pip3,视python版本而定
6.Esptool
7.Micropyton最新稳定版固件一枚http://www.micropython.org/download#esp8266
8.WebREPLhttps://github.com/micropython/webrepl
9.CP2102驱动https://www.silabs.com/documents/public/software/Mac_OSX_VCP_Driver.zip
注意其他品牌的板子驱动会有所不同请自行下载安装
关于python,brew,pip和驱动的安装,请自行搜索相关教程,这里不再赘言
二、步骤
1.安装Esptool
切换到MAC的终端,执行如下命令
pip install esptool
2.在终端下运行
ls /dev/tty*或者ls /dev/cu*,会得到如下结果,篇幅所限只拷贝片段(以本机为例)


3.用安卓数据线连接Nodemcu和MAC,期间Nodemcu上蓝灯闪烁,说明连接正常
4.再次在终端运行如下命令和步骤二的结果对比找出Nodemcu使用的端口
ls /dev/tty*或者ls /dev/cu*,连接Nodemcu的时候这两个端口都可使用(见红框部分)


5.运行esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash或esptool.py --port /dev/tty.SLAB_USBtoUART erase_flash擦除原厂固件

6.来到最关键的一步,运行esptool.py --port /dev/tty.SLAB_USBtoUART --baud 460800 write_flash --flash_size=detect -fm dio 0 esp8266-20170823-v1.9.2.bin烧录Micropython固件,其中esp8266-20170823-v1.9.2.bin是Micropython官网下载的固件,注意此处--baud参数值必须为460800,不要被写在板子上9600的波特率误导了,估摸着是给Lua固件准备的,一笑.另外如果是Nodemcu则-fm dio 0参数需要带上,单独的esp8266模块可省略该参数。

7.运行screen /dev/tty.SLAB_USBtoUART 115200连接Nodemcu,出现光标之后按Nodemcu上的RST键加载固件

出现如上界面说明固件已经刷好,但其中有一个OSError:[Errno 2] ENOENT错误,是因缺少main.py文件引起的,先不用管它。首先来看一下Nodemcu内部的文件清单

8.再通过help()命令来查看Nodemcu的网络初始化配置

可以看到Nodemcu有两种网络模式配置STA_IF和AP_IF,STA_IF客户端模式即正常的网络访问模式,AP_IF即Access Point模式,建议配置它,因为用WEBREPL上传文件需要用到。
9.启用WEBREPL服务,输入import webrepl_setup,会要求你确认是否启用,输入E启用,输入D禁用

输入E,会要求你输入访问密码

输入密码并确认之后,会要求重新加载固件,输入y回车,后面我们将看看如何使用WEBREPL上传文件到Nodemcu,敬请关注..........
网友评论
想问一下,是什么问题?