Connected to the world

Posted on Fri 29 July 2016 in esp8266

We have the d1 mini and able to run python code on it, yey us!

The big sell of the ESP8266 is the embeded wifi. With this entry we'll go through using this with micropython.

Get Connected


Connect to the D1 and enter the following into the repl. I'll explain the code a chunk at a time.

import network
sta_if = network.WLAN(network.STA_IF)

The network module is what gives us all the wonderful code to use the wifi.

network.STA_IF is the connection where the ESP8266 connects to the internet and acts like a client.
network.AP_IF is the other way round, where devices connect to the ESP8266 and it acts like a server.

We are going to be sending requests out to the internet so we use STA_IF. Then use our connection object to check if we are connected.
sta_if.connect('SSID', 'password')

First we activate the interface, use to check/confirm the interface is active. Then the real work is done with the next line. sta_if.connect Once executed you are returned to the repl but if you wait a couple of seconds you should see connection information coming through.

state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2

connected with VM739083-2G, channel 1
dhcp client start...

I'm not sure what the top few lines of info are. The next line shows a succesfull connection, the router and which channel. DCHP is started and from this the ESP8266 gains an ip address.
You then see your ip address, the gateway mask and the gateway ip.

You can get these settings from the lines below

print('network config:', sta_if.ifconfig())

And thats your device connected to the world!

Note that your device will remember your connection settings and will try to connect using these setting each time the esp8266 is restarted. This is a feature of the esp8266 chip rather than micropython.


To do a simple GET request you can use the instructions here or import urequests.

I'll go through pip-micropython at some point but for now the urequest library can be found here.

Use the Ctrl+E method to past the code into the device. I think there must be a small buffer for this as it failed to paste. I found that I had to paste the code in 3 sections - Response, request and then the rest.

If you've ever used the normal python requests library you'll appreciate how powerful it is.

r = get('')

The d1 has sent a request through the internet and recieved a response, yey!