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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

24 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

    Reply
  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)
    ################

    Reply
  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

    Reply
  4. Hi,

    to get a secured AP, I change the line ap.config to add authentication mode.

    ap.config(essid=ssid,authmode=network.AUTH_WPA_WPA2_PSK, password=password)

    If I don’t, the AP is open without password

    ESP32 Dev Kit

    Reply
  5. Thank you for your tutorials, great job!
    A suggestion to complete this tutorial would be to create routing between wifi (or even ethernet) and mobile network (additional module GPRS needed). This way you would have internet connection in a small remote installation without any router.

    Reply
  6. Thank you, Rui, for the excellent tutorial!
    We got it to work with using an Android phone as a client, and also a wireless Windows10 laptop.
    We could not get it to work with an iPad client, and welcome any comments about that. Is Apple fussy about something?

    For those that missed it, there are important comments above about passwords and when to activate WLAN.

    My next step is probably to put much of this code in its own thread so I could run a main control thread.

    But what does everybody recommend: build a web server from scratch or find a web server package? Seems like web sockets are a pretty good way to go.

    Reply
    • I too am having difficulty getting Safari on Mac or Mobile Safari on iOS to successfully load the page. I get the ‘server unexpectedly dropped the connection’ message. I’ve confirmed that it works on Chrome for Mac, and any browser on Windows. Any solution?

      Reply
  7. Hi, very useful and almost what I need as a newbie to python! I see you have a tutorial on AP mode for sending sensor data between 2 esp8266’s using Arduino and C but not for python and this tutorial is about sending a web page so not quite what I need. Can you point me to anything?

    Reply
  8. i wanted to host softAP from Raspberry PI pico with esp8266 module. I can connect to internet using AT commands. I can browse Json in a loop interval and if there is any change it will set GPIO values according to JSON.

    My question and problem is.. I’ve configured softAP on Raspberry PI pico. And it is working great. I can press a button as GPio (input) to activate softAP on devices… it appears in wifi APs.. and can be connected with any Phone or laptop…
    But i’m unable to serve any page from RbPi Pico’s IP address. I want to serve a web page with 2 or 3 fields ..where use could input their HOME’ or OFFICE’s wifi ID and Password and submit these values to Pico’s micropython variables.. which I will further make this pico an IOT devices.

    Can you please share any tutorial if already available if help me making this “html page to be shown to softAP clients and get input fields data”?

    Thank you.

    Reply
  9. I got this working on iPhone with two changes:

    The ap.config() call has an extra parameter to tell it to use WPA/PSK
    ap = network.WLAN(network.AP_IF)
    ap.active(True)
    ap.config(essid=ssid,authmode=network.AUTH_WPA_WPA2_PSK, password=password)
    An HTTP status line is sent before the main response. I also changed to use the write() method, although that may not be significant

    conn.write(‘HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n’)
    conn.write(response)
    conn.close()

    With these changes I can connect using my iPhone 12 and either Safari or Chrome browser

    Reply

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.