Skip to content

ESP8266 – Changing Wifi Hostname

  • by

My recent fascination with the Wemos D1 Mini (ESP8266) devices has been very enlightening. These devices are very capable and there is so much reusable code out there for them that programming them is made very easy.

One thing that I’ve recently been trying to figure out is how to change the default hostname when they join a WIFI network, by default they join as ESP_xxxxxx where the x’s are the last 6 characters of the MAC address of the device.  This isn’t very useful when you have 3 or 4 of these.  I figured there must be a way of changing this and there is.

It took quite a bit of digging and picking bits of code from many discussions on many forums.  So, I’ll pull it all together here in hopes you’ll find this page and find it useful.

So, how is it done?  Pretty simple, the following bits of code need to be added to your code and I’ll include some functioning code that I currently use.  See the comments in the code for where these should go, and look at my full script.

// This should be added to the top where your other includes are
extern "C" {
#include "user_interface.h"
}
// This gets added into your void.setup, and before WiFi.begin
wifi_station_set_hostname("WhateverHostnameYouLike");
// If you're doing some debug output to serial, this should go in that section
Serial.print("Host Name: ");
Serial.println(WiFi.hostname());

This is my full code for my Wemos D1 mini + DHT22 + Thingspeak

#include <DHT.h>
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
 
// Introducir a continuación tus datos de ThingSpeak y red WiFi
String apiKey = "MyThingSpeakAPIkey";
const char* ssid = "MyWifiAPname";
const char* password = "ShhhhSecret";
const char* server = "api.thingspeak.com";

// Time to sleep (in seconds):
//const int sleepTimeS = 60;

#define DHTPIN D4
#define DHTTYPE DHT22
 
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
 
void setup() 
{
Serial.begin(115200);
delay(10);
dht.begin();

wifi_station_set_hostname("MyDeviceHostName");
WiFi.begin(ssid, password);
 
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("Host Name: ");
Serial.println(WiFi.hostname());
 
//WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) 
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
 
}
 
void loop() 
{
delay(2000); 
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) 
{
Serial.println("DHT sensor reading failure !!!");
return;
}
 
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
 
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
 
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("*C Humidity: ");
Serial.print(h);
Serial.print("%");
Serial.println("---->>>> Sent data to ThingSpeak");
}
client.stop();

//Sleep
//   Serial.println("ESP8266 in sleep mode");
//   ESP.deepSleep(sleepTimeS * 1000000,WAKE_RF_DEFAULT);

Serial.println("Waiting 60 seconds... ");
delay(60000);
}

Please feel free to use the above code and share.  This code was taken from a number of sources and combined to do what I needed it for.

2 thoughts on “ESP8266 – Changing Wifi Hostname”

    1. It is possible, but that requires more steps as you’ll have to boot it, find the MAC, set your DHCP config for it, etc, etc. Doing it in code means it will set the name you like for that device regardless of the MAC (ie, it’s an attic temp/humidity but the Wemos dies and you replace it), you won’t have to change anything except to reload the code on the new device and plug it in.

Comments are closed.