MicroPython's WebREPL on the ESP8266
Tags: micropython, nodemcu, webrepl
Published: 2017-05-10 14:48:17

---
title: MicroPython's WebREPL on the ESP8266
published: true
description: See how to implement a WebREPL interface on the NodeMCU ESP8266
tags: MicroPython, NodeMCU, WebREPL
---

The 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 <a href="https://www.amazon.com/gp/product/B01N0B48NI/ref=as_li_tl?ie=UTF8&amp;tag=kenwalgersite-20&amp;camp=1789&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=B01N0B48NI&amp;linkId=dc1e9f82e2beb42bca36616a22b8820d">NodeMCU</a> device is accomplished through the serial port in <a href="https://www.kenwalger.com/blog/iot/micropython-and-nodemcu-esp8266/">this post</a>. 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.
<h3>Enable WebREPL</h3>
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 <a href="https://www.kenwalger.com/blog/iot/micropython-and-nodemcu-esp8266/">here</a> 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:
<pre>import webrepl_setup
</pre>
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.
<h3>Connect to Network</h3>
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Â <strong>MicroPython-*</strong>. TheÂ <strong>*</strong> will be a set of letters and numbers unique to each device.
<h3>Set Password</h3>
Upon selecting the device's network you will need a password. The network password for the access point isÂ <strong>micropythoN</strong> (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:

<a href="https://github.com/micropython/webrepl/archive/master.zip"><img class="aligncenter wp-image-242 size-full" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/webrepl-btn-e1493758180436.png" alt="Download MicroPython WebREPL Client" width="183" height="75" /></a>

Once downloaded and unzipped you'll want to open <code>webrepl.html</code> in either Chrome or Firefox (the browsers currently supported by WebREPL) You should see a screen similar to:

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/webrepl-screenshot.png"><img class="alignnone wp-image-243" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/webrepl-screenshot-300x164.png" alt="" width="600" height="328" /></a>

With your computer is connected to the device on the MicroPython network and using the default IP address and port ofÂ <strong>ws://192.168.4.1:8266</strong> you can select theÂ <em>Connect</em> 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?

<hr />

<em>
Follow me on Twitter <a href="https://www.twitter.com/kenwalger">@kenwalger</a> to get the latest updates on my postings on MicroPython and IoT, or see the original post on my <a href="https://www.kenwalger.com/blog" target="_blank">blog</a>. Let me know how you are using MicroPython for your IoT projects.
</em>