MicroPython: ESP32/ESP8266 Access Point (AP)

Learn how to set your ESP32 or ESP8266 boards as an Access Point (AP) using MicroPython firmware. This allows you to connect directly to your ESP boards via Wi-Fi without a wireless router.

MicroPython: ESP32/ESP8266 Access Point (AP)

Use the following snippet to set your ESP32 or ESP8266 as an access point using MicroPython:

ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)

Prerequisites

To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:

Learn more about MicroPython: MicroPython Programming with ESP32 and ESP8266 eBook

ESP32/ESP8266 Station and Access Point

In most of our web server projects with MicroPython, we connect the ESP32 or the ESP8266 to a wireless router. In this configuration, we can access the ESP board through the local network.

In this scenario, the router acts as an access point and the ESP boards are set as a station. So, you need to be connected to your router (local network) to control the ESP32 or ESP8266.

ESP32/ESP8266 Station MicroPython firmware

In some cases, this might not be the best configuration (when you don’t have a router nearby). But if you set the ESP boards as an access point (hotspot), you can be connected to them using any device with Wi-Fi capabilities without the need to connect to your router.

Basically, when you set the ESP32 or ESP8266 as an access point you create its own Wi‑Fi network and nearby Wi-Fi devices (stations) can connect to it (like your smartphone or your computer).

ESP32/ESP8266 Access Point (AP) MicroPython firmware

In this tutorial, we’ll show you how to set the ESP32 and ESP8266 as an access point in your web server projects. This way, you don’t need to be connected to a router to control them.

Because the ESP doesn’t connect further to a wired network (like your router), it is called soft-AP (soft Access Point).

This means that if you try to load libraries or use firmware from the internet, it will not work. It also doesn’t work if you try to make HTTP requests to services on the internet like publishing sensor readings to the cloud.

ESP32/ESP8266 MicroPython Access Point (AP) for Web Server

For demonstration purposes, we’ll create a simple “Hello, World! web server. To learn more about how to create a web server with the ESP32 or ESP8266, you can read the following tutorial:

Copy the following code to your boot.py file and upload it to your board.

# Complete project details at https://RandomNerdTutorials.com

try:
  import usocket as socket
except:
  import socket

import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'MicroPython-AP'
password = '123456789'

ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)

while ap.active() == False:
  pass

print('Connection successful')
print(ap.ifconfig())

def web_page():
  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
  <body><h1>Hello, World!</h1></body></html>"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  print('Content = %s' % str(request))
  response = web_page()
  conn.send(response)
  conn.close()

View raw code

Customize the SSID and Password

You need to define a SSID name and a password to your access point. In this example, we’re setting the SSID name to MicroPython-AP, but you can modify the name to whatever you want. The password is 123456789, but you can also modify it.

ssid = 'MicroPython-AP'
password = '123456789'

Setting an Access Point with MicroPython

Then, create an access point using the following line of code:

ap = network.WLAN(network.AP_IF)

Activate the access point:

ap.active(True)

Configure the access point with the ssid and password you’ve defined earlier:

ap.config(essid=ssid, password=password)

The following lines print the access point IP address

print('Connection successful')
print(ap.ifconfig())

By default, the IP address is 192.168.4.1

Now, the access point is created.

Socket Server

For demonstrations purposes, we’re creating a socket server that displays an “Hello, Wolrd!” message.

def web_page():
  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"</head><body><h1>Hello, World!</h1></body></html>"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  print('Content = %s' % str(request))
  response = web_page()
  conn.send(response)
  conn.close()

Connecting to the Access Point (AP)

After uploading the code as boot.py to your ESP32 or ESP8266 board, in your smartphone open your Wi-Fi settings and tap the MicroPython-AP network:

Connecting to ESP32 or ESP8266 Access Point (AP) using MicroPython

Enter the password you’ve defined earlier.

Connect to MicroPython Access Point Insert Password

Open your web browser and type the IP address http://192.168.4.1. The web server page should load:

MicroPython Access Point (AP) web browser test

You should have a similar message showing up on the Shell:

MicroPython Access Point (AP) web browser debugging information

Wrapping Up

In this tutorial you’ve learned how to set your EPS32 or ESP8266 boards as soft access point (soft-AP) using MicroPython. If you want to learn how to do the same using Arduino IDE, read the following guides:

Learn more about MicroPython with our resources:

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

8 thoughts on “MicroPython: ESP32/ESP8266 Access Point (AP)”

  1. What’s the benefit of microPython versus the normal ‘Arduino’ coding method? uP is something else to learn for people new to ESP8266 / ESP32

  2. Hi rui,

    the code works after i changed the lines “ap.active(True)”
    it must be the same order as in the example for station-mode:
    ################
    ssidAP = ‘ssid’
    passAP = ‘password’

    ap = network.WLAN(network.AP_IF)
    ap.active(True)
    ap.config(essid=ssidAP, password=passAP)
    #ap.active(True)
    ################

  3. Tested on Wemos D1R2 , works perfect with Android devices ( not on Apple/Safari stuff …).
    Noticed that the access point stays active even after new boot.py and main.py files are loaded … ( tested Oled example and works fine ) …
    So if you want to get rid of the AP you need to erase/reload micropython with esptool .
    Keep up the great Job , thank you very much !

    Bob

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.