Home / References / ESP32 Library / WiFi API / Station
Description
The WiFi.getHostname()
method retrieves the current hostname of the ESP32 device, which is a string identifier used by the system and networks to recognize the device.
The hostname is set when the device connects to a Wi-Fi network or when explicitly assigned using WiFi.setHostname()
.
Syntax and Usage
The method is part of the WiFi
class and is called without arguments. Here’s the basic syntax:
WiFi.getHostname()
This retrieves the current hostname and stores it in a String variable.
Argument(s)
- There are no arguments for this method.
Return Value
The method returns a String
object containing the current hostname of the ESP32 device. If no hostname has been set, it may return the default hostname assigned by the system.
Example Codes
Retrieving Hostname After Connecting to Wi-Fi
This code demonstrates how to use the ESP32 WiFi Library’s setHostname()
and getHostname()
methods to set and retrieve a custom hostname for the ESP32 device. A hostname is a human-readable label for the device on the network, which can make it easier to identify compared to an IP address. This is especially useful in environments with multiple connected devices.
To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi network’s credentials. Upload the code to your ESP32 and open the Serial Monitor at 115200 baud. The code sets a custom hostname (“my_esp32_device”), connects to the WiFi network, and then retrieves and prints the hostname using getHostname()
. Ensure the 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 example sets a specific hostname
* for the ESP32 using setHostname() and then retrieves and prints
* it using getHostname().
*
* 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);
// Set custom hostname
if (WiFi.setHostname("my_esp32_device")) {
Serial.println("Hostname set successfully");
} else {
Serial.println("Failed to set hostname");
}
// Connect to Wi-Fi
WiFi.begin("your_SSID", "your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Retrieve and print hostname
Serial.print("Hostname: ");
Serial.println(WiFi.getHostname());
}
void loop() {
}
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!