Then, call the WiFi.hostname() function before calling WiFi.begin().
WiFi.hostname(newHostname.c_str());
代码

/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-set-custom-hostname-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>
// Replace with your network credentials (STATION)
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
String newHostname = "ESP8266Node";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//Get Current Hostname
Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
//Set new hostname
WiFi.hostname(newHostname.c_str());
//Get Current Hostname
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
//Init Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}
void loop() {
// put your main code here, to run repeatedly:
}
浙公网安备 33010602011771号