Tuesday 22 March 2016

Updating firmware on ESP8266 devices using the Raspberry Pi

I've outline how to update the firmware on the Pimoroni ESP8266 pHAT and I thought I would expand on that a little by detailing how to use the Raspberry Pi to flash the firmware for other ESP8266 devices.  I've have a couple of NodeMCU boards which I got shipped from China for £3.75 each.  Here is one of them along side a Pimoroni pHAT:


I've been playing around with them on a PC but thought I would give one a try on a Raspberry Pi.  This device is a nice little package containing a ESP8266, a USB to serial controller and a 3.3V regulator so that the device can be powered directly from USB.  There are also 2 buttons.  One marked 'flash' and the other 'user'.  They came with the stock Espressif firmware but I have been experimenting with other files.  Using it with a Raspberry Pi is much like using the pHAT with the same tools needed.

Start with a fully patched and upgraded 'Jessie' image and install Minicom:

sudo apt-get install minicom

and then create a convenient working folder where you can download and make executable the flashing tool:

wget https://raw.githubusercontent.com/themadinventor/esptool/master/esptool.py
chmod +x esptool.py


More information about this tool can be found here and also in my previous post.  Once you have done this you have all the tools that you need and so you can connect the ESP8266 to the Pi via a USB cable.  The Pi will happily allow you to hot plug it and so we are ready to connect to the device but first we need to know what the serial port is named.  It is probably /dev/ttyUSB0 but we can check this by searching for serial devices that actually have a driver (and not just virtual devices) as follows:

 ls -l /sys/class/tty/*/device/driver

Which on my Pi gives:

lrwxrwxrwx 1 root root 0 Mar 22 17:06 /sys/class/tty/ttyAMA0/device/driver -> ../../../../bus/amba/drivers/uart-pl011
lrwxrwxrwx 1 root root 0 Mar 22 17:09 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../../bus/usb-serial/drivers/ch341-uart


both the built in UART and the USB device.  Now we can connect to the ESP8266 using Minicom:

minicom -b 115200 -o -D /dev/ttyUSB0

and send it an AT command:

AT

OK


You have to add Ctrl-j after the carriage return for it to work.  Once you see that it is working you can reset the device.  To do this the 'rst' pin needs to be grounded.  You can use a paper clip to briefly connect the pin to the neighbouring ground pin and you will see the LED on the device blink and loads of random characters printed to Minicom before ending in 'ready'.  The device has been hard reset.  The next thing to test is programming mode.  Hold down the button marked 'flash' while doing a reset with the paper clip.  This time the text in Minicom won't display 'ready' and you can't enter commands.  The device is in programming mode.  Close down Minicom so that it doesn't conflict and you are now ready to test with the programming tool:

./esptool.py -p /dev/ttyUSB0 read_mac

should show:

Connecting...
MAC: 5c:cf:7f:0c:40:d5


which is the MAC address of the device.  It's now ready to be programmed and I explained how to do that in my last post.

No comments:

Post a Comment