Chapter 4 WiFi Working Modes
In this chapter, we’ll focus on the WiFi infrastructure for ESP32-WROOM.
ESP32-WROOM has 3 different WiFi operating modes: station mode, AP mode and AP+station mode. All WiFi programming projects must be configured with WiFi operating mode before using WiFi, otherwise WiFi cannot be used.
Project 4.1 Station mode
Component List
ESP32-WROOM x1 |
Type C Wire x1 |
|---|---|
|
|
Component knowledge
Station mode
When ESP32 selects Station mode, it acts as a WiFi client. It can connect to the router network and communicate with other devices on the router via WiFi connection. As shown below, the PC is connected to the router, and if ESP32 wants to communicate with the PC, it needs to be connected to the router.
Circuit
Connect Freenove ESP32 to the computer using the USB cable.
Sketch
Sketch_04.1_Station_mode
Because the names and passwords of routers in various places are different, before the Sketch runs, users need to enter the correct router’s name and password in the box as shown in the illustration above.
After making sure the router name and password are entered correctly, compile and upload codes to ESP32-WROOM, open serial monitor and set baud rate to 115200. And then it will display as follows:
When ESP32-WROOM successfully connects to “ssid_Router”, serial monitor will print out the IP address assigned to ESP32-WROOM by the router.
The following is the program code:
1/**********************************************************************
2 Filename : WiFi Station
3 Description : Connect to your router using ESP32
4 Auther : www.freenove.com
5 Modification: 2023/06/15
6**********************************************************************/
7#include <WiFi.h>
8
9const char *ssid_Router = "********"; //Enter the router name
10const char *password_Router = "********"; //Enter the router password
11
12void setup(){
13 Serial.begin(115200);
14 delay(2000);
15 Serial.println("Setup start");
16 WiFi.begin(ssid_Router, password_Router);
17 Serial.println(String("Connecting to ")+ssid_Router);
18 while (WiFi.status() != WL_CONNECTED){
19 delay(500);
20 Serial.print(".");
21 }
22 Serial.println("\nConnected, IP address: ");
23 Serial.println(WiFi.localIP());
24 Serial.println("Setup End");
25}
26
27void loop() {
28}
Include the WiFi Library header file of ESP32.
1#include <WiFi.h>
Enter correct router name and password.
1const char *ssid_Router = "********"; //Enter the router name
2const char *password_Router = "********"; //Enter the router password
Set ESP32 in Station mode and connect it to your router.
1WiFi.begin(ssid_Router, password_Router);
Check whether ESP32 has connected to router successfully every 0.5s.
1while (WiFi.status() != WL_CONNECTED){
2 delay(500);
3 Serial.print(".");
4}
Serial monitor prints out the IP address assigned to ESP32-WROOM
1Serial.println(WiFi.localIP());
Reference
- Class Station
Every time when using WiFi, you need to include header file “WiFi.h.”.
begin(ssid, password,channel, bssid, connect): ESP32 is used as Station to connect hotspot.
ssid: WiFi hotspot name
password: WiFi hotspot password
channel: WiFi hotspot channel number; communicating through specified channel; optional parameter
bssid: mac address of WiFi hotspot, optional parameter
connect: blloean optional parameter, defaulting to true. If set as false, then ESP32 won’t connect WiFi.
config(local_ip, gateway, subnet, dns1, dns2): set static local IP address.
status: obtain the connection status of WiFI
local IP(): obtian IP address in Station mode
disconnect(): disconnect wifi
setAutoConnect(boolen): set automatic connection Every time ESP32 is power on, it will connect WiFi aitomatically.
setAutoReconnect(boolen): set automatic reconnection Every time ESP32 disconnects WiFi, it will reconnect to WiFi automatically.
Project 4.2 AP mode
Component List & Circuit
Component List & Circuit are the same as in Section 30.1.
Component knowledge
AP mode
When ESP32 selects AP mode, it creates a hotspot network that is separate from the Internet and waits for other WiFi devices to connect. As shown in the figure below, ESP32 is used as a hotspot. If a mobile phone or PC wants to communicate with ESP32, it must be connected to the hotspot of ESP32. Only after a connection is established with ESP32 can they communicate.
Circuit
Connect Freenove ESP32 to the computer using the USB cable.
Sketch
Before the Sketch runs, you can make any changes to the AP name and password for ESP32 in the box as shown in the illustration above. Of course, you can leave it alone by default.
Compile and upload codes to ESP32-WROOM, open the serial monitor and set the baud rate to 115200. And then it will display as follows.
When observing the print information of the serial monitor, turn on the WiFi scanning function of your phone, and you can see the ssid_AP on ESP32, which is called “WiFi_Name” in this Sketch. You can enter the password “12345678” to connect it or change its AP name and password by modifying Sketch.
Sketch_04.2_AP_mode
The following is the program code:
1/**********************************************************************
2 Filename : WiFi AP
3 Description : Set ESP32 to open an access point
4 Auther : www.freenove.com
5 Modification: 2023/06/15
6**********************************************************************/
7#include <WiFi.h>
8
9const char *ssid_AP = "WiFi_Name"; //Enter the router name
10const char *password_AP = "12345678"; //Enter the router password
11
12IPAddress local_IP(192,168,1,100);//Set the IP address of ESP32 itself
13IPAddress gateway(192,168,1,10); //Set the gateway of ESP32 itself
14IPAddress subnet(255,255,255,0); //Set the subnet mask for ESP32 itself
15
16void setup(){
17 Serial.begin(115200);
18 delay(2000);
19 Serial.println("Setting soft-AP configuration ... ");
20 WiFi.disconnect();
21 WiFi.mode(WIFI_AP);
22 Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
23 Serial.println("Setting soft-AP ... ");
24 boolean result = WiFi.softAP(ssid_AP, password_AP);
25 if(result){
26 Serial.println("Ready");
27 Serial.println(String("Soft-AP IP address = ") + WiFi.softAPIP().toString());
28 Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
29 }else{
30 Serial.println("Failed!");
31 }
32 Serial.println("Setup End");
33}
34
35void loop() {
36}
Include WiFi Library header file of ESP32.
1#include <WiFi.h>
Enter correct AP name and password.
1const char *ssid_AP = "WiFi_Name"; //Enter the router name
2const char *password_AP = "12345678"; //Enter the router password
Set ESP32 in AP mode.
1WiFi.mode(WIFI_AP);
Configure IP address, gateway and subnet mask for ESP32.
1WiFi.softAPConfig(local_IP, gateway, subnet)
Turn on an AP in ESP32, whose name is set by ssid_AP and password is set by password_AP.
1WiFi.softAP(ssid_AP, password_AP);
Check whether the AP is turned on successfully. If yes, print out IP and MAC address of AP established by ESP32. If no, print out the failure prompt.
1if(result){
2 Serial.println("Ready");
3 Serial.println(String("Soft-AP IP address = ") + WiFi.softAPIP().toString());
4 Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
5}else{
6 Serial.println("Failed!");
7}
8Serial.println("Setup End");
Reference
- Class AP
Every time when using WiFi, you need to include header file “WiFi.h.”.
softAP(ssid, password, channel, ssid_hidden, max_connection):
ssid: WiFi hotspot name
password: WiFi hotspot password
channel: Number of WiFi connection channels, range 1-13. The default is 1.
ssid_hidden: Whether to hide WiFi name from scanning by other devices. The default is not hide.
max_connection: Maximum number of WiFi connected devices. The range is 1-4. The default is 4.
softAPConfig(local_ip, gateway, subnet): set static local IP address.
local_ip: station fixed IP address.
Gateway: gateway IP address
subnet: subnet mask
softAP(): obtian IP address in AP mode
softAPdisconnect (): disconnect AP mode.
Project 4.3 AP+Station mode
Component List
ESP32-WROOM x1 |
Type C Wire x1 |
|---|---|
|
|
Component knowledge
AP+Station mode
In addition to AP mode and station mode, ESP32 can also use AP mode and station mode at the same time. This mode contains the functions of the previous two modes. Turn on ESP32’s station mode, connect it to the router network, and it can communicate with the Internet via the router. At the same time, turn on its AP mode to create a hotspot network. Other WiFi devices can choose to connect to the router network or the hotspot network to communicate with ESP32.
Circuit
Connect Freenove ESP32 to the computer using the USB cable.
Sketch
Sketch_04.3_AP_Station_mode
It is analogous to Project 4.1 and Project 4.2. Before running the Sketch, you need to modify ssid_Router, password_Router, ssid_AP and password_AP shown in the box of the illustration above.
After making sure that Sketch is modified correctly, compile and upload codes to ESP32-WROOM, open serial monitor and set baud rate to 115200. And then it will display as follows:
When observing the print information of the serial monitor, turn on the WiFi scanning function of your phone, and you can see the ssid_AP on ESP32.
The following is the program code:
1/**********************************************************************
2 Filename : WiFi AP+Station
3 Description : ESP32 connects to the user's router, turning on an access point
4 Auther : www.freenove.com
5 Modification: 2023/06/15
6**********************************************************************/
7#include <WiFi.h>
8
9const char *ssid_Router = "********"; //Enter the router name
10const char *password_Router = "********"; //Enter the router password
11const char *ssid_AP = "WiFi_Name"; //Enter the router name
12const char *password_AP = "12345678"; //Enter the router password
13
14void setup(){
15 Serial.begin(115200);
16 Serial.println("Setting soft-AP configuration ... ");
17 WiFi.disconnect();
18 WiFi.mode(WIFI_AP);
19 Serial.println("Setting soft-AP ... ");
20 boolean result = WiFi.softAP(ssid_AP, password_AP);
21 if(result){
22 Serial.println("Ready");
23 Serial.println(String("Soft-AP IP address = ") + WiFi.softAPIP().toString());
24 Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
25 }else{
26 Serial.println("Failed!");
27 }
28
29 Serial.println("\nSetting Station configuration ... ");
30 WiFi.begin(ssid_Router, password_Router);
31 Serial.println(String("Connecting to ")+ ssid_Router);
32 while (WiFi.status() != WL_CONNECTED){
33 delay(500);
34 Serial.print(".");
35 }
36 Serial.println("\nConnected, IP address: ");
37 Serial.println(WiFi.localIP());
38 Serial.println("Setup End");
39}
40
41void loop() {
42}

