ESP32 WiFi Library – WiFi.softAPConfig

Home / References / ESP32 Library / WiFi API / AP

Description

The softAPConfig method is used to configure the softAP network settings for an ESP32 when it is functioning as a Wi-Fi access point. This method allows you to set the IP address, gateway, and subnet mask for the softAP interface.


Syntax and Usage

Here’s how to use the softAPConfig method:WiFi.softAPConfig(IPAddress local_ip, IPAddress gateway_ip, IPAddress subnet_mask);

The method initializes the softAP interface’s IP configuration using the provided parameters:

  1. local_ip – IP address assigned to the softAP
  2. gateway_ip – Gateway IP address for the softAP network
  3. subnet_mask – Subnet mask defining the network’s range

All three parameters are mandatory and must be valid IP configurations. The method should be called before initializing the softAP with WiFi.softAP().


Argument(s)

  • local_ip – The IP address assigned to the softAP interface. This is the address clients connect to when they join this network.
  • gateway_ip – The gateway IP address for the softAP network. This is important for routing traffic between the softAP and other networks or devices.
  • subnet_mask – The subnet mask defining the network’s range. It determines how IP addresses are distributed in the local network.

For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.


Return Value

The method returns a boolean value indicating success or failure:

  • true – The configuration was successful.
  • false – The configuration failed. This can be due to invalid IP parameters, conflicting configurations, or hardware issues.

Example Codes

This code demonstrates how to configure and start an ESP32 Access Point (AP) with a custom static IP configuration using the softAPConfig() and softAP() methods. It sets up a predefined IP address, subnet mask, and gateway for the AP, ensuring consistent network settings for local communication. The example also includes functionality to display these customized network details, such as the AP’s IP address, subnet mask, and gateway, in the Serial Monitor. This is particularly useful for projects requiring fixed IP configurations, such as IoT devices or local servers, where predictable network parameters are essential.

To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. The ESP32 will create an Access Point with the specified SSID (“AvantMaker-ESP32-AP”) and password (“12345678”). Once the AP starts, the Serial Monitor will display the configured IP address (192.168.4.1), subnet mask (255.255.255.0), and gateway IP (192.168.4.1). You can modify the SSID, password, or IP settings to suit your project needs. Ensure no conflicting networks exist in the same range to avoid connectivity issues.

/*
 * Author: Avant Maker
 * Date: February 7, 2025
 * Version: 1.0
 * Description: This example code demostrate how to use
 * WiFi.softAPConfig method to configures the ESP32 with
 * a static IP address, gateway, and subnet mask. 
 * The softAP is then started with the specified SSID and password. 
 * Clients can connect to the network using these 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/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>

const char* ssid = "AvantMaker-ESP32-AP";
const char* password = "12345678";

void setup() {
  Serial.begin(115200);

  // Configure static IP for the Access Point
  IPAddress local_ip(192, 168, 4, 1);  // Custom IP for the AP
  IPAddress gateway(192, 168, 4, 1);   // Gateway IP (same as local_ip for AP)
  IPAddress subnet(255, 255, 255, 0);  // Subnet mask

  // Apply the static IP configuration
  if (WiFi.softAPConfig(local_ip, gateway, subnet)) {
    Serial.println("softAP configured with static IP");
  } else {
    Serial.println("Failed to configure softAP");
  }

  // Start the Access Point with the specified SSID and password
  if (WiFi.softAP(ssid, password)) {
    Serial.println("softAP started");

    // Display the AP's IP details
    Serial.print("AP IP Address: ");
    Serial.println(WiFi.softAPIP());

    Serial.print("Subnet Mask: ");
    Serial.println(subnet);

    Serial.print("Gateway IP: ");
    Serial.println(gateway);
  } else {
    Serial.println("Failed to start softAP");
  }
}

void loop() {
  // No loop handling needed for this example
}

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 !!