The following code is based on the default esp-idf generated flashing function (run from within VS Code). It maps those default parameters for use in the subprocess functions.
Code: Select all
proc_args = ['esptool.py']
proc_args.extend(['-p', f"{DEVICE_PORT}"])
proc_args.extend(['-b', '460800'])
proc_args.extend(['--before', 'default_reset'])
proc_args.extend(['--after', 'hard_reset'])
proc_args.extend(['--chip', 'esp32'])
proc_args.append('write_flash')
proc_args.extend(['--flash_mode', 'dio'])
proc_args.extend(['--flash_freq', '40m'])
proc_args.extend(['--flash_size', 'detect'])
proc_args.extend(['0x10000', f"{BUILD_DIR}/6mw-firmware.bin"])
proc_args.extend(['0x1000', f"{BUILD_DIR}/bootloader/bootloader.bin"])
proc_args.extend(['0x8000', f"{BUILD_DIR}/partition_table/partition-table.bin"])
print('\n\nARGS\n', proc_args)
p = subprocess.run(proc_args, capture_output=True, text=True)
print('\n\nOUTPUT\n', p.stdout)
print('\n\nERRORS\n', p.stderr)
I have found an alternative approach using subproces.Popen. Using this with the default '--help' argument works as expected. That is, each individual line is received and processed in real time.
But if I pass the proc_args as defined above, it does not run the actual arguments (it just continues to display the help text, as though no arguments have been passed):
Code: Select all
p = subprocess.Popen(proc_args, stdout=subprocess.PIPE, universal_newlines=True, shell=True)
for out in iter(p.stdout.readline, ''): # read rest of output
print(out, end='')
p.stdout.close()
return_code = p.wait()
Code: Select all
shell=True
Code: Select all
universal_newlines=True
Is there anything obvious about the difference between run() and Popen() that would cause this to work for one, but not the other?
Alternatively, does anyone else have a working example of successfully calling esptool.py from within python with a "tried and tested" result?
Thanks in advance. Grace
P.S. if there's a more appropriate thread or sub-forum for this post, please let me know