Network Access with MicroPython on an ESP8266

MicroPython Overview

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.


Also published on Medium.

Facebooktwitterredditlinkedinmail

2 thoughts on “Network Access with MicroPython on an ESP8266”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.