Kazu's Log

Jan 12, 2016

My first NodeMCU

After years break from Arduino and Gainer, I bought a NodeMCU Development Kit from AliExpress last year. This device is really cool because

This article just covers basics, because I dont’t have any particular project yet!

Upload NodeMCU firmware to the device

My development kit didn’t have NodeMCU firmware. So I had to install the firmware to the device.

esptool is the tool to install the firmware. To use the tool, you need to install pyserial beforehand.

% export PYTHONPATH="$HOME/local/lib/python2.7/site-packages"
% easy_install --prefix=$HOME/local pyserial
Searching for pyserial
Best match: pyserial 3.0.1
Processing pyserial-3.0.1-py2.7.egg
pyserial 3.0.1 is already the active version in easy-install.pth
Installing miniterm.py script to /Users/kazuyoshi/local/bin

Using /Users/kazuyoshi/local/lib/python2.7/site-packages/pyserial-3.0.1-py2.7.egg
Processing dependencies for pyserial
Finished processing dependencies for pyserial
%

Then you can use esptool.

% git clone https://github.com/themadinventor/esptool.git
Cloning into 'esptool'...
remote: Counting objects: 293, done.
remote: Total 293 (delta 0), reused 0 (delta 0), pack-reused 293
Receiving objects: 100% (293/293), 103.01 KiB | 0 bytes/s, done.
Resolving deltas: 100% (157/157), done.
Checking connectivity... done.
% cd esptool
% python esptool.py
usage: esptool [-h] [--port PORT] [--baud BAUD]
               {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,flash_id,read_flash,erase_flash}
               ...
esptool: error: too few arguments
%

The firmware’s compiled binaries are available on nodemcu-firmware’s repos.

% python ./esptool.py --port=/dev/cu.SLAB_USBtoUART  write_flash  -fm=dio -fs=32m 0x00000 ~/Downloads/nodemcu_integer_0.9.6-dev_20150704.bin
Connecting...
Erasing flash...
Took 2.27s to erase flash block
Wrote 450560 bytes at 0x00000000 in 43.8 seconds (82.4 kbit/s)...

Leaving...
%

Talk to the device

pyserial has miniterm.py that is enough to do “hello world” on the device.

% miniterm.py /dev/cu.SLAB_USBtoUART 9600
--- Miniterm on /dev/cu.SLAB_USBtoUART  9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

After connected to the device, you need to click “RST” button on the device to reset. You will see the below banner after some unreadable characters.

NodeMCU 0.9.6 build 20150704  powered by Lua 5.1.4
lua: cannot open init.lua
>

Now you are in Lua REPL!

> print("hello")
hello
> wifi.setmode(wifi.STATION)
> wifi.sta.config("your_ssid", "your_password")
> conn=net.createConnection(net.TCP, false)
> conn:on("receive", function(c, data) print(data) end)
> conn:connect(80, "8-p.info")
> conn:send("GET / HTTP/1.0\r\n\r\n")
> HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 13 Jan 2016 05:40:16 GMT
Content-Type: text/html
Content-Length: 773
Last-Modified: Sun, 18 Oct 2015 04:45:53 GMT
Connection: close
ETag: "56232401-305"
Accept-Ranges: bytes

<!doctype html>
...

You can quit miniterm.py by Ctrl+].