Plan B: Flashing Sonoff devices using FTDI & wires - Success!!

OK, so the SonOTA stuff didn't work, time for Plan B - use FTDI USB-Serial adapter and some wires to connect the ESP8266 to the Mac and have at it...

To make sure everything happened cleanly, I thought I'd follow the instructions (blimey).

Removed current Arduino IDE install, and re-installed 1.8.5, the latest. Then I installed the Espressiv 2.4.0 board code, as per instructions. I compiled the sonoff code, minor glitch when the MQTT buffer was apparently too small. This was due to some old libs from my previous attempts - I hadn't deleted everything Arduino because I wanted to able to do some of the things I'd been doing previous to this escapade. I just deleted the old (differently named!) versions of ArduinoJSON and PubSubClient. Things then compiled ok. All good so far...

At this point I had to connect the board via the FTDI adapter. I used a breadboard for this, and reckoned I could avoid soldering the pins onto the board by using male/female jumper wires, pushed together through the holes in the board. Excellent.

L-R: FTDI Adapter, Breadboard connections, Sonoff board

View of jumper wires pressed through board - fingers crossed


The hard bit now started... You have to hold down the GPIO0 short switch (the plastic filler beside the jumper wires in the picture above) while connecting the power, then release it to tell the chip to go into flash mode. Tricky, because lazy me had to hold the jumper wire pins firmly against the contacts to ensure a good one, whilst triggering the upload with the other hand.

Using the Arduino facility for uploading almost worked, at one point it got to 94% before blowing out. Grr. Then the stupid USB-Serial FTDI port kept disappearing... WTF. It was visible as a device in System Report->Hardware->USB. In fact, sometimes there were two of them!! Not good...

In the meantime I'd started to use Expressif's Python-based esptool.py instead of Arduino IDE. This is easier in some respects, but the Tasmota guide suggested using it to erase_flash, reset_flash and write_flash, so I put those 3 commands into a single file to save typing whilst holding onto contacts. Then I split them incorrectly into 3 separate files - mistake with vi :-). So File 2 managed a complete write_flash, then File 3 erased it again! Bummer...

Meanwhile, the USB failure was becoming extremely annoying, basically having to restart the Mac after every attempt and it seemed to be getting worse. I eventually found some people on StackExchange who were reporting similar problems, which they'd solved by tidying up the USBSerial drivers in /Libraries/Extensions and /System/Libraries/Extensions. For what it's worth, I

  • Removed /S/L/E/FTDIUSBSerial.kext, rebooted
    • No joy...
  • Removed /L/E/usbserial.kext
    • Joy! seems to have resolved the problem; adapter can now be plugged in/removed with exactly the required effect
Time to do something more concrete with the connections to make it simpler. I soldered some connection pins into the holes on the PCBs - easier said than done, of course, cocking up one set of pins in the process. I also reviewed the female jumper wire connections - one, which had been very slack and I'd tried to tighten, broke, so I used some new ones. >Sigh<

Now I'm getting the same error consistently...


Macintosh-Gouk:esptool-master john$ ~/flashtasmota
esptool.py v2.3-dev
Connecting...TRACE +0.000 command op=0x08 data len=36 wait_response=1 timeout=0.100 data=b'\x07\x07\x12 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU'
TRACE +0.000 Write 46 bytes: b'\xc0\x00\x08$\x00\x00\x00\x00\x00\x07\x07\x12 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU\xc0'
TRACE +0.011 Read 1 bytes: b'\xc0'
TRACE +0.000 Read 61 bytes: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.001 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.013 Read 1 bytes: b'\x08'
TRACE +0.001 Read 33 bytes: b'\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0\xc0\x01\x08\x02\x00\x07\x07\x12 \x00\x00\xc0'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.001 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'
TRACE +0.000 Full packet: b'\x01\x08\x02\x00\x07\x07\x12 \x00\x00'

