ESP32 MicroPython Connect to WiFi: Step-by-Step Guide

What do you need before connecting an ESP32 to Wi-Fi with MicroPython?
Before touching any Wi-Fi code, make sure the basics are in place. The sources describe two typical setups: a plain ESP32 dev board running MicroPython v1.12, and an ESP32-WROVER module running MicroPython to push sensor readings over Wi-Fi to a PC. Either works for this guide.
| Item | Why it matters |
|---|---|
| ESP32 board (dev board or WROVER-based) | Has the Wi-Fi radio you are going to use |
| MicroPython firmware already flashed | The sources assume a working REPL; flashing itself is out of scope here |
| USB serial connection to your computer | Needed to run code and read error messages |
| Home network SSID and password | The credentials you will enter on the config page or in code |
| Awareness of RAM limits | A standard ESP32 ran out of memory allocating ~85 KB in the sources |
One honest note up front: the sources show the station interface being switched on and a credentials page being served, but they do not contain the exact scan/connect/status call sequence. Where a detail is missing below, I say so rather than guess, and you should confirm the exact method names against the official MicroPython network documentation for your firmware version.
\2A typical ESP32 development board, the kind used for MicroPython Wi-Fi projects. Image: Edwiyanto, CC BY-SA 4.0, via Wikimedia Commons
Step 1: Enable the station interface
MicroPython on the ESP32 exposes the Wi-Fi radio as a station (client) interface object. In the source project this object is called wlan_sta, and the only line the code actually needs before using the network is to make the interface active:
wlan_sta.active(True)
Think of this as turning the radio on. Nothing is joined yet; the interface is simply awake and ready to be told which network to use. The source calls this inside a request handler, which is unusual and not something you need to copy. A cleaner approach is to run it once at startup, before you create any sockets or serve any pages.
If wlan_sta does not exist in your script, that is because the source snippet created it elsewhere and did not show that line. You will need to construct the station interface object yourself from the network module; the sources do not show that constructor, so check your firmware docs for the exact spelling.
\2The ESP-WROOM-32 module that provides Wi-Fi and Bluetooth on many ESP32 boards. Image: Brian Krent, CC BY-SA 4.0, via Wikimedia Commons
Step 2: Provide the network credentials (code or config page)
There are two ways to tell the board which network to join. You can hard-code the SSID and password in your script, which is quick for a one-off test, or you can do what the source project does and build a small configuration page that the ESP32 serves itself.
The config-page approach works like this:
- On boot, the ESP32 starts a tiny web server using the
socketmodule. - You open that page from a phone or laptop.
- You type your home network's SSID and password into a form.
- The script reads those values and uses them for the actual connection.
The server side in the source is just a few lines:
server_socket = socket.socket()
server_socket.bind(addr)
server_socket.listen(1)
listen(1) means the board queues one pending connection at a time, which is fine for a config page that one person uses once. Incoming requests are parsed with a regular expression that pulls the path out of the HTTP request line, and then a simple if/elif chain picks a handler: the root path serves the form, a CSS path serves the stylesheet, and anything else returns a not-found response.
The sources do not show the line that passes the SSID and password into the connect call, so that final step is left for you to look up; the important design point is that credentials never have to be baked into the firmware.
Step 3: Keep the config page small enough to fit in RAM
This is where the source project tripped up, and it is the single most useful lesson in the sources for anyone building a Wi-Fi setup page on an ESP32.
The author wanted a nice-looking form and pulled in the Bootstrap stylesheet, bootstrap.min.css, which is about 141 kB. The handler read the whole file with f.read() and pushed it through client.send() in one go. The result on MicroPython v1.12 was:
MemoryError: memory allocation failed, allocating 84992 bytes
The board simply cannot allocate a contiguous block that large for a single read. The accepted fix is to stream the file in 1 KB pieces instead of loading it whole:
f = open(path, 'r')
while True:
chunk = f.read(1024)
if not chunk:
break
client.send(chunk)
client.close()
f.close()
Each iteration reads at most 1024 bytes, sends them, and loops until read() returns an empty value. Memory use stays flat no matter how big the file is.
Two practical takeaways:
- Chunked sending is the right pattern for any file you serve from an ESP32, not just CSS.
- Even with chunking, a 141 kB stylesheet is a lot for a page whose only job is to collect two text fields. A plain HTML form with no external CSS is faster, uses less flash and avoids the problem entirely.
If you do keep a stylesheet, store it as its own file, give it its own URL handler as the source does, and always close both the client socket and the file handle when you are done.
Step 4: Verify the ESP32 is actually on the network
The sources do not include a status check, so here is how to confirm the link using only the tools already in play:
- Watch the REPL. Keep the serial console open while the script runs. If something fails, MicroPython prints a traceback there, exactly as the
MemoryErrorabove appeared for the source author. - Reach the board from another device. The whole point of the config-page design is that a phone or laptop can load a page from the ESP32. If the page loads, sockets and the interface are working.
- Send something real. In the second source, an ESP32-WROVER running MicroPython samples an ADXL345 accelerometer and pushes the readings over Wi-Fi to a PC that runs the FFT. A one-way data push like this is a good end-to-end test: if the PC receives data, the connection is up and stable enough for actual work.
- Repeat after a power cycle. Unplug the board and let it boot again. A connection that only works once, right after you typed the credentials, is not finished yet.
If you want a programmatic check (for example, polling whether the interface reports a connection and printing the assigned IP address), the MicroPython network docs describe that; the sources here do not, so I am not quoting a call for it.
Common mistakes when connecting ESP32 MicroPython to Wi-Fi
| Mistake | What happens | What to do instead |
|---|---|---|
Reading a large file with f.read() and sending it in one call |
MemoryError: memory allocation failed on a ~141 kB file |
Loop over f.read(1024) and send each chunk |
| Serving a heavy CSS framework from the board | Slow page loads and memory pressure | Use a minimal HTML form, or strip the stylesheet down |
| Activating the interface inside a request handler | Works but is confusing and repeats work on every request | Call wlan_sta.active(True) once at startup |
| Forgetting to close the client socket or file | Resources leak until the next request fails | Always client.close() and f.close() after sending |
| Expecting a client to tolerate half-finished responses | Browsers hang waiting for the rest of a file | Finish the chunk loop before closing |
| Hard-coding credentials for a device you will move | Needs a reflash every time the network changes | Keep the config-page approach so credentials can be changed in the field |
A less obvious point from the accelerometer source: Wi-Fi is often just the transport, and the real bottleneck sits elsewhere. That author's timing jitter came from a polling loop on the I2C side, not from the network, and the fix was to use the sensor's own FIFO buffer. If your Wi-Fi project "feels slow," check the sensor loop before blaming the radio.
What the sources do not tell you
To keep this guide trustworthy, here is what you still need to confirm elsewhere, because the sources simply do not contain it:
- The exact constructor used to create the station interface object (
wlan_stain the source) from thenetworkmodule. - The method that takes an SSID and password and starts the join.
- How to read the connection status or the assigned IP address.
- Behaviour on newer MicroPython releases; the only version named in the sources is v1.12, and the memory numbers quoted above come from that version.
The MicroPython ESP32 documentation covers all four, and our own MicroPython setup guide on this site walks through a working example. Use this article for the overall flow, the credentials-page pattern and the memory pitfalls, and lean on those references for the specific API calls.
Frequently asked questions
Can I let users enter Wi-Fi credentials without reflashing the ESP32?
Yes. The source project runs a small socket-based web server on the ESP32 at boot and serves a config page where the SSID and password for the home network are typed in.
Why does my MicroPython web server throw MemoryError when serving a file?
Because f.read() loads the entire file into RAM before sending. On MicroPython v1.12 a ~141 kB CSS file failed at an 84992-byte allocation. Read and send the file in 1 KB chunks instead.
Which call turns on the Wi-Fi station interface in MicroPython?
The source uses wlan_sta.active(True) on the station interface object. The sources do not show how that object is created, so check the MicroPython network docs for the constructor.
Is MicroPython on ESP32 fast enough to stream sensor data over Wi-Fi?
One source uses an ESP32-WROVER running MicroPython to read an ADXL345 over I2C and push the data over Wi-Fi to a PC for FFT analysis. The timing problems that author hit were in the I2C polling loop, not the Wi-Fi link.
How many pending connections should the config server accept?
The source calls server_socket.listen(1), which is enough for a setup page used by one person at a time.
Getting an ESP32 onto Wi-Fi from MicroPython comes down to enabling the station interface, handing it your credentials and then verifying the link from another device. The sources add a valuable real-world pattern, a self-hosted config page, and an equally valuable warning: keep anything you serve from the board small or stream it in chunks, or you will hit MemoryError long before you see a Wi-Fi problem.
Sources
stackoverflow.comMicropython Webserver: Serve large textfiles without memory allocation failureelectronics.stackexchange.comHow can I read from an accelerometer through i2c/spi for ~1 second at frequency>2kHz with precise distancing between measuresThis guide summarises the public sources listed above in our own words. Check the original documentation before relying on exact figures.

