June 15, 2020

The ESP8266 - getting started with a NodeMCU module

I am sitting here at my desk with a brand new NodeMCU module in its sealed antistatic bag. My task is to reflash firmware for a temperature monitor gadget (I need to change the SSID it tries to connect to). Frankly, it has been so long that I have to relearn how to do this.

I do have plenty of stuff on my computer, so I am not truly starting from scratch. I have the SDK, source code for projects with Makefiles, and various ESP related utilities. My first goal will be to flash the firmware to blink an LED onto this factory fresh device.

So, I cut open the bag with scissors, find a micro USB cable, and plug it into my linux desktop. I watch the messages in /var/log/messages and see:

Product: CP2102 USB to UART Bridge Controller
cp210x converter now attached to ttyUSB0
Actually I see much more than that (the usual USB related chatter), but those lines tell the story. My system sees the USB to serial chip that is on the NodeMCU board and is ready to talk to it in ttyUSB0.

The Makefile for my "blink2" project is my next stopping point. This board probably has NodeMCU/Lua firmware on it, but I have found this to be worse than useless and ignore it.

My makefile has a "make info" entry that uses esptool to do some simple information requests. I set the tty device name in my Makefile and give that a go. It seems to work just fine as follows:

make info
esptool -p /dev/ttyUSB0 read_mac
Connecting...
MAC: 18:fe:34:d4:ex:2x
esptool -p /dev/ttyUSB0 flash_id
Connecting...
Manufacturer: e0
Device: 4016
The makefile also has a "make flash" entry to burn the firmware onto the unit, so let's go for broke and try that.
make flash
esptool elf2image blink2
esptool --port /dev/ttyUSB0 write_flash  0x00000 blink2-0x00000.bin 0x40000 blink2-0x40000.bin
Connecting...
Erasing flash...
Wrote 28672 bytes at 0x00000000 in 2.8 seconds (82.5 kbit/s)...
Erasing flash...
Wrote 186368 bytes at 0x00040000 in 18.1 seconds (82.3 kbit/s)...
Leaving...
That simply worked. The unit is now blinking away. No holding buttons or even having to reset the unit after flashing. It makes me glad I use Makefiles.

Buttons

There are two buttons on the unit. One is labeled "RST" and is obviously a reset button. No mysteries there. The other is labeled "FLASH" and I have a foggy memory of needing to hold it down in certain scenarios that I have forgotten.

Flash my tmon project

Given my experience with my blink demo, this ought to be as simple as changing to the directory with the code and typing "make flash". Let's find out.

I cd to my "tmon" directory and edit the file "secret.h" which has my SSID. I keep it there because I put the code on Github, but this file is in my .gitignore file and not under Git control. Then I type "make clean; make" followed by "make flash". Away it goes. It compiles in less than a second and there seems to be no hassle flashing over the top of the blink demo.

For whatever reason, I need to hit the reset button to get this code running. It sends one message over Wifi, which is good, then goes to sleep and waits for a timer signal that it will never get unless I add a jumper wire. It still responds to "make info". This is as far as I can get without working with the actual tmon hardware.

The old tmon unit does identify itself as ttyUSB0, that much is good. However, it does not respond to "make info", the error from esptool being "Failed to connect to ESP8266". The issue is my jumper from D0 to RST, once I disconnect this, it responds to "make info" and I can type "make flash" and all is well. Once the code is flashed, I reconnect the jumper, click RST, and away we go.