These Micropython programs for the ESP32 MCU are a powerful combination for IoT

The Micropython REPL Prompt: Communications with the ESP32 MCU

Micropython runs on ESP32 microprocessorThe Micropython language is a compact subset of the Python 3 programming language designed for use on microcontrollers. It can be used to program the very powerful ESP32 microcontroller from Espressif®.

We have developed an educational kit that includes the ESP32, with a remote control shade motor as a practical application that you can actually use in your home. This makes a great gift for anyone interested in learning how to use microcontrollers! Got a geek in your life? Please see the links at the bottom of this page for more info.

Micropython USB REPL (Read-Evaluate-Print Loop) System

The REPL system is a command line interface that allows the programmer to execute Python commands directly within the ESP 32. The REPL prompt looks like this:

>>>

It is most often used with USB serial communications, with a USB cable connected from your computer to the ESP32 MCU. We use a very simple program called Picocom on our Ubuntu Linux laptops to facilitate the USB connection.

We could also use the free program called PuTTY; it is available for Linux, Mac or Windows. Here is a link that explains how to install and use PuTTY.

Whether you use a simple command line program like Picocom or a GUI program like PuTTY, you end up at the same place: a screen with the REPL prompt >>>, ready to use.

In the Ubuntu Linux environment you can launch a terminal screen and use this Picocom command to start the REPL communications:

ab@ab-Latitude-E6540-1:~$ picocom -b 115200 /dev/ttyUSB0

Micropython USB REPL connection for ESP32 microprocessorThis is a picture of a screen capture of the terminal console after the above command has been executed.

My Dell laptop has successfully initiated the REPL system; you can see the Picocom command line at the top of the picture, then the system acknowledgement and reply, revealing some of the parameters related to the connection.

The REPL >>> prompt appears at the bottom of the picture.

So what can you do with this?

Although it looks simple enough, the REPL prompt is actually a very powerful tool that is a major feature of the Python programming environment. It is also the best way to debug your Micropython code because you can put a 'print()' statement anywhere you want to see the value of a variable.

Anything you can put into a page of code can first be tested right here at the REPL prompt. As an example, we'll do some simple math, then we'll do some simple text string building.

Micropython REPL (Read-Evaluate-Print Loop) Examples

Micropython REPL example of simple mathWhat did we do here?

1. We created 2 variables a and b and assigned values (5, 2)

2. We added the two variables together and assigned the total to a third variable c

3. Finally, we printed the result to the screen (7)


Micropython REPL example of a simple text joinWhat did we do here?

1. We created 2 variables txt1 and txt2 and assigned values ('Hello', ' World')

2. We joined the two together and assigned the result to a third variable txt3

3. Finally, we printed the result to the screen (Hello World)


It is worth noting that Python is a dynamically-typed language, so we did not have to declare any variables as a specific type, before we used them. This is a good example of why Python is user-friendly.

Micropython webREPL Examples

The Micropython version that is compiled specifically for the ESP32 takes things a step further. A major feature of the ESP32 is the embedded on-chip WiFi system that will connect to your router from a cold start in less than a second. This opens up a world of interesting possibilities, not the least of which is webREPL.

Here's where you find it: Micropython webREPL (opens in a new tab)

Micropython webREPL for ESP32 browser screenThe webREPL system creates a wireless REPL connection to your ESP32, once the microprocessor is online.

It also provides a method of uploading and downloading files, and it is a great way to get started.

However, there are a few 'gotchas' and things to know about ...

  • When you first load webREPL, it defaults to the IP address of the chip when the WiFi system is running in AP mode. If you have already paired it with your router, you'll need to enter the IP address that was assigned by your router (the port number stays the same).
  • If you do something to clobber the WiFi connection in your programming, you will lose the webREPL connection. The serial method described on this page would still be available.
  • If you get your code into a loop configuration, you can interrupt the auto-loading main.py file with CTRL-C on the keyboard, but only with serial REPL, not webREPL.
  • This raises another point about the boot sequence in that boot.py must always be present, and it is executed first. It is not a good idea to put any code in that file as there is no way to interrupt it. The main.py file is executed after the boot.py has executed, and the main.py CAN be interrupted with CTRL-C (but only with serial REPL).
  • One nice feature of the webREPL system is that it can upload/download files to the ESP32, but only one at a time. If you are doing a lot of development work, the FTP capability of Micropython and the ESP32 is a better way to go. The next article in this series will cover the use of FTP.

------------------------------------

You can purchase educational kits for blinds and window openers, and our control products in our online DIY automation store.

Stay tuned, if you're interested; we are taking the journey into IoT and automation, writing about every step we take!

Adrian Biffen
Senior Partner
RollerTrol Automation Systems

We sincerely hope you enjoy our advanced technology products; if you have any questions, please contact us at any time!

Articles in this series:

Part 1: Micropython for the ESP32: Connecting to Your WiFi Router Network

Part 2: Micropython for the ESP32: REPL Communications (you are here)

Part 3: Micropython for the ESP32: FTP Fast File Transfer

Part 4: Micropython for the ESP32: Pre-loaded Programs

Part 5: Micropython for the ESP32: DC Motor Reversing Switch

Part 6: Micropython for the ESP32: WiFi Blind Motor Educational Kit Components

Part 7: Micropython for the ESP32: Error Codes and Definitions