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.
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)
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:
- Thonny IDE:
- uPyCraft IDE:
- Getting Started with uPyCraft IDE
- Install uPyCraft IDE (Windows, Mac OS X, Linux)
- Flash/Upload MicroPython Firmware to ESP32 and ESP8266
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.
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).
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()
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:
Configure the access point with the ssid and password you’ve defined earlier:
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.
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:
Enter the password you’ve defined earlier.
Open your web browser and type the IP address http://192.168.4.1. The web server page should load:
You should have a similar message showing up on the Shell:
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:
- ESP32 Access Point (AP) for Web Server using Arduino IDE
- ESP8266 NodeMCU Access Point (AP) for Web Server using Arduino IDE
Learn more about MicroPython with our resources:
- MicroPython with ESP32 and ESP8266: Interacting with GPIOs
- ESP32/ESP8266 MicroPython Web Server – Control Outputs
- MicroPython – Getting Started with MQTT on ESP32/ESP8266
- MicroPython Programming with ESP32 and ESP8266 (eBook)
Thanks for reading.