Level 3Unit A · Session 2
2

Unit A · On the network

Getting on a network

What we are making: a board that is reachable. By the end of this session another device in this room can talk to yours, and you will know which of the two modes your school allows.

The idea

A network is just an agreement about addresses. Your board needs an address, and something else needs to be able to reach it. There are two ways to arrange that, and they are genuinely different situations — not a real one and a backup.

■ESP32runs the networkserves the page▭Phone or laptopjoins ROBOT-01opens 192.168.4.1its own WiFiNo school network involved. No internet. Works in a field.MODE A — the board makes its own network

Mode A — the board creates a network. Nothing else has to exist.

■ESP32joins school WiFigets an address◉School routerhands outaddresses▭Laptopsame WiFiopens that addressEverything stays inside school. Still no internet needed.MODE B — the board joins a network that already exists

Mode B — the board joins a network somebody else runs.

Mode A: the board makes its own WiFi

The board becomes an access point. It hands out addresses. Its own address is always 192.168.4.1. You join that network from your phone exactly like joining any other WiFi.

Mode A — make a network
1#include <WiFi.h>
2 
3const char* AP_NAME = "ROBOT-01"; // change 01 to your team number
4const char* AP_PASS = "robotics2027"; // must be 8 characters or more
5 
6void setup() {
7 Serial.begin(115200);
8 WiFi.softAP(AP_NAME, AP_PASS);
9 Serial.print("Network created: ");
10 Serial.println(AP_NAME);
11 Serial.print("Open this address: ");
12 Serial.println(WiFi.softAPIP()); // always 192.168.4.1
13}
14 
15void loop() {
16 Serial.print("Devices joined: ");
17 Serial.println(WiFi.softAPgetStationNum());
18 delay(3000);
19}
  • Line 1 — the WiFi library ships with the ESP32 board package. Nothing to install.
  • Line 3 — change this. If three teams run ROBOT-01 in one room, nobody can tell which is theirs.
  • Line 4 — a password under 8 characters is silently ignored and your network appears open.
  • Line 8 — one line. That is the entire access point.
  • Line 19 — how many devices joined. A cheap, useful check.
⚠ Careful
Rename your network. The single most common wasted twenty minutes in this unit is three teams debugging each other's boards because they all left it as ROBOT-01.

Mode B: the board joins the school WiFi

Now the board is a client, like your laptop. The router gives it an address, and you have to ask what that address turned out to be.

Mode B — join a network
1#include <WiFi.h>
2 
3const char* SSID = "SCHOOL-WIFI"; // exactly as it appears, capitals matter
4const char* PASS = "____________";
5 
6void setup() {
7 Serial.begin(115200);
8 WiFi.begin(SSID, PASS);
9 Serial.print("Joining");
10 
11 int tries = 0;
12 while (WiFi.status() != WL_CONNECTED && tries < 30) {
13 delay(500);
14 Serial.print(".");
15 tries = tries + 1;
16 }
17 
18 if (WiFi.status() == WL_CONNECTED) {
19 Serial.println();
20 Serial.print("Joined. My address is: ");
21 Serial.println(WiFi.localIP());
22 } else {
23 Serial.println();
24 Serial.println("Could not join. Check name, password, and 2.4GHz.");
25 }
26}
27 
28void loop() { }
  • Line 12 — this loop is the important idea. You wait, but not forever. Thirty tries, half a second each, then give up and say so.
  • Line 18 — you check whether it worked. A program that assumes it worked is the kind that hangs silently on a roof in March.
  • Line 21 — WiFi.localIP() is the address you type into a browser next session.
🔧 If it goes wrong
The ESP32 cannot see 5 GHz networks at all. If your school WiFi is 5 GHz only, Mode B is impossible no matter what you type, and the board is not broken.

Find out which mode you have — today

Ten minutes. Do this now, write the answer on the inside cover of this book, and tell your teacher. The answer decides how your team works for the rest of the year.

  1. Upload the Mode B program with your school's network name and password.
  2. Watch the serial monitor. Did it join, and did it print an address?
  3. If it joined: from a laptop on the same WiFi, try to open that address in a browser next session.
  4. If it did not join after 15 seconds: you are on Mode A. That is a complete answer, not a failure.
  5. Write it down: our school allows / does not allow the board on WiFi.
What you seeWhyWhat to do
Joins, then drops after a minuteNetwork kicks unknown devices, or weak signal.Note it. Session 7 is entirely about surviving this.
Never joins, password is right5GHz only, or the network needs a login page.Use Mode A. A network with a sign-in page cannot be joined by a board.
Joins but laptop cannot reach itClient isolation — devices cannot see each other.Very common on school and guest WiFi. Use Mode A.
Address starts 169.254It did not really get an address.Treat as not joined.

What you learned

  • A board can join a network or be one.
  • Waiting for something that may never happen needs a limit and an else.
  • Client isolation and 5GHz are real constraints that no amount of code fixes.
  • You now know which mode your team is building in.
Challenge optional — only if you finish early
  • Find out your board's MAC address and what it is for.
1. A new board