Specify connection timeout of esptool

How to specify connection such as 60 seconds during uploading syncronization?

usage: esptool [-h]
               [--chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2}]
               [--port PORT] [--baud BAUD]
               [--before {default_reset,usb_reset,no_reset,no_reset_no_sync}]
               [--after {hard_reset,soft_reset,no_reset,no_reset_stub}]
               [--no-stub] [--trace] [--override-vddsdio [{1.8V,1.9V,OFF}]]
               [--connect-attempts CONNECT_ATTEMPTS]
               {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,merge_bin,get_security_info,version}
               ...
esptool: error: argument operation: invalid choice: '--connect-attempts 60' (choose from 'load_ram', 'dump_mem', 'read_mem', 'write_mem', 'write_flash', 'run', 'image_info', 'make_image', 'elf2image', 'read_mac', 'chip_id', 'flash_id', 'read_flash_status', 'write_flash_status', 'read_flash', 'verify_flash', 'erase_flash', 'erase_region', 'merge_bin', 'get_security_info', 'version')
*** [upload] Error 2

I mean, I tried using upload_flags in platformio.ini, but I got argument error from esptool.py:

[env:esp32cam]
platform = espressif32
board = esp32cam
framework = espidf
monitor_speed = 115200
monitor_filters =
  send_on_enter
monitor_eol = LF
monitor_echo = yes
upload_speed = 115200
upload_flags = 
  --connect-attempts 60

So, how to properly used upload_flags?

To properly specify connection attempts or similar options during the upload process in PlatformIO for ESP devices, you need to ensure that the options you are passing with upload_flags are valid and supported by the underlying tool (esptool.py in this case). The error you’re encountering suggests that --connect-attempts is being passed in a way that’s not compatible or not supported in the context of the operation you’re attempting.

Correct Usage of upload_flags in platformio.ini

  1. Check Compatibility of Options with esptool.py:
    The --connect-attempts option is valid in esptool.py, but it must be placed correctly in the command structure. It cannot directly follow a subcommand like write_flash or similar. Ensure the flag is at the correct position in the generated command.

  2. Update Your platformio.ini:
    To pass the --connect-attempts option to esptool.py, add it to the upload_flags list, which appends arguments to the esptool.py call during uploads.

    Update your platformio.ini like this:

    [env:esp32cam]
    platform = espressif32
    board = esp32cam
    framework = espidf
    monitor_speed = 115200
    monitor_filters =
      send_on_enter
    monitor_eol = LF
    monitor_echo = yes
    upload_speed = 115200
    upload_flags =
      --connect-attempts=60
    
  3. Verify the Placement of Flags:
    The upload_flags should apply to the initial connection process. If it’s being misinterpreted, you may need to verify the generated upload command using platformio run -t upload -v (verbose mode). This shows how PlatformIO structures the esptool.py command.

  4. Alternative Placement for Connection Retry Settings:
    If the --connect-attempts option continues to cause issues, consider using the extra_scripts mechanism to run custom scripts or modify the build process. Create a Python script to append flags programmatically:

    Import("env")
    
    # Add --connect-attempts=60 flag
    env.Append(UPLOADERFLAGS=["--connect-attempts", "60"])
    

    Then, reference this script in your platformio.ini:

    extra_scripts = post:custom_upload.py
    
  5. Ensure Tool Versions Are Updated:
    Make sure you’re using the latest version of PlatformIO and esptool.py. Run:

    platformio update
    

Debugging the Problem

  • Run the upload in verbose mode:

    platformio run -t upload -v
    

    Look for how the --connect-attempts flag is applied and if it’s correctly positioned in the command.

  • If you continue to encounter issues, try running esptool.py directly to see if the same error occurs.

By following these steps, you should be able to pass the --connect-attempts option properly to the upload process.

The question that arises for me: Why do you need this? I suspect you have a fault that lies in a completely different place.

The --connect-attempt parameter controls the number of attempts, not the timeout. So if you set --connect-attempt to 60, 60 attempts will be made to establish a connection. The timeout is set to 3 seconds by default. You therefore have a total duration of 60 * 3 seconds = 180 seconds.

See
https://docs.espressif.com/projects/esptool/en/latest/esp32/esptool/configuration-file.html#options

This answer smells like ChatGPT or another AI chatbot.

Did you test it and does it work for you?

When I tested it, I got this error message:

esptool: error: unrecognized arguments: --connect-attempts 60 0x10000 .pio\build\esp32-s3-devkitc1-n16r8\firmware.bin

Yes, it’s generated by ChatGPT, yes, it’s worked which means connection timeout is longer compared to default, I don’t measure yet how long is it.

The different is, I was misused the upload_flags in platformio.ini which is using space, while it was expecting =.

So, replacing space with = solved my problem.

About extra_scripts, I can’t guarantee whether it’s worked or not.

I need that because I’m using esp32-camera breakout board where I should manually click the button in order to upload new program.

There is a “programmer” available called “ESP32-CAM-MB” which gives you USB connection, upload and reset button.
See