MicroPython’s WebREPL on the ESP8266
he NodeMCU ESP8266 device has built in WiFi. This allows us to, among other things, connect to the device through it’s own network to run commands. I walked through how connecting to a MicroPython enabled NodeMCU device is accomplished through the serial port in this post. I’d like to take a look now how to utilize another access feature that pairing MicroPython with an ESP8266 device allows, the WebREPL.
The WebREPL is a web based interface for the Read-Evaluate-Print-Loop (REPL) which allows for running commands on the ESP8266 in the same fashion as the terminal REPL does. However, since it is done through a wireless network connection it opens up some additional freedom for access. Of course having a device up and running on a network also poses some potential security issues, but we’ll look at those in another post.
Enable WebREPL
First thing first though, let’s assume that MicroPython is running on an ESP8266 device and you have a serial connection to the device. Again, have a look at my post here for that walk through. Assuming that you have flashed the ESP8266 with the latest version of MicroPython, version 1.8.7 as of this writing, the WebREPL must be enabled through the serial interface. It is a one line command to do from a command prompt:
import webrepl_setup
Entering in this command will produce a series of setup prompts, asking if you want WebREPL to be enabled at boot time, to set a password for the WebREPL, and to confirm rebooting the device to activate the changes.
Connect to Network
Once the system reboots you can connect to the device via WiFi. This is one of the neat features of these devices, if the board doesn’t connect to a wireless access point, it creates one for us. One can then connect to it via our computer or other wireless device. Look for an advertised WiFi network with a name of MicroPython-*. The * will be a set of letters and numbers unique to each device.
Set Password
Upon selecting the device’s network you will need a password. The network password for the access point is micropythoN (note the capitalization of the last letter there). You should then be connected to the device’s network, congratulations!
Now in many situations one could simply browse to the device’s IP address in a browser and get some form of web page, such as our WebREPL interface. Remember that we are dealing with a device with limited memory here and, therefore, doesn’t have the actual webpage. Instead it provides a convenient websocket interface which can be used with the WebREPL Client. You can download it below:
Once downloaded and unzipped you’ll want to open webrepl.html
in either Chrome or Firefox (the browsers currently supported by WebREPL) You should see a screen similar to:
With your computer is connected to the device on the MicroPython network and using the default IP address and port of ws://192.168.4.1:8266 you can select the Connect button to start a WebREPL session.
You should see a prompt to enter your password, which is the one you set after enabling the WebREPL service on the device earlier. Once logged into the device you have access to the REPL just like you would from a serial connection.
Congratulations! You have just connected a NodeMCU ESP8266 to a wireless network with MicroPython. Not too horrible of a task, right?
Follow me on Twitter @kenwalger to get the latest updates on my postings on MicroPython and IoT.
Originally published at Blog of Ken W. Alger.