ESP32 Part 2 - Wireless

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.

Estimated time 3 hours

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.

IncludeLibrary Manager nameAuthorInstall note
WiFi.hWiFiEspressifBuilt 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.

ESP32 as Station to a router versus ESP32 as Access Point
Station: the ESP32 joins a router. Access point: the ESP32 is the network.
ModeWho makes the networkUsed in
Station (STA)Your routerMost lessons: web page, internet data, MQTT
Access point (AP)The ESP32Sketch 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.

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

CheckWhy
Network name typed exactlySpaces and capital letters matter
Password correctA wrong password gives dots forever
2.4 GHz networkThe classic ESP32 cannot see 5 GHz networks
Not a guest networkGuest 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

  1. USB data cable from 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 your Wi-Fi

Download .ino

What 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

  1. The block from WiFi.begin to "connected" is the same in every Wi-Fi sketch in this course.
  2. != means 'is not equal to', so the while loop keeps going until the status is WL_CONNECTED.
  3. 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 .ino

What 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

  1. The password must be at least 8 characters, or the network is not created.
  2. No router is involved - this works anywhere, even with no internet.
  3. Your phone may warn that the network has no internet. That is expected.

Test and record evidence

Expected result: Sketch 1: Connecting with dots, then connected, the IP address, the signal strength and the free memory. Sketch 2: ESP32-yourname appears on your phone; after you join, Devices joined changes from 0 to 1.

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).
Challenge (extension - optional): In sketch 1, move the signal strength print into loop() with a 2-second delay, then walk away from the router and watch the number fall.

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.