mDNS is cool as hell

mDNS is cool as hell

TLDR:  Setup mDNS on a wifi-connected device to establish a human-friendly hostname on your local network for convenience.

IP addresses are great but they've never really had the warm and fuzzy feeling conveyed by an actual hostname when you connect to something. Especially in a browser ( for some reason ) using a hostname is an expected layer of window dressing for the user experience. I know that when I finally get my IoT powered dog-feeder / garden-waterer / rat-killer / security-drone up and running, my wife is going to want an acutal hostname to connect to if I want her to use it.

Even when the user is yourself this is a nice convenience that solves the issue of having to remember/cache/nmap-your-whole-network-to-lookup an IP address for your Raspberry Pi, again.  IPv6 will only make it harder to keep track of ( and actually use/type ) IP addresses.

What the mDNS?

Compatible with, but not part of the main DNS spec, mDNS stands for 'multicast DNS'. When requesting a .local hostname, the client sends a request out across the local network for the client by the given hostname to identify itself, which it does by returning a response containing it's IP address.  Get the full, more accurate description.

How to Use

mDNS can be setup and used in a wide variety of ( local ) network situations.  The setup in each is going to be slightly different depending on the underlying OS of the client 'advertising' the hostname as well as the client 'discovering' it.
Advertising is when a connected device establishes that it wants to be identified by a particular hostname. On a Linux system this is actually as easy as modifying /etc/hosts and /etc/hostname, while on embedded devices I've been using a simple library. Examples of both are below.

Examples

Here is how to get your ish setup at http://whatever.local

RaspberryPi

This is actually so easy that I had already done it without realizing it. Probably a result of my original running of raspi-config.

The addition to /etc/hosts shown in bold

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 pi3

The file /etc/hostname should contain only the hostname

pi3

ESP-whatever

Install an mDNS library, like the convincingly named ESP8266mDNS .

#include <ESP8266mDNS.h>

then in your setup()

MDNS.begin("esp8266")     // returns false if fails

Then this device will be available at http://esp8266.local

Discovery

The client intending to connect to the other device by it's .local hostname will need to be able to discover mDNS clients.  This is where you'll hear people talk about Bonjour, Avahi, and the other various discovery services.  Most likely if using a laptop, this will 'just work' due to the discovery service already being included in a modern web browser.   Not much more to be said here except to move along to when it doesn't work...

Troubleshooting

Google Chrome needs to have an explicit 'http://' prefix typed the first time that a mDNS hostname is used.  It seems that after the first time it will understand what you mean and avoid trying to search the Internet for you.

windows ipconfig \flushdns trick etc, confirm with ping

Android has very poor support for mDNS.  It is an actively discussed issue.  IMHO, this is biggest pitfall in the coolness of mDNS.  I often use my phone or tablet to connect to various connected devices either using ssh or http and it would be ubiquitously convenient if it just worked everywhere.  I expect a lot from technology.

Don't Get Fancy

If you find yourself trying to setup subdomains or suddenly needing to support HTTPS, then those are signs that your use case is probably outgrowing mDNS.  Remember this is really just for convenience within your local network.