Project Fails to Upload to ESP32 on Platformio - Works on Arduino IDE

This is my first time using platformio, so user error is entirely a possibility. This exact sketch uploads fine to my HiLetGo ESP32s (NodeMCU ESP32S) using the Arduino IDE, but when I try to upload via platformio it fails to upload. I am using the same upload_speed on the arduino IDE.

Platformio.ini [env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
upload_speed = 115200
board_buid.flash_mode = dout

    Log
    Traceback (most recent call last):
    File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 2959, in <module>
    _main()
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 2952, in _main
    main()
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 2653, in main
    esp.connect(args.before)
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 460, in connect
    last_error = self._connect_attempt(mode=mode, esp32r0_delay=False)
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 440, in _connect_attempt
    self.sync()
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 379, in sync
    timeout=SYNC_TIMEOUT)
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 322, in command
    self.write(pkt)
  File "C:\Users\Charlie\.platformio\packages\tool-esptoolpy\esptool.py", line 285, in write
    self._port.write(buf)
  File "c:\users\charlie\.platformio\penv\lib\site-packages\serial\serialwin32.py", line 323, in write
    raise writeTimeoutError
serial.serialutil.SerialTimeoutException: Write timeout
*** [upload] Error 1
1 Like

It looks like it’s timing out when trying to connect to the device. Do you have a an upload_port specified in your platformio.ini file?

1 Like

It was autoselecting the correct port (COM4, which worked with Arduino IDE) and the serial monitor output works as expected. I just tried it with a manually specified upload_port and I still get the exact same error as above.

What’s the purpose of the line:

board_buid.flash_mode = dout

Do you have a special board?

Things to try:

  • Remove the board_buid.flash_mode line
  • Ensure no serial monitor is open and blocking the connection
  • Lower the upload_speed to 9600 (you can increase it again later)

My board is the NodeMCU ESP32S, which is listed in Platformio and it seems that people have success with this board.

I found board_build.flash_mode = dout on an old forum thread when looking for people with similar issues, I’ve since removed this line and there is no difference in behavior with or without - it did not address my issue.

I ensured that no serial monitor is open and have tried to upload after a fresh restart with nothing else open, still fails. The serial monitor in Platformio works as expected at 115200, same setting as in the Arduino IDE.

I tried upload_speed = 9600 and it behaves the same, still fails.

I appreciate the help.

Issue is still present, has anyone else ran into this?

hello, same problem here. I have a M5stack that I can compile project but not upload. The same project uploads fine on other computers. Arduino IDE works with the same project on the same machine, I don’t know what else I can do. Have you somehow solved your problem? If yes, could you please share what you did?

Best regards

No, zero solution here. Tried with VSCode and Atom at this point and neither work.

Only suggestions I can make are to ensure the upload_speed is no higher than 460800 baud, and perhaps to manually specify the upload_port incase auto-detection is getting it wrong. Other than that, perhaps the auto-reset isn’t working properly - try holding the reset button down before the upload process commences?

There is at least one thread on the esp8266 core (yes, I know it’s not the ESP32, but it’s the same esptool error)… and they suggested to use 115200 as the board rate, not 9600… but that’s no help as you started with that :-/ However, in that same issue, one of the developers, earlephilhower, suggests it’s a power or connectivity problem… i.e. try a different USB port, cable, disconnect anything connected to the ESP32, etc. Although if it works with Arduino IDE, it would be more suggestive of an issue with esptool, but it’s still worth trying.

This problem exists and has not been solved.
Has anyone found a solution?

What exact error message are you getting? Have you tried the suggestions above regarding changing the upload speed to 9600 baud? Trief different USB cables? Put the ESP32 in bootloader mode before uploading (hold “FLASH” button, press reset)?

1 Like

Try a different cable. Unfortunately my pile of cables is quickly getting sort into the many that are good enough for power and the few that transfer data well. Who knew that a few strands of wire are worth leaving out

1 Like

Unfortunately it’s pretty common… those appliances that only need the charge wires, so they omit them. Woe is the user that tries to use it thinking it’s a ‘normal’ USB cable! :open_mouth:

