Unit H - ESP32 Wireless

41. WiFi Fundamentals: Station vs Access Point

Join the LAN, then build a network health meter that reports RSSI and reconnects if the link drops.

Estimated time 3 hours

Learning outcomes

  • Contrast Station (STA) with SoftAP and when each is used
  • Join WiFi, print IP, gateway and RSSI
  • Build a network health meter with millis reporting
  • Classify link quality as strong / ok / weak from RSSI
  • Attempt reconnect when the STA link drops

Parts and preparation

ESP32 DevKit (classic Wi-Fi capable), USB data cable, 2.4 GHz WiFi with known SSID and password. No external components.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

WiFi Fundamentals: Station vs Access Point instructional connection diagram

Project: Network health meter

Before you host pages or call APIs, you need a trustworthy join - and a way to see if the link is still healthy. Sketch 1 proves Station connect and prints the address you will use in later labs. Sketch 2 is the useful meter: every 5 seconds it prints RSSI with a strong/ok/weak label and tries to reconnect if WiFi drops.

SoftAP (board as hotspot) returns in lesson 45 for first-boot setup.

ESP32 as Station to a router versus ESP32 as Access Point
STA joins a router for this project. SoftAP is for setup later.
ModeWho creates the networkThis unit
STAExisting routerHealth meter + later web/MQTT
AP / SoftAPESP32Lesson 45 provisioning

What happens when you join

Association authenticates with the SSID/password. DHCP then leases localIP(), subnet and gateway. Write the IP down for lesson 42. RSSI is dBm - more negative is weaker (about -40 strong, -80 weak).

ValueMeaning
localIP()ESP32 address on the LAN
gatewayIP()Usually the router
RSSI()Signal strength in dBm
WL_CONNECTEDSafe to use networking APIs

Status and pre-flight

Poll WiFi.status() until WL_CONNECTED. Classic ESP32 needs 2.4 GHz. Guest WiFi often blocks device-to-device traffic - use the main LAN for web labs.

CheckWhy
SSID exactSpaces and capitals matter
Password correctWrong password = endless dots
2.4 GHzNo 5 GHz radio on classic ESP32
Data USB cableCharge-only = no Serial/port

Credentials

Replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD before upload. Never commit real passwords to public repos. Products need SoftAP setup (lesson 45).

Wiring and safe build sequence

  1. USB data cable PC to ESP32
  2. Board: ESP32 Dev Module (or matching), correct port
  3. Serial Monitor 115200
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.

Worked sketch 1: Join the LAN and prove the IP

Download .ino sketch

What this sketch is for: Discovery: Station mode joins the router and prints IP, gateway and RSSI once. Write the IP down - lesson 42 opens http://that-ip/.

#include <WiFi.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

void setup() {
  Serial.begin(115200);
  delay(500);

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address:  ");
  Serial.println(WiFi.localIP());
  Serial.print("Gateway:     ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("RSSI (dBm):  ");
  Serial.println(WiFi.RSSI());
}

void loop() {
}

How the code works

  1. Replace the two placeholders with your 2.4 GHz SSID and password.
  2. WiFi.mode(WIFI_STA) makes Station intent explicit.
  3. Empty loop on purpose - sketch 2 adds the meter.

Worked sketch 2: Network health meter

Download .ino sketch

What this sketch is for: Useful monitor you could leave running on a bench: every 5 s print RSSI with strong/ok/weak, and call WiFi.reconnect() if the link drops.

#include <WiFi.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
unsigned long lastReport = 0;

const char* qualityLabel(int rssi) {
  if (rssi >= -55) return "strong";
  if (rssi >= -70) return "ok";
  return "weak";
}

void setup() {
  Serial.begin(115200);
  delay(500);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Health meter IP ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (millis() - lastReport < 5000) return;
  lastReport = millis();

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Link down - reconnecting...");
    WiFi.reconnect();
    return;
  }

  int rssi = WiFi.RSSI();
  Serial.print("RSSI ");
  Serial.print(rssi);
  Serial.print(" dBm (");
  Serial.print(qualityLabel(rssi));
  Serial.println(")");
}

How the code works

  1. millis timer avoids blocking delay(5000) in loop.
  2. Bands are teaching defaults - tune for your building.
  3. Walk away from the router and watch strong -> ok -> weak.

Test and record evidence

Expected result: Sketch 1: dots, then IP, gateway and RSSI. Sketch 2: every ~5 s a quality line; moving the board changes the label; unplugging the router path shows reconnecting.

Practical evidence checklist

Common faults and checks
  • Endless dots: SSID, password, 2.4 GHz.
  • Guest WiFi: use main LAN for later web labs.
  • No Serial: 115200, data cable, EN/RESET.
Extension challenge: Log the minimum RSSI seen since boot, and blink GPIO 2 when quality is weak.

Check your understanding

Q1. What project does sketch 2 implement?

Show answer

A network health meter with RSSI quality labels.

Q2. What does STA mode mean?

Show answer

The ESP32 joins an existing router.

Q3. What does localIP() return?

Show answer

The ESP32 IPv4 address on the LAN.

Q4. What does a more negative RSSI mean?

Show answer

Weaker signal.

Q5. Why write the IP down?

Show answer

Later labs open http://that-ip/ on the same WiFi.

Q6. Why might 5 GHz-only fail?

Show answer

Classic ESP32 is 2.4 GHz only.

Q7. What does sketch 2 do on link down?

Show answer

Print reconnecting and call WiFi.reconnect().

Q8. Why avoid committing real passwords?

Show answer

Credentials can be misused; use placeholders.