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.
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.
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.
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().
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.