Accessing local MQTT broker via hostname

Hi All,

I am using the MQTT library on an Argon for local communication between several devices(Raspberry Pi, Particle Argon, and M5 core2 - which is an ESP32).

I am interested in identifying my MQTT broker by its local hostname instead of IP address, as I currently do. This will help me when I move the system from one location to another, as I shouldn't have to code the new IP Address of the MQTT broker and reflash the firmware onsite.

Can anyone assist me with getting the syntax right to access by hostname? I use the hostname on other devices or on an MQTT explorer without issue, so I think it's probably doable and that I'm just not doing it right.

I can get the MQTT broker to connect if I use the IP Address:

byte server = { 10,0,0,200 };
MQTT client(server, 1883, callback);

or if I use a remote broker:

MQTT client("mqtt.eclipse.org", 1883, callback);

But I cannot get it to connect to using my local hostname:

MQTT client("raspberrypimqtt.local", 1883, callback);

If you want to resolve .local domains what you need is MDNS, which is different than the regular DNS used by MQTT, and actually by TCPClient which is used in the MQTT library.

What you need to do is:

  • Resolve the .local domain using a MDNS library, probably only once per reset
  • Pass the IP address to MQTT instead of the hostname

I haven’t used this library in a long time but the MDNS library used to work. I don’t see an obvious reason why it shouldn’t still work.

Thanks for that, I’ll get to work and see if I can’t get it to go!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.