WiFi Connect Sequence
Run the WiFi Connect Sequence MicroSim Fullscreen
About This MicroSim
Your robot joins a WiFi network in a fixed order of steps. This MicroSim shows each step as an arrow on a sequence diagram. A sequence diagram has one vertical line for each thing that talks, and arrows for the messages between them. Here the two lines are the robot and the access point (the router).
The steps are:
WLAN(STA_IF)creates the WiFi object in station mode.active(True)turns on the WiFi chip.connect(SSID, PASSWORD)asks the router to let the robot join. This call returns right away. It does not wait!- The router checks the password (authentication).
- The router gives the robot an IP address with DHCP.
- Meanwhile, the
whileloop callsisconnected()every 0.1 seconds until it returnsTrueor the timeout runs out. ifconfig()[0]reads the robot's IP address.
The code panel on the right is the connect code from
Chapter 11.
The line that is running lights up. The orange bar shows the time since start = ticks_ms().
The black mark on the bar is the timeout. The dark box at the bottom is the serial console, showing exactly what the robot prints.
The circle on the robot is a status light: orange while connecting, green when connected, and red when the connection fails.
How to Use
- Press Next Step to run one step at a time, or Auto Play to run them all.
- At step 5 the loop starts. The clock runs by itself (at 2x speed) until the robot connects or gives up.
- Pick a different Scenario and run it again. Watch the arrows, the
wlan.status()value, and the console. - Move the Timeout (s) slider. The number in the
if ticks_diff(...)line changes to match. - Uncheck Use secrets.py to see what the code looks like with the password typed right into
main.py.
Try this challenge: Choose "Slow router (connects at 12 s)" with the default 10 second timeout. The robot says it failed, even though the router was about to let it in. Fix it using only the timeout slider.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Learning Objective
Students will sequence the WiFi connection calls (WLAN, active, connect, polling isconnected, ifconfig) and explain the robot's behavior when the network is missing, the password is wrong, or the timeout expires (Bloom's Taxonomy: Apply).
Grade Level
Grades 8–12
Duration
15–20 minutes
Prerequisites
- WiFi, the WLAN object, and
isconnected()from Chapter 11: Wireless Networking and Web Servers. ticks_ms()andticks_diff()non-blocking timing from Chapter 4: Control Flow, Functions, and Exception Handling.- The
secrets.pypattern from Chapter 5: Data Structures, Modular Programming, and Version Control.
Activities
- Order the calls (4 min). Before opening the sim, give students the seven calls on cut-up cards and ask them to put them in order. Then step through the Success scenario and have them correct their order. Emphasize that
connect()returns before the connection exists. - Failure analysis (6 min). Students run "Wrong password" and "Network out of range" and record, for each, which arrow failed, the final
wlan.status()value, how many timesisconnected()was called, and the console output. They explain why both cases print the same message even though the causes differ. - Timeout tuning (4 min). Students run both slow-router scenarios. They find the smallest timeout that works for the 12 s router and discuss the trade-off: a long timeout tolerates slow routers but makes a real failure take longer to report.
- Secrets check (3 min). Students uncheck "Use secrets.py" and explain what would be exposed if this
main.pywere pushed to a public repository. - Debugging extension (optional). Students modify the chapter code to print
wlan.status()inside the loop, so the console reports the specific failure reason.
Assessment
- Formative: Ask "At step 3, is the robot connected yet? How do you know?" Look for answers that reference the loop or the status value.
- Exit ticket: "Your robot prints
WiFi connection failed!. List three different causes and one way to tell them apart." (Wrong password, network out of range or 5 GHz only, timeout too short; checkwlan.status().) - Rubric (4-point): Exemplary — correct call order with the reason
connect()must be followed by a polling loop, all three failure modes distinguished using status codes, and a justified timeout choice. Proficient — correct order and two failure modes explained. Developing — correct order, but failure explanations are generic. Beginning — cannot order the calls or treatsconnect()as blocking.
References
- Chapter 11: Wireless Networking and Web Servers — the WiFi connect code this sim steps through.
- MicroPython
network.WLAN— documentation foractive(),connect(),isconnected(),ifconfig(), and theSTAT_status values. - Connecting to the Internet with Raspberry Pi Pico W — Raspberry Pi's official guide to WiFi on the Pico W, including connection status codes.
- Dynamic Host Configuration Protocol — Wikipedia article on how a router hands out IP addresses.
- Wireless access point — Wikipedia article on the device the robot connects to.
- Timeout (computing) — Wikipedia article on why programs stop waiting after a set time.