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)
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:
- 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:
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:
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:
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:
- 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.
What’s the benefit of microPython versus the normal ‘Arduino’ coding method? uP is something else to learn for people new to ESP8266 / ESP32
MicroPython is easier to learn for someone getting started with programming. We like to have both alternatives described in our blog, but currently Arduino coding has more libraries and community support
Thanks Ruis, I’ll have to look into uP further, I do have your course material on it.
What is the bandwidth and no of clients that can be connected with esp32?
Unfortunately I haven’t tried those types of tests, so I don’t have accurate information.
Regards,
Rui
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)
################
Hi Franz.
Thanks for sharing.
It is now fixed.
Regards,
Sara
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
Is there any more info on this?
Why does this happen?
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
Thank you Jean, it makes my iphone works.
And thank to everybody for this web site.
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.
Hi Oscar.
We have something like that using Arduino IDE and the SIM800L module.
Here’s a list of the tutorials:
https://randomnerdtutorials.com/esp32-sim800l-publish-data-to-cloud/
https://randomnerdtutorials.com/esp32-cloud-mqtt-broker-sim800l/
https://randomnerdtutorials.com/esp32-sim800l-send-text-messages-sms/
Regards,
Sara
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.
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?
Looks like someone did a complete copy/paste of your article, typos and all…
Hi.
Thanks for letting us know.
Unfortunately, it’s not just that article and it is happening all the time :/
Regards,
Sara
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?
Hi.
Unfortunately, at the moment, we don’t have any tutorial about that specific subject.
Regards,
Sara
Thank you for sharing
i use your code and it works only for one client.
if i disconnect that client and connect another doesnt work pretty fine
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.
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
Thanks for sharing.