NodeMcu: ESP8266 & NodeMcu开发入门

NodeMCU,是一个开源[2]的物联网平台。 它使用Lua脚本语言编程[2]。该平台基于eLua [3]开源项目,底层使用ESP8266 sdk 0.9.5版本。该平台使用了很多开源项>目, 例如 lua-cjson[4], spiffs[5]. NodeMCU包含了可以运行在 esp8266 Wi-Fi SoC芯片之上的固件,以及基于ESP-12模组的硬件。

--- 来自https://zh.wikipedia.org/wiki/NodeMCU

官网:http://nodemcu.com/index_cn.html

2016_12_23_NodeMCU_DEVKIT_1.0

(图片来自:https://github.com/nodemcu/nodemcu-devkit-v1.0)

  • 第一次开机

1. 下载nodemcu-uploader工具(这里我们需要用到Miniterm模块,当然也可以用到其他的串口工具,如minicom):

$ git clone https://github.com/kmpm/nodemcu-uploader.git

2. 查看开机log(注意这里使用的串口波特率为74880):

$ python ./nodemcu-uploader.py --port /dev/cu.SLAB_USBtoUART --start_baud 74880 terminal
--- Miniterm on /dev/cu.SLAB_USBtoUART  74880,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 26292, room 16 
tail 4
chksum 0xf7
load 0x3ffe8000, len 2296, room 4 
tail 4
chksum 0xab
load 0x3ffe88f8, len 8, room 4 
tail 4
chksum 0x36
csum 0x36
rf cal sector: 1019
rf[112] :

相关的文档请从看这里:

https://espressif.com/sites/default/files/documentation/20a-esp8266_rtos_sdk_programming_guide_cn.pdf

2016_12_23_esp8266_boot_info

3. 查看当前固件的相关信息,将串口波特率重新设为115200,并按reset按键:

NodeMCU custom build by frightanic.com
	branch: master
	commit: 81ec3665cb5fe68eb8596612485cc206b65659c9
	SSL: true
	modules: file,gpio,http,mdns,mqtt,net,node,rtcfifo,rtcmem,rtctime,spi,tmr,uart,websocket,wifi,ws2812
 build 	built on: 2016-12-23 04:54
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
lua: cannot open init.lua
> 

这里我们可以看到当前固件所包含的模块及SDK的版本信息,同时我们还可以发现系统开机之后会自动去执行init.lua脚本。

  • 关于nodemcu-uploader工具

-简介

首先看一下这个工具能做什么:

$ python nodemcu-uploader.py 
usage: nodemcu-uploader [-h] [--verbose] [--version] [--port PORT]
                        [--baud BAUD] [--start_baud START_BAUD]
                        [--timeout TIMEOUT]
                        {upload,exec,download,file,node,terminal} ...
nodemcu-uploader: error: too few arguments

在使用早期版本固件时(如0.9.5, 0.9.6)的时候,我们可能需要修改一下代码:

diff --git a/nodemcu-uploader.py b/nodemcu-uploader.py
diff --git a/nodemcu_uploader/uploader.py b/nodemcu_uploader/uploader.py
index 9bfbb8c..121707e 100644
--- a/nodemcu_uploader/uploader.py
+++ b/nodemcu_uploader/uploader.py
@@ -156,6 +156,7 @@ class Uploader(object):
         """Write output to the port and wait for response"""
         self.__writeln(output)
         self._port.flush()
+        self.__clear_buffers()
         return self.__expect(timeout=timeout or self._timeout)

不然在执行文件相关的命令时,可能会报如下错误:

$python ./nodemcu-uploader.py --port /dev/cu.SLAB_USBtoUART --start_baud 9600 --baud 9600 file list
opening port /dev/cu.SLAB_USBtoUART with 9600 baud
Traceback (most recent call last):
  File "./nodemcu-uploader.py", line 10, in <module>
    main.main_func()
  File ".../nodemcu-uploader/nodemcu_uploader/main.py", line 250, in main_func
    uploader = Uploader(args.port, args.baud, start_baud=args.start_baud)
  File ".../nodemcu-uploader/nodemcu_uploader/uploader.py", line 71, in __init__
    __sync()
  File ".../nodemcu-uploader/nodemcu_uploader/uploader.py", line 69, in __sync
    raise DeviceNotFoundException('Device not found or wrong port')

- 常用命令

1. 上传文件(针对早期版本如0.9.5, 0.9.6需要指定`–start_baud 9600 –baud 9600’参数)

$ python ../nodemcu-uploader/nodemcu-uploader.py --port /dev/cu.SLAB_USBtoUART --start_baud 9600 --baud 9600 upload nodemcu_info.lua 
opening port /dev/cu.SLAB_USBtoUART with 9600 baud
Preparing esp for transfer.
Transferring nodemcu_info.lua as nodemcu_info.lua
All done!

NOTE:

nodemcu_info.lua可以从https://github.com/ckuehnel/NodeMCU-applications下载。

2. 显示固件中的文件(针对早期版本如0.9.5, 0.9.6需要指定`–start_baud 9600 –baud 9600’参数):

$ python ../nodemcu-uploader/nodemcu-uploader.py --port /dev/cu.SLAB_USBtoUART --start_baud 9600 --baud 9600 file list
opening port /dev/cu.SLAB_USBtoUART with 9600 baud
Listing files
for key,value in pairs(file.list()) do print(key,value) end
nodemcu_info.lua	992
> 

3. 执行固件中的文件(针对早期版本如0.9.5, 0.9.6需要指定`–start_baud 9600 –baud 9600’参数):

$ python ../nodemcu-uploader/nodemcu-uploader.py --port /dev/cu.SLAB_USBtoUART --start_baud 9600 --baud 9600 file do nodemcu_info.lua
opening port /dev/cu.SLAB_USBtoUART with 9600 baud
Executing nodemcu_info.lua
dofile("nodemcu_info.lua")
NodeMCU Version 0.9.6

ChipID 14257508
FlashID 1458400 Flashsize 4.096 MByte
Flashmode 2 Flashspeed 40 MHz

File system info:
Total : 3396281 Bytes
Used : 7028 Bytes
Remain: 3389253 Bytes

Remaining heap size is: 21024
NodeMCU running 1167.081886 sec from last restart

> 
  • 通过esptool更新固件

1. 下载固件

固件可以从这里下载:https://github.com/nodemcu/nodemcu-firmware/releases,可以看到最新版本的固件官方已经不再提供,官方只提供了源代码。想使用最新版本的固件,可以下载固件到本地编译,当然也可以通过在线编译的方式获取:https://nodemcu-build.com/,具体可以参考:https://nodemcu.readthedocs.io/en/master/en/build/。

在本地编译可以选择基于docker的编译环境:https://hub.docker.com/r/marcelstoer/nodemcu-build/

或者是Linux开发环境:http://www.esp8266.com/wiki/doku.php?id=toolchain#how_to_setup_a_vm_to_host_your_toolchain

建立编译环境及编译固件并不是件容易的事情,这里使用官方0.9.6版本的固件:

https://github.com/nodemcu/nodemcu-firmware/releases/download/0.9.6-dev_20150704/nodemcu_float_0.9.6-dev_20150704.bin

2. 下载esptool工具:

$ git clone https://github.com/espressif/esptool.git
Cloning into 'esptool'...
remote: Counting objects: 1127, done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 1127 (delta 30), reused 0 (delta 0), pack-reused 1061
Receiving objects: 100% (1127/1127), 4.58 MiB | 810.00 KiB/s, done.
Resolving deltas: 100% (642/642), done.
Checking connectivity... done

3. 使用esptool更新固件:

$ ./esptool.py --port /dev/cu.SLAB_USBtoUART --baud 9600 write_flash -fm dio 0x00000 ~/Downloads/nodemcu_float_0.9.6-dev_20150704.bin 
esptool.py v1.3-dev
Connecting...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Auto-detected Flash size: 32m
Running Cesanta flasher stub...
Flash params set to 0x0240
Wrote 462848 bytes at 0x0 in 481.4 seconds (7.7 kbit/s)...
Leaving...

NOTE:

- 在更新firmware的过程中,ESP8266模块上的蓝色LED会不断闪烁。

- 对于目前最新版本的固件,已经不再需要加上`–baud 9600’参数:

$ ./esptool.py --port /dev/cu.SLAB_USBtoUART write_flash -fm dio 0x00000 ~/Downloads/nodemcu-master-16-modules-2016-12-23-04-55-35-float.bin 
esptool.py v1.3-dev
Connecting...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Couldn't connect. [<class '__main__.FatalError'>; Timed out waiting for packet header]. Retrying...
Auto-detected Flash size: 32m
Running Cesanta flasher stub...
Flash params set to 0x0240
Wrote 499712 bytes at 0x0 in 43.5 seconds (92.0 kbit/s)...
Leaving...
  • 相关的参考文档
  1. https://zh.wikipedia.org/wiki/NodeMCU
  2. https://github.com/nodemcu/nodemcu-firmware
  3. https://github.com/ckuehnel/NodeMCU-applications
  4. https://nodemcu.readthedocs.io/en/master/en/flash/
  5. https://github.com/espressif/esptool
  6. http://www.esp8266.com/wiki/doku.php?id=toolchain#how_to_setup_a_vm_to_host_your_toolchain

评论

Be the First to Comment!

提醒我
avatar

wpDiscuz