Fatal Error (Invalid head of packet ('\xcd') uploading to ESP8266

I am working on a project which has a Teensy (3.1/3.2) and an ESP8266 board. I am using PlatformIO (Core 3.5.0rc6) with Atom as the IDE. The two components of the program (host, esp) build with no errors, the host uploads successfully to the teensy; however the ‘esp’ upload fails as follows:
Looking for upload port…
Auto-detected: COM3
Uploading .pioenvs\esp\firmware.bin
esptool.py v1.2-dev
Running Cesanta flasher stub…

A fatal error occurred: Invalid head of packet (‘\xcd’)

I get the same error if I run the upload command from a Windows Command Window. Interestingly if I run this command:
esptool.py -p COM3 write_flash 0x0 .pioenvs\esp\firmware.bin
from the Command Window, the (esp) program loads successfully.

The pertinent section of the .platformIO.ini file looks like this:
src_filter = +<common/>,+<esp/>
# TODO: enable -Werror once platformio published v2.4 of esp8266/Arduino (pr #3271)
build_flags = -Wall -Isrc/common -Isrc/esp -DHAVE_STRLCPY -DHAVE_STRLCAT
platform = espressif8266
framework = arduino
board = esp_wroom_02
extra_scripts = tools/platformio_cfg_esp.py
lib_ldf_mode = deep
lib_ignore = NMEA2000-Teensy, FlexCAN_Library, Time
lib_deps =
Adafruit NeoPixel
*GitHub - me-no-dev/ESPAsyncTCP: Async TCP Library for ESP8266
*GitHub - me-no-dev/ESPAsyncWebServer: Async Web Server for ESP8266 and ESP32

Does anyone have any thoughts on why the ‘esp’ upload fails?


Please open PIO IDE Terminal and type pio run -t upload -v and share output on https://hastebin.com.
Post link here.

I ran the command only uploading the ‘esp’ portion of the project as that is what fails. I pasted the output here:


I mentioned this before, but I just noticed now how similar the upload commands are: the one that fails and the one that works.

In the output above the command to upload run by the ‘pio run -e esp -t upload -v’ command (near the end of the listing) is:
\tools/esptool.py -p COM3 -b 2000000 write_flash --flash_mode dio --flash_size 16m 0x0 .pioenvs\esp\firmware.bin

and this fails as shown above. Yet if I run the following command in a Windows terminal, (esp) uploads successfully:
tools/esptool.py -p COM3 write_flash 0x0 .pioenvs\esp\firmware.bin
with output:

esptool.py v1.2-dev
Running Cesanta flasher stub…
Flash params set to 0x0000
Writing 237568 @ 0x0… 237568 (100 %)
Wrote 237568 bytes at 0x0 in 16.9 seconds (112.2 kbit/s).


Please take a look at 54 line. It seems that you overwrote UPLOADCMD in tools/platformio_cfg_esp.py. Please fix your custom upload tool or use official by PlatformIO.