Built-in project · ESP32-DevKitC V4

MicroPython WiFi station mode on an ESP32, on an ESP32-DevKitC V4

MicroPython on an ESP32: network.WLAN(STA_IF) joins Mokxi’s simulated access point and prints the IP address the router hands out. 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.

Beginner 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Joining WiFi with network.WLAN in station mode
  • Waiting for a connection with isconnected
  • What an IP address is and where it comes from
  • Keeping the network name and password in constants
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 1

How it works

Station mode means the board joins an existing WiFi network, like a phone does. Here that network is Mokxi's simulated access point, named Mokxi-Lab with the password breadboard.

The script turns on the station interface and calls sta.connect. It waits in a loop, checking every 250 milliseconds, until it is connected. Then it prints the IP address the router gave it. After that, every 10 seconds it prints whether the link is still up.

Parts list

1 part, plus the jumper wires. They retail for about $10; on Mokxi they're free.

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.

main.py · ESP32-DevKitC V4
# Station mode: join a WiFi network as a client and print the address
# the router gives the board. Here the network is Mokxi's simulated
# access point; its name and password are the board's WiFi properties.
# On a real board, put in your own network's.
import network
import time

SSID = "Mokxi-Lab"
PASSWORD = "breadboard"

sta = network.WLAN(network.STA_IF)
sta.active(True)
print("Connecting to", SSID)
sta.connect(SSID, PASSWORD)
while not sta.isconnected():
    time.sleep_ms(250)
print("Connected, IP address:", sta.ifconfig()[0])

# then keep an eye on the link, as a program that uses it would
while True:
    time.sleep(10)
    print("still connected" if sta.isconnected() else "connection lost")

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It keeps waiting and never connects This project
    Check that SSID and PASSWORD match the network exactly. In Mokxi they are the board's WiFi properties. On a real board, use your own network's.
  • 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.