Home / References / ESP32 Library / WiFi API / AP
Description
The softAPsetHostname
method is used to set the hostname for the ESP32 when it is operating in Soft Access Point (Soft-AP) mode. This hostname can be used to identify the ESP32 on the network.
Syntax and Usage
The softAPsetHostname
method is used to configure the hostname for the Soft-AP. The method requires a single argument: the hostname as a string.
WiFi.softAPsetHostname(const char *hostname);
Argument(s)
- hostname: A string representing the hostname to be set for the Soft-AP. The hostname must be no longer than 32 characters.
Return Value
The method returns true
if the hostname is successfully set, and false
otherwise.
Example Codes
Setting a Custom Hostname for Soft-AP
This code demonstrates how to use the ESP32 AP (Access Point) class’s softAPsetHostname()
and softAPgetHostname()
methods to set and retrieve a custom hostname for a soft AP. The ESP32 creates a password-protected Access Point with the SSID “AvantMaker-ESP32-AP” and password “12345678”. It then attempts to assign a custom hostname (“AvantMakerESP32Host”) to the soft AP using softAPsetHostname()
. If successful, the hostname is displayed in the Serial Monitor using softAPgetHostname()
. This feature is useful for identifying the ESP32 on a network, especially in environments with multiple devices.
To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. The ESP32 will start the soft AP and attempt to set the hostname. The Serial Monitor will display whether the hostname was set successfully and show the current hostname of the soft AP. Modify the SSID, password, or hostname as needed for your project. Ensure that the hostname is unique within your network to avoid conflicts with other devices.
/*
* Author: Avant Maker
* Date: February 14, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use softAPsetHostname to set a custom hostname for
* the ESP32 when it is operating in Soft-AP mode.
*
* 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);
WiFi.softAP("AvantMaker-ESP32-AP", "12345678");
if (WiFi.softAPsetHostname("AvantMakerESP32Host")) {
Serial.println("SoftAP hostname set successfully.");
} else {
Serial.println("Failed to set SoftAP hostname.");
}
// Get the hostname of the Soft-AP
const char* hostname = WiFi.softAPgetHostname();
Serial.print("Soft-AP Hostname: ");
Serial.println(hostname);
}
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!