February 9th, 2017

The USB-TTY converter ordered on the 27th of November finally arrived yesterday. Obviously, I was impatient to test it. It worked with miniterm.py, and nodemcu-uploader could list the Lua scripts in a Sonoff already programmed with NodeMCU. I never tried uploading a file with nodemcu-uploader, instead I tried flashing NodeMCU with esptool.py. Invariably, the operation would fail with the error message:

A fatal error occurred: Timed out waiting for packet header
Most often that would be when 99% of the image had been flashed, but sometimes nearer the beginning of the process.

That was disappointing.

Surprisingly, the Sonoff could be flashed with the very same adapter using the Arduino IDE (version 1.6.13 running on Ubuntu 14.04). So, going back to esptool.py, I tried flashing at many different rates but without success. A search of the Web looking up the error message came up with two germane blog entries at vilimblog:

The photograph of the "FTDI FT232RL (Chinese YP-05 counterfeit, from eBay)" looked like what I had purchased except that "FTDI232" was printed in white on the back instead of "YP-05" and there were no headers along the sides. Perhaps, my purchase did not contain a counterfeit FTDI chip; after all it did work with the Arduino IDE.

It turns out that the adapter does work with the latest beta version of esptool.py (esptool-2.0_beta). How to update is explained below. But that does not answer the question: does it contain a counterfeit FTDI chip or not? While it was one of the more expensive USB adapter from China, it was half the price of a FTDI USB to TTL cable from SparkFun and considerably cheaper than the Adafruit cable. But price alone can't be the determining factor. For one thing, these products are true cables which are undoubtedly costlier to produce. For another, the need of a USB type A to mini-B USB cable should be taken into account when pricing the converter.

I could try the fake/real FT232RL in Windows and get an answer I suppose. But there is a risk. Whatever the answer, I think I should feel lucky to have a working product. Soon, I will be shopping for some mechanical switches, and perhaps I will look into getting a converter based on the CH340G at that time. It is the chip used in the WeMos D1 mini with which I have had no problems. When I get a replacement, I will investigate the question of the authenticity of the FTDI chip again. In the meantime, I am happy to be able to play with the Sonoff Wifi switches directly connected to my desktop, without using a Raspberry Pi as I was doing before.

Updating esptool.py

It is easy to upgrade to the latest stable version of esptool.py

michel@hp:~$ esptool.py version esptool.py v1.2.1 michel@hp:~$ sudo pip install esptool -U ... michel@hp:~$ esptool.py version esptool.py v1.3

Unfortunately the cheap FT232RL converter did not work with esptool.py version 1.3. I then decided to install version 2, which is still in beta and has to be installed manually. The script is available from the Espressif GitHub site.

1. Get the script
Click on the green button Clone or download and then click on Download ZIP and save the archive esptool-master.zip in a folder. The site could be cloned instead, but I have no intention of delving into the code.
2. Extract all the files from the archive to a new folder
I will assume you know how to do that. I use Double Commander which handles archives seamlessly to extract the archive to the directory /home/michel/tmp/esptool-master-done.
3. Install
Open a terminal and change working directory to the folder containing the extracted archive
michel@hp:~$ cd tmp/esptool-master michel@hp:~/tmp/esptool-master$ sudo setup.py install
That's all there is to it.
4. Testing
Testing means flashing a NodeMCU image. That begins by connecting the Sonoff to the USB serial converter and connecting the later to my desktop computer.
  • I made sure that the Vcc jumper on the USB serial converter was in the 3.3V position.
  • I then pushed the USB serial converter onto a breadboard and used Dupont wires to connect GND, TX and RX to the Sonoff header. I did not connect Vcc (3.3V) at that point.

  • I plugged in the USB serial converter to the desktop with my big red USB cable
  • I then verified that the device showed up as /dev/ttyUSB0.
  • I pressed the tactile switch on the Sonoff while connecting the Vcc wire to put the Sonoff in flashing mode.
  • Lastly I flashed the latest NodeMCU image that I am using.
Here is the output of these steps.
michel@hp:~/tmp/esptool-master$ cd /home/michel/Development/NodeMCU michel@hp:~/Development/NodeMCU$ ls /dev/tty* ... /dev/tty2 /dev/tty35 /dev/tty50 /dev/tty9 /dev/ttyS22 /dev/ttyUSB0 /dev/tty20 /dev/tty36 /dev/tty51 /dev/ttyprintk /dev/ttyS23 michel@hp:~/Development/NodeMCU$ esptool.py write_flash -fs 8m 0x00000 \ > nodemcu-master-11-modules-2017-02-01-17-56-38-integer.bin WARNING: Flash size arguments in megabits like '8m' are deprecated. Please use the equivalent size '1MB'. Megabit arguments may be removed in a future release. esptool.py v2.0-beta1 Connecting.... Detecting chip type... ESP8266 Uploading stub... Running stub... Stub running... Attaching SPI flash... Configuring flash size... Flash params set to 0x0020 Compressed 473620 bytes to 310653... Wrote 473620 bytes (310653 compressed) at 0x00000000 in 27.2 seconds (effective 139.2 kbit/s)... Hash of data verified. Leaving... Hard resetting... michel@hp:~/Development/NodeMCU$
As can be seen this was successful but the syntax of esptool.py has changed and the -fs 8m (for flash size 8 megabits) should be changed to -fs 1MB (for flash size 1 megabyte).

Correcting for the change in the way to specify the size of the flash memory and trying for the fastest flash speed, I then tried

michel@hp:~/Development/NodeMCU$ esptool.py -b 921600 write_flash \ > -fs 1MB 0x00000 nodemcu-master-11-modules-2017-02-01-17-56-38-integer.bin ... Wrote 473620 bytes (310653 compressed) at 0x00000000 in 4.1 seconds (effective 917.6 kbit/s)...
That is almost 8 times faster; not bad for a "defective" chip. Note that esptool.py is a bit fussy; the baud rate must be before the write_flash. It makes sense because more than one image file can be flashed each at different rates.
5. Cleaning up
The directory containing the extracted files from the esptool archive can be deleted.

To complete the test, I uploaded a Lua script to the Sonoff and then opened a session with miniterm.py to execute the script. Everything worked correctly. As I said above, I am glad I can experiment with Sonoff switches without an intermediary.