Skip to content
Boris Lovosevic edited this page Jan 27, 2018 · 10 revisions

mDNS Module

Multicast DNS (mDNS) is the protocol that creates a device-uniqueidentifier to register as a hostname via a multicast service on local networks.

Usage:

import network

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("WiFi_SSID", "WiFi_password")
tmo = 50
while not sta_if.isconnected():
    utime.sleep_ms(100)
    tmo -= 1
    if tmo == 0:
        break

if sta_if.isconnected():
    mdns = network.mDNS()
    _ = mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"})
    _ = mdns.addService('_telnet', '_tcp', 23, "MicroPython", {"board": "ESP32", "service": "mPy Telnet REPL"})
    _ = mdns.addService('_http', '_tcp', 80, "MicroPython", {"board": "ESP32", "service": "mPy Web server"))

Create mDNS instance

mdns = network.mDNS()


Methods


mdns.start(name, instance)

name string, server host name
instance string, mDNS instance description

Returns True on success, False on fail.

After the mdns is started, the MicroPython host will be visible in local network as name.local


mdns.stop()

stop the mdns server, free the resources

Returns True on success, False on fail.


mdns.addService(service, protocol, port, instance, txdata)

Add service to mdns server.

service string, service type, use names like '_http', '_ftp', _telnet', '_mytcp'
protocol string, protocol type, _tcp or _udp
port integer, the port on which the service runs
instance string, service instance name
txdata optional, distionary (max 8 items), describe the service characteristics

Returns True on success, False on fail.


mdns.removeService(service, protocol, port, instance, txdata)

Remove the previously added service from mdns server.

service string, service type
protocol string, protocol type

Returns True on success, False on fail.


mdns.queryHost(hostname [,timeout])

Query the IP address of the LAN host hostname.

hostname string, host name to query.
timeout optional; default=2000; timeout for host query in ms.

Returns IPv4 addresse as strings.

>>> mdns.queryHost('ubuntumate')
'192.168.0.63'

mdns.queryService(servise, protocol [,timeout])

Query the service info from hosts on LAN.

service string, service type
protocol string, protocol type
timeout optional; default=2000; timeout for host query in ms.

Returns list of items containing services information.
Each service information item is a tuple with the following items:

  1. interface_type 'STA', 'AP' or 'ETH'
  2. protocol_type 'V4' or 'V6'
  3. instance_name string or None
  4. host_name string or None
  5. port integer or None
  6. ip_addr list of IP addresses or None
  7. tx_record dictionary of TX record items or None
>>> mdns.queryService('_smb', '_tcp')
[('STA', 'V4', 'UBUNTUMATE', 'UbuntuMate.local', 445, ['192.168.0.63', '254.128.0.0'], None)]
>>> 
Clone this wiki locally