Skip to content
Boris Lovosevic edited this page May 18, 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():
    try:
        mdns = network.mDNS()
        mdns.start("mPy","MicroPython with 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"})
    except:
        print("mDNS not started")
boris@UbuntuMate:~$ avahi-browse -avtr
Server version: avahi 0.6.32; Host name: UbuntuMate.local
E Ifce Prot Name                                          Type                 Domain
....
+   eth0 IPv4 MicroPython                                   Web Site             local
+   eth0 IPv4 MicroPython                                   Telnet Remote Terminal local
+   eth0 IPv4 MicroPython                                   FTP File Transfer    local
=   eth0 IPv4 MicroPython                                   Web Site             local
   hostname = [mPy.local]
   address = [192.168.0.16]
   port = [80]
   txt = ["service=mPy Web server" "board=ESP32"]
=   eth0 IPv4 MicroPython                                   Telnet Remote Terminal local
   hostname = [mPy.local]
   address = [192.168.0.16]
   port = [23]
   txt = ["service=mPy Telnet REPL" "board=ESP32"]
=   eth0 IPv4 MicroPython                                   FTP File Transfer    local
   hostname = [mPy.local]
   address = [192.168.0.16]
   port = [21]
   txt = ["board=ESP32" "passive=True" "service=mPy FTP File transfer"]
: Cache exhausted
: All for now
boris@UbuntuMate:~$ 

Create mDNS instance

mdns = network.mDNS()


Methods


mdns.start(name, instance)

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

Raises an exception if not succesfully started.

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


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, dictionary (max 8 items), describe the service characteristics

Returns True on success, False on fail.
Raises an exception if mDNS server not started or wrong parameters are given.

>>> mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"})
True

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.
Raises an exception if mDNS server not started or wrong parameters are given.


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(service, protocol [,timeout] [,maxres])

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.
maxres optional; default=8; maximal number of results to return.

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)]
>>>