How to setup MicroPython WebREPL on your ESP32 development board

When I wrote about setting up MicroPython on an ESP development board, I came across the WebREPL.

If you setup MicroPython WebREPL on your ESP32 board, then you can interact with your ESP32 board wirelessly.

In case you need it, this is how to setup MicroPython WebREPL on your ESP32 development board.

Components of MicroPython WebREPL

Before setting up MicroPython WebREPL on our ESP32 development board, let's look at the components of MicroPython WebREPL.

All in all, there is the server component on our ESP32 board that interacts with the hardware. When activated, this server component listens for commands coming from WebREPL clients through WebSocket.

For example, we can use the MicroPython WebREPL client from a computer that is connected to the same network as our ESP32 board and interact with it.
MicroPython WebREPL client running from micropython.org

Since the client is a web page loaded on a browser, we only need to setup the WebREPL server on our ESP32 board.

Setting up MicroPython WebREPL server on our ESP32 development board

First, setup MicroPython on your ESP32 development board if you have not done so.

Once you have done so, you will be able to access MicroPython REPL over serial with a terminal emulator program. For example, you can use the following command to access MicroPython REPL over serial on macOS:

screen -port /dev/tty.SLAB_USBtoUART 115200

At this point in time, you will be able to enable WebREPL on your ESP32 board. In order to do so, enter the following Python codes into the REPL prompt:

 
import webrepl_setup

Once you have done so, follow the instructions to enable WebREPL on your ESP32 board:
MicroPython webrepl_setup sequence on ESP32

When your ESP32 board rebooted successfully, you will find webrepl_cfg.py with content similar to the following:

PASS = 'abcd1234'

In addition to that, boot.py now contains the following codes:

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import webrepl
webrepl.start()

Given that, whenever your ESP32 board starts MicroPython, it will start WebREPL as well.

However, since there is no network connection between your computer and the ESP32 board, you cannot connect to your WebREPL server yet.

Connecting your ESP32 board to your router network

Therefore, you need to connect your ESP32 board to your router's 2.4GHz network. Given that, terminate your terminal emulator program session. If you are using screen, then you will press Ctrl-A+K.

Once the serial connection is released, use rshell to connect to your ESP32 board via serial connection:

rshell -p /dev/tty.SLAB_USBtoUART -b 115200 --editor nano

When you do so, you will be able to edit files on the ESP32 board with the nano editor.

Once rshell starts, run the following command to edit boot.py on your ESP32 board:

edit /pyboard/boot.py

When your nano editor loads boot.py, edit it to look like the following:

def do_connect(ssid, pwd):
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(ssid, pwd)
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)

# Attempt to connect to WiFi network
do_connect('your_ssid', 'your_password')

import webrepl
webrepl.start()

Change your_ssid and your_password to the credentials (SSID, WiFi password) for your 2.4GHz WiFi network. When you are done with the changes, press Ctl-X, Y and Enter to save the changes.

After you have edited boot.py, enter into MicroPython REPL:

repl

Once Python REPL loads, press Ctrl-D to restart your ESP32 board. When your ESP32 development board had restarted, you should find some output similar to the following:

MPY: soft reboot
network config: ('192.168.1.131', '255.255.255.0', '192.168.1.1', '192.168.1.1')
WebREPL daemon started on ws://192.168.1.131:8266
Started webrepl in normal mode
MicroPython v1.12-68-g3032ae115 on 2020-01-15; ESP32 module with ESP32
Type "help()" for more information.

In this case, I will use ws://192.168.1.131:8266 to connect to my ESP32 development board. If your ESP32 development board had connected to your WiFi network successfully, then you should get the URL to connect to your board.

Using the MicroPython WebREPL client to interact with your ESP32 development board

At this point in time, you can interact with your ESP32 development board with the MicroPython WebREPL client. Given that, access MicroPython WebREPL client from a browser within the same network as your ESP32 development board. When the MicroPython WebREPL client loads on your browser, enter the web socket URL into the text field on the top left corner:
MicroPython WebREPL client with 192.168.1.131 as the IP address and 8266 as the port to connect to the WebREPL server on an ESP32 board

After you click Connect, you will be prompted to enter the password (for eg: abcd1234) that you had configured earlier. When you have done so, you can interact with the WebREPL server on your ESP32 board.
Connecting to and interacting with MicroPython WebREPL with a browser directed at 192.168.1.131

In addition to entering Python commands, you can also send files and get files with the controls on the right.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.