MICROPYTHON WITH THE ESP32 GUIDE SERIES

How To Interrupt A Running Program

In this lesson, I'll show you how to interrupt a running program and how to restart your ESP32 using soft reset.

To demonstrate how to interrupt a running program and rest your ESP32, I'll use our familiar "blinking LED script".

Here is the script:

from machine import Pin
from utime import sleep

led = Pin(21, Pin.OUT)

while True:
    print(".")
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

Notice that inside the "while" block, I have added a line that prints a dot character in the shell. This will help us recognise that the program is running without having to look at the LED on the breadboard.

In this lesson, we'll take a closer look at the boards that can use MicroPython.

Ctrl-C to interrupt a program

Click on the play button to start the program. Notice that the LED is blinking, and a new dot character appears in the shell every 0.5 seconds.

While the program is running, you cannot use the shell. To re-gain access to the shell, you must interrupt the program. This, essentially, will end the program execution.

To interrupt the program you can use the Interrupt execution command, under the Run menu.

Or, you can type Crtl-C on your keyboard.

Control-C will interrupt a running program and give you access to the shell.

Pressing Ctrl-C results to a KeyboardInterrupt event.

Ctrl-D to soft-reboot a program

As opposed to a keyboard interrupt via Ctrl-C, you can do a soft-reboot by typing Ctrl-D, or selecting "Send EOF / Soft Reboot" from the Run menu.

When you do a soft reboot, your ESP32 restarts your program from "scratch". As opposed to Ctrl-C, you do not get access to the shell, since the program is still running.

Try Ctrl-D while the blinking LED program is running. Notice that after you do a soft-reboot, the shell shows a horizontal line, and then new dots appear, generated by the running program.

Ctrl-D does a soft-reboot.

The soft-reboot is indicated by a horizontal line in the shell.

After a soft reboot, the RAM of your ESP32 will not be cleared. Any variable values remain in the RAM.

Learn more about the reset and boot modes in MicroPython here.

Stop/Restart backend

Another option available to us is to stop and restart the backend. The result of this is that your ESP32 does a "hard reset". This is similar to pressing the reset button on the board itself.

Of course, a hard reset will stop the program and give you a fresh shell prompt. All RAM contents are lost, but the flash memory remains so any files stored in the ESP32 file system will remain intact.

You can also do a hard-reset by clicking on the Stop button in Thonny.

Stop/Restart backend has the effect of a hard-reset operation.

After a hard-reset, a running program will stop and you will get a fresh shell command prompt.

Disconnect

Finally, you can simulate the physical disconnection and connection of the USB cable by selecting Disconnect from the Run menu.

Edit your caption text here

Often, this is an effective way to deal with communications problems between your computer and the target device. If a software disconnect and connect does not help, the next step would be to physically disconnect and connect the USB cable.

Learn MicroPython for the ESP32

With this video course, you will learn how to use the
MicroPython programming language with the ESP32 micro-controller.


MicroPython is the perfect language for anyone looking for the easiest (yet still powerful) way to program a micro-controller.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

Image
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}