Detecting chip type...TRACE +0.000 command op=0x0a data len=4 wait_response=1 timeout=3.000 data=b'x\x00\x00`'
TRACE +0.000 Write 14 bytes: b'\xc0\x00\n\x04\x00\x00\x00\x00\x00x\x00\x00`\xc0'
TRACE +0.013 Read 1 bytes: b'\xc0'
TRACE +0.001 Read 11 bytes: b'\x01\n\x02\x00\x00 \x06\x00\x00\x00\xc0'
TRACE +0.005 Full packet: b'\x01\n\x02\x00\x00 \x06\x00\x00\x00'
 ESP8266
TRACE +0.000 command op=0x0a data len=4 wait_response=1 timeout=3.000 data=b'\\\x00\xf0?'
TRACE +0.000 Write 14 bytes: b'\xc0\x00\n\x04\x00\x00\x00\x00\x00\\\x00\xf0?\xc0'
TRACE +0.010 Read 1 bytes: b'\xc0'
TRACE +0.001 Read 23 bytes: b'\x01\n\x02\x00\x00 \x06\x00\x01\x05\xc0\xc0\x01\n\x02\x00\x00 \x06\x00\x01\x05\xc0'
TRACE +0.001 Full packet: b'\x01\n\x02\x00\x00 \x06\x00\x01\x05'

A fatal error occurred: Failed to read register address 3ff0005c (result was 0105)

This is the trace output, which shows that I am actually getting connected to the chip and it's just not responding correctly to the prompting. This is true of both boards, so WTF. I'm beginning to lose the will to live, or at least, keep on with this!

Right. Cup of tea. This has to be possible... After all nothing has changed (really!!). Chip can't be broken. Not both. Not in such a smart way! Look at Python code in esptool.py, delve into the SLIP comms protocol so I can see a few of the commands and responses coming and going in the trace above. Note that it's possible to specify the chip with the "--chip" argument, let's try that using the tool interactively, asking for the MAC ID as a simple thing. Result!!

Macintosh-Gouk:esptool-master john$ python3 ./esptool.py --port /dev/cu.usbserial-A9M9DV3R --chip esp8266 read_mac
esptool.py v2.3-dev
Connecting...
Chip is ESP8266EX
Uploading stub...
Running stub...
Stub running...
MAC: 5c:cf:7f:57:c5:24

Hard resetting...

Noting that it's uploading the "compatibility stub", some code that bridges the gap between the ESP12 and ESP32. This is cool. Let's run the remaining commands...

Macintosh-Gouk:esptool-master john$ python3 ./esptool.py --port /dev/cu.usbserial-A9M9DV3R --chip esp8266 erase_region 0x0F4000 0x008000
esptool.py v2.3-dev
Connecting....
Chip is ESP8266EX
Uploading stub...
Running stub...
Stub running...
Erasing region (may be slow depending on size)...
Erase completed successfully in 0.4 seconds.

Hard resetting...

Lovely...

Macintosh-Gouk:esptool-master john$ python3 ./esptool.py --port /dev/cu.usbserial-A9M9DV3R --chip esp8266 erase_flash
esptool.py v2.3-dev
Connecting........__
Chip is ESP8266EX
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 3.2s

Hard resetting...

And now the piece de resistance...

Macintosh-Gouk:esptool-master john$ python3 ./esptool.py --port /dev/cu.usbserial-A9M9DV3R --chip esp8266 write_flash -fs 1MB -fm dout 0x0 ~/Documents/Arduino/SonoffDownload/sonoff.bin
esptool.py v2.3-dev
Connecting....
Chip is ESP8266EX
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Compressed 495808 bytes to 341889...
Wrote 495808 bytes (341889 compressed) at 0x00000000 in 30.3 seconds (effective 130.8 kbit/s)...
Hash of data verified.

Leaving...

Hard resetting...

OK! Time for some taiji.

Comments

Popular posts from this blog

Replacing Coffee Machine Pump (Dualit Espressivo)

Multiple SHT30 sensors on a single I2C bus with Sonoff-Tasmota

NodeMCU + Tasmota code + SHT30