Home / References / ESP32 Library / WiFi API / Station
Description
The SSID
method in the ESP32 WiFi library is used to retrieve the Service Set Identifier (SSID) of the connected WiFi network. The SSID is the name of the WiFi network that the ESP32 is either currently connected to or attempting to connect to.
Syntax and Usage
The SSID
method can be used in the following ways:
– Without Arguments:
WiFi.SSID();
Call the method without arguments to retrieve the SSID of the currently connected network.
– With Arguments:
WiFi.SSID(index);
Call the method with an index argument to retrieve the SSID of a scanned network from the list of available networks.
Argument(s)
The SSID
method accepts the following argument:
- index: (Optional, integer) The index of the network in the list of scanned networks. The index must be between
0
and the total number of networks found byWiFi.scanNetworks()
. If omitted, the method returns the SSID of the currently connected network.
Return Value
The SSID
method returns a String that represents the SSID:
- If no argument is provided, it returns the SSID of the currently connected WiFi network.
- If an
index
argument is provided, it returns the SSID of the corresponding network in the list of scanned networks. - If no network is found or the index is invalid, it returns an empty string.
Example Codes
Below are examples demonstrating how to use the SSID
method:
Example 1: Get SSID of the Connected Network
This code demonstrates how to use the ESP32 WiFi Library’s SSID()
method to retrieve and display the name (SSID) of the currently connected WiFi network. After establishing a connection to a specified WiFi network, the code uses the SSID()
method to fetch the network’s name and prints it to the Serial Monitor for verification.
To use this code, replace “YourSSID” and “YourPassword” with your WiFi network’s SSID and password. Upload the code to your ESP32 board and open the Serial Monitor at 115200 baud. The code will connect to the WiFi network and display the SSID of the connected network. Ensure your ESP32 is within range of the WiFi signal for a successful connection.
/*
* Author: Avant Maker
* Date (MM-DD-YY): 02-06-25
* Version: 1.0
* Description: This code connects to a WiFi network
* and prints the SSID of the connected network.
*
* Code Source: ESP32 Arduino Library Example Code
*
* For more detailed explanation about
* this code please refer to 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/references/esp32-arduino-core-index/
*
* 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
* innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Connect to a WiFi network
WiFi.begin("YourSSID", "YourPassword");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Print the SSID of the connected network
Serial.print("The SSID of the connected network: ");
Serial.println(WiFi.SSID());
}
void loop() {
// Your code here
}
Example 2: Get SSID of a Scanned Network
This code demonstrates how to use the ESP32 WiFi Library’s SSID()
method to scan for nearby WiFi networks and retrieve their names (SSIDs). The WiFi.scanNetworks()
function detects available networks, and the SSID()
method fetches the SSID of each network found during the scan. The results are printed to the Serial Monitor for review.
To use this code, upload it to your ESP32 board and open the Serial Monitor at 115200 baud. The code will automatically scan for nearby WiFi networks and display their SSIDs along with their index numbers. Ensure your ESP32 is in an area with active WiFi signals for accurate results. This example is useful for identifying available networks in the device’s vicinity.
/*
* Author: Avant Maker
* Date (MM-DD-YY): 02-06-25
* Version: 1.0
* Description: This code scans for available WiFi
* networks and prints their SSIDs.
*
* Code Source: ESP32 Arduino Library Example Code
*
* For more detailed explanation about
* this code please refer to 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/references/esp32-arduino-core-index/
*
* 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
* innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Start WiFi scan
Serial.println("Scanning for WiFi networks...");
int numNetworks = WiFi.scanNetworks();
// Print the SSID of each network
for (int i = 0; i < numNetworks; i++) {
Serial.print("Network ");
Serial.print(i);
Serial.print(": ");
Serial.println(WiFi.SSID(i));
}
}
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!