MicroPython WiFi AP and station at once on an ESP32, on an ESP32-DevKitC V4
MicroPython on an ESP32: the board starts its own hotspot and joins Mokxi’s simulated access point at the same time, and prints both addresses. The whole build, an ESP32-DevKitC V4, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $10. On Mokxi they're free.
What you'll learn
- Access point mode versus station mode
- Running both modes at once
- Setting a hotspot name, password and security
- Checking a connection in a loop
- Difficulty: Intermediate
- Time: About 10 minutes
- Board: ESP32-DevKitC V4
- Parts: 1
How it works
This MicroPython program has no parts to wire. The ESP32 starts its own hotspot, named ESP32-Hotspot with the password 12345678, and prints the hotspot name and its address.
At the same time it joins a network as a client. It connects to Mokxi-Lab with the password breadboard, waits until it is connected, and prints its IP address. Then every 10 seconds it prints whether it is still connected. Nothing joins the hotspot in the simulator.
Parts list
1 part, plus the jumper wires. They retail for about $10; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# AP and station at once: the ESP32 starts its own hotspot and also joins
# a network as a client. Here the network it joins is Mokxi's simulated
# access point, and nothing can join its hotspot.
import network
import time
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid="ESP32-Hotspot", password="12345678", authmode=network.AUTH_WPA_WPA2_PSK)
print("Hotspot:", ap.config("essid"), ap.ifconfig()[0])
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect("Mokxi-Lab", "breadboard")
while not sta.isconnected():
time.sleep_ms(250)
print("Station IP address:", sta.ifconfig()[0])
while True:
time.sleep(10)
print("station", "still connected" if sta.isconnected() else "connection lost")
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It never gets past the station connection. This project
The network name and password insta.connectmust match the network you want to join. - You cannot join the hotspot. This project
In the simulator nothing can join it. On a real board, use the name and password printed by the program. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.