I took an hour and went through organising all the various USB leads I have, and there is a compartment just for the ‘charge only’ ones now, so I don’t pick one of those up by mistake!

Thanks for the answer.
I closed the project, and I opened it up and it worked.
I think the problem was my insensitivity, which took so much time and distracted you.

1 Like

No distraction… glad it’s working for you now! :slight_smile:

Just wanted to share my solution to a similar problem. I couldn’t program my ESP32 but for me it was the bluetooth on my laptop. When i disabled it i could see it with the com ports that only the ESP was available. Try it if you have issues it might work for you!

2 Likes

I spent lots of time trying to get my ESP32-WROOM to reliably load code from VSCode/PlatformIO. Most of the web discussion focuses on hardware changes: more bulk capacitance, timing on EN line, but none of that helped. What ultimately made flashing 100% reliable was simply downgrading from the esptoolpy version 2.6 that is the default to 2.1 by adding the following to my platformio.ini in the upload section:

platform_packages = tool-esptoolpy@1.20100.0

With that change, every download was fast and reliable at speeds up to 430kbps.

Sadly, after hours of trying, none of these worked for me (while the Arduino IDE had no issues whatsoever, super weird). I decided to try running esptool via subprocess instead of whatever platformio uses (I’m not fluent in Python so idk!) and it worked! :dizzy:

Create an extra_script.py in the root of your project and add:

Import("env")

import subprocess
from os.path import expanduser, join

home = expanduser("~")
esptool = join(home, ".platformio", "packages", "tool-esptoolpy", "esptool.py")

def on_upload(source, target, env):
    file = str(source[0])
    subprocess.run(["python", esptool, "write_flash", "-z", "0x10000", file])

env.Replace(UPLOADCMD=on_upload)

…and in platform.ini…

extra_scripts = post:extra_script.py

Customize the esptool command as needed. Hope this helped! :smiling_face_with_three_hearts:

2 Likes

Same problem for me. In Auduino IDE works, but not with PlatformIO in Visual Code under Windows 10.
Maybe there is something wrong with esptool and the portable python serial library on my laptop.

I am working in windows and my installed serial port is the COM4. The COM4 port via USB was installed during the Arduino IDE installation process.
So I tried to use esptool using python at the command line.
In windows, when I want to use python, I use the windows linux console WLC. I have installed python 3.7 using pyenv. Then I have cloned the esptool repository in order to be on the latest version.
I don’t want to use sudo to access the /dev/ttyS4, so I added my user to the group dialout:
sudo gpasswd --add ${USER} dialout

After a WLC restart, the COM4 ist mapped to the /dev/ttyS4 but the command flash_id:
python esptool.py --port /dev/ttyS4 --baud 115200 flash_id
still doesn’t works because the dtr issue appears (esptool.py AttributeError: 'Serial' object has no attribute 'dtr' (ESPTOOL-255) · Issue #324 · espressif/esptool · GitHub)

After solving the dtr issue described above with:
pip install --ignore-installed pyserial
the flash_id works without any problems and also the PlatformIO generated firmware flash:
python esptool.py --port /dev/ttyS4 --baud 115200 write_flash --flash_mode dio 0x0 /mnt/c/MyPlatformIOPrj/HelloWorld/.pio/build/nodemcuv2/firmware.bin
Here my WLC output:
python --version
Python 3.7.0a3

python esptool.py --port /dev/ttyS4 --baud 115200 flash_id
esptool.py v3.1-dev
Serial port /dev/ttyS4
Connecting…
Detecting chip type… ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: a4:cf:13:bf:62:77
Uploading stub…
Running stub…
Stub running…
Manufacturer: ef
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin…

I will continue to use Visual Code and PlatformIO to build the project and python in WLC to flash the device.

That worked for me, too. Thanks a lot!

Changing the upload speed in the script helps to perform uploads a little faster:

subprocess.run(["python", esptool, "-b 921600", "write_flash", "-z", "0x10000", file])