Skip to content

Commit

Permalink
Switch to 115200 baud upload if 460800 fails (esphome#856)
Browse files Browse the repository at this point in the history
* Switch to 115200 baud upload if 460800 fails

* Update __main__.py
  • Loading branch information
OttoWinter authored Nov 14, 2019
1 parent 092bca0 commit 694395a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
31 changes: 21 additions & 10 deletions esphome/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,27 @@ def compile_program(args, config):

def upload_using_esptool(config, port):
path = CORE.firmware_bin
cmd = ['esptool.py', '--before', 'default_reset', '--after', 'hard_reset',
'--baud', str(config[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS].get('upload_speed', 460800)),
'--chip', 'esp8266', '--port', port, 'write_flash', '0x0', path]

if os.environ.get('ESPHOME_USE_SUBPROCESS') is None:
import esptool
# pylint: disable=protected-access
return run_external_command(esptool._main, *cmd)

return run_external_process(*cmd)
first_baudrate = config[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS].get('upload_speed', 460800)

def run_esptool(baud_rate):
cmd = ['esptool.py', '--before', 'default_reset', '--after', 'hard_reset',
'--baud', str(baud_rate),
'--chip', 'esp8266', '--port', port, 'write_flash', '0x0', path]

if os.environ.get('ESPHOME_USE_SUBPROCESS') is None:
import esptool
# pylint: disable=protected-access
return run_external_command(esptool._main, *cmd)

return run_external_process(*cmd)

rc = run_esptool(first_baudrate)
if rc == 0 or first_baudrate == 115200:
return rc
# Try with 115200 baud rate, with some serial chips the faster baud rates do not work well
_LOGGER.info("Upload with baud rate %s failed. Trying again with baud rate 115200.",
first_baudrate)
return run_esptool(115200)


def upload_program(config, args, host):
Expand Down
2 changes: 2 additions & 0 deletions esphome/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def mock_exit(return_code):
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(u"Running command failed: %s", err)
_LOGGER.error(u"Please try running %s locally.", full_cmd)
return 1
finally:
sys.argv = orig_argv
sys.exit = orig_exit
Expand Down Expand Up @@ -216,6 +217,7 @@ def run_external_process(*cmd, **kwargs):
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(u"Running command failed: %s", err)
_LOGGER.error(u"Please try running %s locally.", full_cmd)
return 1
finally:
if capture_stdout:
# pylint: disable=lost-exception
Expand Down

0 comments on commit 694395a

Please sign in to comment.