Is there a way to reset (restart/reboot) the board within the IDE?

I can restart it by plugging in/out the USB cable or pressing the reset button on the board, but is there a way to do that in software within the PlatformIO?

I could click the “Upload” button again, and that seems to be resetting the board, but it uploads the same code again so it takes a long time (and it wears the flash storage, probably). The board I am using is NodeMCU V3. I thought “Start Debugging” will reset the board and run the code from the beginning, but it seems that debugging is not supported on ESP-12E.

PlatformIO does not have such a button, but using Advanced Scripting, one can add it.

Simply add the line

extra_scripts = reset.py

to the platformio.ini and the file reset.py as

Import("env")
import os
from platformio.builder.tools.pioupload import AutodetectUploadPort

platform = env.PioPlatform()

AutodetectUploadPort(env)
upload_port = env.subst('$UPLOAD_PORT')

def get_esptoolpy_reset_flags(resetmethod):
    # no dtr, no_sync
    resets = ("no_reset_no_sync", "soft_reset")
    if resetmethod == "nodemcu":
        # dtr
        resets = ("default_reset", "hard_reset")
    elif resetmethod == "ck":
        # no dtr
        resets = ("no_reset", "soft_reset")

    return ["--before", resets[0], "--after", resets[1]]

reset_flags = ' '.join(get_esptoolpy_reset_flags(env.subst("$UPLOAD_RESETMETHOD")))

esptool = os.path.join(platform.get_package_dir("tool-esptoolpy"), "esptool.py")
esptool_cmd = f'$PYTHONEXE "{esptool}" --port {upload_port} {reset_flags} --no-stub run' 

# Multiple actions
env.AddCustomTarget(
    name="reset",
    dependencies=None,
    actions=[
        esptool_cmd
    ],
    title="Reset ESP8266",
    description="Resets the ESP8266 board"
)

and after a restart of VSCode a new custom target appears

grafik

So you can just press the reset button and then Monitor.

Note: Resetting will attempt to claim the USB serial interface, so any previous monitor session must be stopped, otherwise esptool.py can’t access the devce. You can stop a monitor session by pressing Ctrl+C while being focused in the terminal window.

1 Like

This works as expected, but is there way to open auto Serial Monitor after success reset?

This is what I initially also wanted but failed. In the actions array, one can add the element pio device monitor to start the monitoring, but somehow when done in this way / shell session, the serial monitor directly aborts on a decoding failure due to the initial output of the ESP8266 coming from the bootloader at a different / unusual baud rate.

This does work only in the case where monitor_speed = 74880 and the sketch also uses that baud rate.

Then you can do, as said,

    actions=[
        esptool_cmd,
        "pio device monitor"
    ],

and when doing that you get

… a reset and monitor with good output.

If the sketch uses e.g. 115200 baud then…

…not so nice.

However, this shouldn’t affect you. When you reset the ESP8266, and its firmware starts running, the USB-serial adapter will buffer the incoming characters (up to a certain number of them), so even if there is some delay between resetting and re-opening the serial monitor, you can see the output. If in doubt, put a small delay() in the setup().

Awesome!! thanks very much!. Mine is working with 115200 baud rate and it’s opening serial monitor.!

I could not get the script working. I had created “reset.py” in the same directory where “platformio.ini” was, and c-p’ed the code. But I got a lot of errors like:

  • Undefined variable ‘Import’
  • Unable to import ‘platformio.builder.tools.pioupload’
  • Undefined variable ‘env’

Python is in the system path, and the version is 3.9.0 (64bit). And does this script also work for Arduino Nano?

The IntelliSense errors in VSCode when opening the Python files are irrelevent because the Python extension cannot determine that this is an SCons script that is dynamically called by PlatformIO and variables and functions like Import() etc are injected during runtime.

No, this script performs only the reset action for ESP8266 chips, resetting other chips is different (I think 1200bps COM port touch).

Another option is to just play with the DTR/RTS bits? I was doing that the other day with a serial monitor (Ctrl+T,Ctrl+D, and then Ctrl+T,Ctrl+R twice, IIRC … the first time holds it in reset, the second time releases) with a ESP8266 D1 Mini. Serial console remains open the whole time, as it uses the existing serial session.

4 Likes