Kazu's Log

Mar 3, 2016

Feltron Annual Report

This is in my “to blog” list for more than a month. I received Nicholas Fleton’s 2014 Annual Report at the end of January. He is known for visualizations and life logs from Daytum, Reporter to Facebook’s timeline. I had bought his 2010 Annual Report when I was in Japan. So 2014’s one is my second one, but sadly that was the finale of his annual report series.

Visualizations are one of the area I like, but I just like. I mean, I haven’t make anything to show my interest. I played with D3.js and Paper.js a bit but that’s all.

March will be for wrapping up what I did in Q1. But after that, I would like to spend some time (and hopefully blog) on visualizations.

Feb 3, 2016

wrk2 on OS X El Capitan

Building wrk2 on OS X El Capitan is not straightforward, due to OpenSSL.

El Capitan has OpenSSL’s libraries, but it doesn’t have corresponding headers. This email from Apple Developer Relations team explains the intention of the mismatch.

As a result, building wrk2 doesn’t succeed.

% make
Building LuaJIT...
HOSTCC    host/minilua.o
OK        Successfully built LuaJIT
CC src/wrk.c
In file included from src/wrk.c:3:
src/wrk.h:11:10: fatal error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>
1 error generated.
make: *** [obj/wrk.o] Error 1

The good news is, OpenSSL is available on Homebrew. After doing brew install openssl, you can specify the location of OpenSSL by passing CC and LDFLAGS through enviromental variables.

% CC='cc -I/usr/local/opt/openssl/include' LDFLAGS='-L/usr/local/opt/openssl/lib' make
Building LuaJIT...
HOSTCC    host/minilua.o
HOSTLINK  host/minilua
ld: warning: directory not found for option '-Ldeps/luajit/src'
DYNASM    host/buildvm_arch.h
LUAJIT src/wrk.lua
LINK wrk

It does work without LDFLAGS, but in that case, wrk2 includes the headers from Homebrew (1.0.2f as of today), but the binary uses the libraries from OS X (0.9.8). Both of them might be incompatible, so it should be avoided.

Jan 16, 2016

Blinking an LED with NodeMCU

What I did first on NodeMCU was connecting to the Internet, but usually blinking an LED is the “hello world” in the microcontroller world. So let’s do it.

Connecting an LED


  • Node MCU Developer Kit
  • LED
  • Resistor

The circuit is pretty simple.

  • Connect the Node MCU’s D1 (GPIO) to the LED’s longer lead (anode)
  • Connect the LED’s shorter lead (cathode) to the resistor
  • Connect the resistor to the Node MCU’s GND


luatool is a small Python script that communicates with NodeMCU. Instead of using pySerial’s miniterm.py interactively, you can use luatool to upload your Lua script to the device.

luatool needs pySerial. The install instruction is in my previous post.

% git clone https://github.com/4refr0nt/luatool
% cd luatool
% python luatool/luatool.py --port /dev/cu.SLAB_USBtoUART --id

->=node.chipid() -> send without check

Why using infinite loops is a bad idea?

Once you have luatool, you can write a Lua script on Emacs (or your favorite editor, but isn’t it Emacs?) and upload the script to the device.

To blink an LED, the simplest solution is just having an infinite loop like below.

US_TO_MS = 1000

gpio.mode(LED_PIN, gpio.OUTPUT)

while true do
   gpio.write(LED_PIN, gpio.HIGH)
   tmr.delay(500 * US_TO_MS)
   gpio.write(LED_PIN, gpio.LOW)
   tmr.delay(500 * US_TO_MS)

Sending the script by using luatool…

% python luatool/luatool.py --port /dev/cu.SLAB_USBtoUART --src ~/blink.lua --dofile
->dofile("blink.lua") -> send without check
--->>> All done <<<---

It does work, but then you will realize that you can’t upload anything from luatool.

The reason is, luatool is doing all operations by just sending Lua code. Once you execute a script that has an infinite loop, The Lua interpreter on the device can’t receive any new code due to the loop, and it makes luatool unusable.

Use tmr.alarm instead

Actually NodeMCU’s reference clearly metiones that on tmr.delay’s section.

Busyloops the processor for a specified number of microseconds.

This is in general a bad idea, because nothing else gets to run, and the networking stack (and other things) can fall over as a result.

Instead you can use tmr.alarm that wraps tmr.register and tmr.start.

Here is the new script. EXPR and A or B is Lua’s ternary operator equivalent idiom.


gpio.mode(LED_PIN, gpio.OUTPUT)
value = true

tmr.alarm(0, 500, 1, function ()
    gpio.write(LED_PIN, value and gpio.HIGH or gpio.LOW)
    value = not value

If your previous script is running, you need kill that by clicking RST (Reset) on the device, before using luatool.

% python luatool/luatool.py --port /dev/cu.SLAB_USBtoUART --src ~/blink.lua --dofile
->dofile("blink.lua") -> send without check
--->>> All done <<<---

It looks same, but unlike before, you can use luatool even this script is running.

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]
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
Erasing flash...
Took 2.27s to erase flash block
Wrote 450560 bytes at 0x00000000 in 43.8 seconds (82.4 kbit/s)...


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")
> 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+].

Jan 1, 2016

Hello World

Every new year, I make a new blog to begin anew. Now my blog is powered by Hugo that I already sent 4 patches during DigitalOcean’s Hacktoberfest.

The site’s repos is on BitBucket. This Wercker application clones the repos and runs Hugo on it to build HTML files, then s3sync on the application uploads the files to Amazon S3.

Aside from “how I write”, I will make a small change on “what I write”. I’m a long-time blogger and I usually write whatever I want, because I love to read diary-like blogs. However in this year, I will focus on technical topics in a few areas to market myself.

Sounds self-help-ish? I know!

I’m reading Soft Skills now, and I’d like to experiment with some of his suggestions. That’s not super comfortable, but I want to learn new things by doing them. So please don’t sing Fitter Happier for a moment.