0

Im trying to assign a static IP to my ESP32 and also set the host name. I am able to get one or the other to work, but not get them to work together.

Tried moving around WiFi.hostname & WiFi.config in the code, tried WiFi.begin and then setting hostname & IP

#include <WiFi.h>
void setup() {

IPAddress local_IP(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(208, 67, 222, 222);
IPAddress secondaryDNS(208, 67, 220, 220);

WiFi.mode(WIFI_STA);
WiFi.hostname("ESP");
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
WiFi.begin("xxxx", "yyyy");

while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}

question in a question: The WiFi.config that that gives me a static IP is in the format local_IP, gateway, subnet, primaryDNS, secondaryDNS but the documentation for WiFi.config 2 (and also looking at the library WiFi.h code) is ip, dns, gateway, subnet.

and according to the documentation this should also work, but doesn't WiFi.config (ip)

1
  • 3
    How are you determining that the "hostname" is working or not? The concept of a "hostname" only exists in the realm of DHCP (which you aren't using), DNS (which isn't controlled by the ESP32 and you're not using anyway) or mDNS (which again you're not using).
    – Majenko
    Aug 30, 2021 at 15:03

1 Answer 1

4

There is no such thing as a hostname associated with a specific device.

  • When you request an IP from your router using DHCP the ESP32 has the option (which it uses) to tell the router what it'd like to be called. The router may, if it chooses, decide to inject that name into DNS lookups done through the router to resolve that allocated IP address.
  • You can manually "advertise" a name through the mDNS protocol which requires the inclusion and use of another library and some more configuration in your sketch, along with mDNS support in the devices and applications doing the name lookup (it is getting more universally implemented but is by no means certain to be available).

Since you're not using either DHCP to obtain your IP, nor using mDNS to advertise your name, there is no mechanism that exists to magically make your device have a "name" on your network.

The simplest method to "get around" the issue is to just use DHCP and let your router do all the work. If you really want a static IP address then configure your router to provide a static IP address to your device.

6
  • Well that certainly would explain my issue. But what doesn't addup is that other devices on my network (printers, NAS, etc) have static IPs and they show custom host names
    – user80062
    Aug 30, 2021 at 19:06
  • 1
    Thats because these devices use DHCP to tell the router their name. Just as Majenko described. For the static IPs: you may have set the devices to static IPs in the router setting. In that case they still get their IP from the routers DHCP, just the same every time. Or they don't use DHCP and you have set the IP on the device while hopefully making sure, that no other device can have the same IP
    – chrisl
    Aug 30, 2021 at 19:44
  • More likely they are advertising their names with mDNS. myPrinter.local for example.
    – Majenko
    Aug 30, 2021 at 19:52
  • None of the devices I am talking about are using DHCP. They all have static IPs and custom hostnames in the device itself. The printers have mDNS, the NAS doesnt but I am still seeing it's custom name. Im going to try to add mDNS to the ESP32 tomorrow and see if that helps
    – user80062
    Aug 31, 2021 at 3:14
  • The NAS is probably using SMB which includes network name resolution of its own.
    – Majenko
    Aug 31, 2021 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.