Network Access with MicroPython on an ESP8266

In a previous post we have seen how to set up a WebREPL on a NodeMCU ESP8266 and create it’s own network. This is very handy in a lot of situations. In many other situations, however, there is already a network available for a device to join. With proper network access any machine on the network can use the NodeMCU. Let’s take a look at MicroPython networking and how we can leverage it to connect to the WebREPL interface from a different machine on the network.

Network Access

The first step is to get the NodeMCU ESP8266 connected to our network. We will need to do this from the serial connection since our WiFi settings will soon change. See my post here for how to setup a serial connection with the NodeMCU ESP8266. In taking a look at the networking documentation on the MicroPython site, we see that there is a network module available. MicroPython has made our lives easier, very nice!

With ssid as the name of the network, and password being the network password, we can get network access with the following commands:

import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'password')

Entering each of those commands into the REPL should allow the NodeMCU ESP8266 to connect to the network.

Network Access

 

Test the Connection

If we give the device a moment or two to connect we can get the network configuration for the device with the ifconfig function.

wlan.ifconfig()

 

NodeMCU Network Config

Great! The ESP8266 is now has network access on the 10.0.0.31 network. The other numbers there are the network mask, gateway, and the DNS address, respectively. With the board connected we can enable the WebREPL interface and start the service.

Start WebREPL

Notice the two different IP addresses there. The 192.168.4.1 is the WiFi network the ESP8266 is generating and the 10.0.0.31 address is for the external network. The addresses themselves may be different on your own device based on your network.

Network Connection to WebREPL

Open the WebREPL client, as discussed in this post and use the IP address of the local network instead of the ESP8266 default 192.168.4.1 address. In this example I’ll input ws://10.0.0.31:8266/ into the address box to connect. Enter the password for the WebREPL that you created earlier and start entering Python commands!

WebREPL connection

Over in the terminal window you should also see a notification that there is a WebREPL connection.

Showing WebREPL Connection

Wrap-Up

In this short post we have seen here how we can obtain network access for our NodeMCU ESP8266 to an existing network and access it through the WebREPL client. Now you can access it through the serial interface, it’s own network, or a device on the same network. With this variety of ways to access an ESP8266 device


Follow me on Twitter @kenwalger to get the latest updates on my postings on MicroPython and IoT.

Facebooktwitterredditlinkedinmail

MicroPython’s WebREPL on the ESP8266

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 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:

Download MicroPython WebREPL Client

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.

Facebooktwitterredditlinkedinmail