Home / References / ESP32 Library / WiFi API / Station
Description
The begin
method is part of the ESP32 WiFi library. It is used to initialize the Wi-Fi connection in Station Mode (STA). This method configures the ESP32 to connect to a specified Wi-Fi network or to start Wi-Fi without predefined credentials, depending on the usage.
Usage
The begin
method can be used in two ways:
– With Arguments:
WiFi.begin(const char* ssid, const char *passphrase)
Initializes the Wi-Fi module and attempts to connect to a specified network using the provided SSID and password. This is the most common usage for connecting to a known Wi-Fi network.
– Without Argument:
WiFi.begin()
If begin() is called without providing any parameters, the ESP32 will be initialized in Station mode (STA mode), but it will not attempt to connect to any Wi-Fi network.In this case, the ESP32’s Wi-Fi module will be activated and ready to accept subsequent configurations (for example, setting the SSID and password through other methods or dynamically assigning the network via a Wi-Fi configuration tool).
Arguments
WiFi.begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)
When using the begin
method with argument, the following arguments are required:
- ssid (const char*):The SSID (Service Set Identifier) of the Wi-Fi network to which the ESP32 will connect.
- password (const char*):The password for the Wi-Fi network. If the network is open (no password), this argument can be omitted or set to
NULL
. - channel (int32_t, optional):The Wi-Fi channel number of the access point.
- bssid (const uint8_t*, optional):The MAC address of the access point to connect to, useful if multiple APs share the same SSID.
- connect (bool, optional):A flag indicating whether to initiate the connection immediately.
Return Value
The WiFi.begin()
method in the ESP32 Arduino core library does not return any value. Therefore, you cannot determine the success of a connection attempt by checking the return value of the begin method.
To determine if the ESP32 has successfully connected to a Wi-Fi network, you should use the status() method of ESP32 WiFi Library to poll the current connection status. For example, you can loop calling WiFi.status() until it returns WL_CONNECTED, which indicates that the ESP32 has successfully connected to the Wi-Fi network.
Please refer to the example code below for more detailed information on how to verify if the ESP32 has successfully connected to a Wi-Fi network.
Example Codes
Below are examples demonstrating both usages of the begin
method:
Example 1: With Arguments
This code demonstrates how to use the WiFi.begin()
method from the ESP32 WiFi Library to connect to a WiFi network. It connects to a predefined network using the SSID and password, waits until the connection is established, and prints a confirmation message to the serial monitor.
To use this code, replace the ssid
and password
variables with your WiFi credentials. Upload the code to your ESP32 board, open the serial monitor, and observe the connection process. Ensure the baud rate in the serial monitor matches the value set in Serial.begin()
.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This code demonstrates how to connect to a
* WiFi network using the ESP32's WiFi library.
* It includes connecting to a WiFi network with predefined
* SSID and password. The program waits until the connection
* is successfully established before proceeding.
* Once connected, it prints a confirmation message to
* the serial monitor. This example also shows how to
* declare and use external variables for storing WiFi
* credentials.
*
* License: MIT
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their nnovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
// Attempt to connect to Wi-Fi
WiFi.begin(ssid, password);
// Wait until the connection is successful
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
// Your code here
}
Example 2: Without Argument
This code demonstrates how to use the WiFi.begin()
method from the ESP32 WiFi Library to initialize the Wi-Fi module without specifying network credentials. It shows the process of starting the Wi-Fi configuration, but does not connect to any specific network.
To use this code, upload it to your ESP32 board and open the serial monitor. The code will initialize the Wi-Fi module and display status messages. Note that no network connection will be established since SSID and password are not provided. Ensure the baud rate in the serial monitor matches the value set in Serial.begin()
.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This code demonstrates how to Initialize
* the ESP32 Wi-Fi connection without specifying
* network credentials.
*
* License: MIT
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their nnovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin(); // Initialize Wi-Fi without parameters
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Configuring Wi-Fi...");
}
Serial.println("Wi-Fi initialized, but not connected to any network.");
}
void loop() {
// Your code here
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!
Related WiFi Library Methods