E08. Wi-Fi Fundamentals: Station vs Access Point
Join your Wi-Fi network and print the ESP32's address, then make the ESP32 create a Wi-Fi network of its own.
Learning outcomes
- Explain the two Wi-Fi modes: joining a network (station) and making one (access point)
- Join a Wi-Fi network and print the IP address and signal strength
- Make the ESP32 create its own network and count who joins
- Keep real Wi-Fi passwords out of shared code
Parts and preparation
ESP32 DevKit (classic Wi-Fi capable), USB data cable, a 2.4 GHz Wi-Fi network with known name and password, a phone. No external components.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
Libraries for this lesson
In Arduino IDE 2 open Tools → Manage Libraries…. Search by the Library Manager name and install the package by the exact author below. Similar names from other authors can use different APIs and will break the example.
| Include | Library Manager name | Author | Install note |
|---|---|---|---|
WiFi.h | WiFi | Espressif | Built into the esp32 Arduino core. |
What you will do
The ESP32 can use Wi-Fi in two ways.
Sketch 1 joins your Wi-Fi network, like a phone does, and prints the address it was given. Sketch 2 makes the ESP32 create a Wi-Fi network of its own that your phone can join.
| Mode | Who makes the network | Used in |
|---|---|---|
| Station (STA) | Your router | Most lessons: web page, internet data, MQTT |
| Access point (AP) | The ESP32 | Sketch 2, and Wi-Fi setup in ESP32 Lesson 12 |
Joining a network
WiFi.begin(ssid, password) starts joining. It takes a few seconds, so the sketch waits in a while loop, printing a dot every half second, until WiFi.status() says WL_CONNECTED.
Once joined, the router gives the ESP32 an IP address such as 192.168.1.42. Write it down - in ESP32 Lesson 09 you type it into a browser.
| Command | Gives you |
|---|---|
| WiFi.localIP() | The ESP32's address on your network |
| WiFi.RSSI() | Signal strength in dBm: -40 is strong, -80 is weak |
| WiFi.status() | WL_CONNECTED once the ESP32 has joined |
Before you upload
Most join problems are one of these.
| Check | Why |
|---|---|
| Network name typed exactly | Spaces and capital letters matter |
| Password correct | A wrong password gives dots forever |
| 2.4 GHz network | The classic ESP32 cannot see 5 GHz networks |
| Not a guest network | Guest Wi-Fi often stops your phone reaching the ESP32 |
Keep passwords private
Type your network name and password in place of YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD before you upload. Put the placeholders back before you share the sketch with anyone. ESP32 Lesson 12 shows how real products avoid typing passwords into code.
Wiring and safe build sequence
- USB data cable from PC to ESP32
- Board: ESP32 Dev Module (or matching), correct port
- Serial Monitor 115200
Worked sketch 1: Join your Wi-Fi
Download .inoWhat this sketch is for: Type your network name and password into the two lines near the top, upload, and watch Serial. Write down the IP address - ESP32 Lesson 09 needs it.
#include <WiFi.h> // the Wi-Fi commands
String ssid = "YOUR_WIFI_SSID"; // your Wi-Fi network name
String password = "YOUR_WIFI_PASSWORD"; // your Wi-Fi password
void setup() { // runs once
Serial.begin(115200); // start Serial at the ESP32 speed
WiFi.begin(ssid, password); // start joining the network
Serial.print("Connecting"); // tell the user we are trying
while (WiFi.status() != WL_CONNECTED) { // repeat until we have joined
delay(500); // wait half a second
Serial.print("."); // print a dot each time
}
Serial.println(" connected"); // we are on the network
Serial.print("IP address: "); // a label
Serial.println(WiFi.localIP()); // the address the router gave us
Serial.print("Signal strength: "); // a label
Serial.print(WiFi.RSSI()); // signal strength, e.g. -60
Serial.println(" dBm"); // the unit
Serial.print("Free memory: "); // a label
Serial.println(ESP.getFreeHeap()); // lower than without Wi-Fi
}
void loop() { // nothing to repeat in this sketch
}How the code works
- The block from WiFi.begin to "connected" is the same in every Wi-Fi sketch in this course.
- != means 'is not equal to', so the while loop keeps going until the status is WL_CONNECTED.
- Free memory is lower than in ESP32 Lesson 02 - Wi-Fi uses tens of thousands of bytes.
Worked sketch 2: Make your own network
Download .inoWhat this sketch is for: Now the ESP32 is the network. Upload, then look for ESP32-yourname in your phone's Wi-Fi list and join it with the password lab12345. Change yourname so it does not clash with a classmate.
#include <WiFi.h> // the Wi-Fi commands
void setup() { // runs once
Serial.begin(115200); // start Serial at the ESP32 speed
WiFi.softAP("ESP32-yourname", "lab12345"); // make a network: name, password
Serial.print("Network ready. Address: "); // a label
Serial.println(WiFi.softAPIP()); // the ESP32's address, usually 192.168.4.1
}
void loop() { // runs over and over
Serial.print("Devices joined: "); // a label
Serial.println(WiFi.softAPgetStationNum()); // how many phones or PCs have joined
delay(2000); // check every 2 seconds
}How the code works
- The password must be at least 8 characters, or the network is not created.
- No router is involved - this works anywhere, even with no internet.
- Your phone may warn that the network has no internet. That is expected.
Test and record evidence
Practical evidence checklist
Common faults and checks
- Dots forever: check the network name and password, and that the network is 2.4 GHz.
- Sketch 2 network missing: the password must be at least 8 characters.
- No Serial: set 115200, use a data cable, and press EN (reset).
Check your understanding
Q1. What is station mode?
Show answer
The ESP32 joins an existing network, like a phone does.
Q2. What is access point mode?
Show answer
The ESP32 creates its own network for other devices to join.
Q3. What does WiFi.localIP() give you?
Show answer
The ESP32's address on your network.
Q4. Which is the stronger signal, -45 dBm or -75 dBm?
Show answer
-45 dBm (closer to zero is stronger).
Q5. Why does the sketch print dots?
Show answer
To show it is still waiting to join the network.
Q6. Why might a 5 GHz network not work?
Show answer
The classic ESP32 only uses 2.4 GHz.
Q7. How long must an access point password be?
Show answer
At least 8 characters.
Q8. Why put the placeholders back before sharing a sketch?
Show answer
So you do not give away your Wi-Fi password.