Skip to content

MicroPython: Network Basics

Leo Vidarte edited this page Mar 6, 2017 · 5 revisions
  1. Network basics

The network module is used to configure the WiFi connection. There are two WiFi interfaces, one for the station (when the ESP8266 connects to a router) and one for the access point (for other devices to connect to the ESP8266). Create instances of these objects using:

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

You can check if the interfaces are active by:

sta_if.active() False ap_if.active() True

You can also check the network settings of the interface by:

ap_if.ifconfig() ('192.168.4.1', '255.255.255.0', '192.168.4.1', '8.8.8.8')

4.1. Configuration of the WiFi

You can configure the module to connect to your own network using the STA_IF interface.

import network sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('GLBTD-Guest', 'gl0b4nt4nd1l') sta_if.isconnected() True print(sta_if.ifconfig()) ('10.100.210.153', '255.255.255.0', '10.100.210.254', '8.8.8.8')

Here is a function you can run (or put in your boot.py file) to automatically connect to your WiFi network:

def do_connect(): import network sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect('', '') while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig())

Clone this wiki locally