Home / References / ESP32 Library / WiFi API / AP
Description
The softAPgetHostname
method is used to retrieve the hostname of the ESP32 when it is configured as a Soft Access Point (Soft-AP). This method allows you to get the hostname that is used to identify the ESP32 on the network.
Syntax and Usage
The softAPgetHostname
method is used as follows:
WiFi.softAPgetHostname();
This method does not require any arguments and returns the hostname of the Soft-AP.
Argument(s)
This method does not require any arguments.
Return Value
The softAPgetHostname
method returns a const char*
value representing the hostname of the ESP32 Soft-AP.
Example Codes
Setting and Getting Hostname
This code demonstrates the use of the ESP32 AP Class’s softAPgetHostname()
method to retrieve the hostname of a Soft Access Point (SoftAP).
To use this code, upload it to your ESP32 board. It sets up a SoftAP with the SSID “AvantMaker-ESP32-AP” and password “12345678”, then attempts to set the hostname to “AvantMakerESP32Host”. After setting, it retrieves and prints the hostname using softAPgetHostname()
.
/*
* Author: Avant Maker
* Date: February 14, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use softAPgetHostname to retrieve the 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!