ESP32 WiFi Library – WiFi.softAPgetStationNum

Home / References / ESP32 Library / WiFi API / AP

Description

The softAPgetStationNum method is used to retrieve the number of client devices currently connected to the ESP32’s Soft Access Point (SoftAP). This method is part of the ESP32 WiFi Library and is useful for monitoring the number of connected clients in SoftAP mode.


Syntax and Usage

The softAPgetStationNum method does not require any arguments and returns the number of connected clients.

WiFi.softAPgetStationNum()

WiFi.softAPgetStationNum()

This usage retrieves the number of client devices currently connected to the SoftAP.


Argument(s)

This method does not require any arguments.


Return Value

The softAPgetStationNum method returns an uint8_t value representing the number of client devices connected to the SoftAP.


Example Codes

Displaying the Number of Connected Clients

This code demonstrates how to use the ESP32 AP Class’s softAPgetStationNum() method to retrieve the number of devices connected to the ESP32’s Soft Access Point (AP). The ESP32 is set up as a WiFi access point with the SSID “AvantMaker-ESP32-AP” and password “12345678”. The softAPgetStationNum() method counts the connected clients and displays the result periodically in the Serial Monitor.

To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. Connect devices to the ESP32’s WiFi network using the provided SSID and password. The Serial Monitor will display the number of connected clients, updating every 2 seconds. This example helps monitor client connections to the ESP32’s AP in real time.

/*
 * Author: Avant Maker
 * Date: February 14, 2025
 * Version: 1.0
 * Description: This example demonstrates how to start a SoftAP 
 * and periodically display the number of connected clients using 
 * the softAPgetStationNum method.
 *
 * 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/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 the SoftAP
  if (!WiFi.softAP("AvantMaker-ESP32-AP", "12345678")) {
    Serial.println("Failed to create SoftAP");
    return;
  }

  Serial.println("SoftAP started. Waiting for clients...");
}

void loop() {
  // Get the number of connected clients
  uint8_t numClients = WiFi.softAPgetStationNum();
  Serial.print("Number of connected clients: ");
  Serial.println(numClients);

  delay(2000); // Update every 2 seconds
}

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 Essential Guide – Network Section – WiFi Chapter

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

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!

error: Content is protected !!