1# mDNS example 2 3Shows how to use mDNS to advertise lookup services and hosts 4 5## Example workflow 6 7- mDNS is initialized with host name and instance name defined through the project configuration and `_http._tcp` service is added to be advertised 8- A delegated host `esp32-delegated._local` is added and another `_http._tcp` service is added for this host. 9- WiFi STA is started and trying to connect to the access point defined through the project configuration 10- The system event handler is used to pass the network events to mDNS so the service is aware when the interface comes up or down 11- GPIO0 (BOOT Button) is initialized as pulled-up input that can be monitored for button press 12- Example task is started to check if the button is pressed so it can execute the mDNS queries defined 13 14### Configure the project 15 16* Open the project configuration menu (`idf.py menuconfig`) 17 18* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../README.md) for more details. 19* When using Make build system, set `Default serial port` under `Serial flasher config`. 20* Set `mDNS Hostname` as host name prefix for the device and its instance name in `mDNS Instance Name` 21* Disable `Resolve test services` to prevent the example from querying defined names/services on startup (cause warnings in example logs, as illustrated below) 22 23### Build and Flash 24 25Build the project and flash it to the board, then run monitor tool to view serial output: 26 27``` 28idf.py -p PORT flash monitor 29``` 30 31- Wait for WiFi to connect to your access point 32- You can now ping the device at `[board-hostname].local`, where `[board-hostname]` is preconfigured hostname, `esp32-mdns` by default. 33- You can also browse for `_http._tcp` on the same network to find the advertised service 34- Pressing the BOOT button will start querying the local network for the predefined in `check_button` hosts and services 35- Note that for purpose of CI tests, configuration options of `MDNS_RESOLVE_TEST_SERVICES` and `MDNS_ADD_MAC_TO_HOSTNAME` are available, but disabled by default. If enabled, then the hostname suffix of last 3 bytes from device MAC address is added, e.g. `esp32-mdns-80FFFF`, and a query for test service is issued. 36 37 38(To exit the serial monitor, type ``Ctrl-]``.) 39 40See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. 41 42## Example Output 43``` 44I (0) cpu_start: Starting scheduler on APP CPU. 45I (276) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE 46I (276) mdns-test: mdns hostname set to: [esp32-mdns] 47I (286) wifi: wifi driver task: 3ffc2fa4, prio:23, stack:3584, core=0 48I (286) wifi: wifi firmware version: a3be639 49I (286) wifi: config NVS flash: enabled 50I (296) wifi: config nano formating: disabled 51I (296) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE 52I (306) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE 53I (336) wifi: Init dynamic tx buffer num: 32 54I (336) wifi: Init data frame dynamic rx buffer num: 32 55I (336) wifi: Init management frame dynamic rx buffer num: 32 56I (346) wifi: Init static rx buffer size: 1600 57I (346) wifi: Init static rx buffer num: 10 58I (346) wifi: Init dynamic rx buffer num: 32 59I (356) mdns-test: Setting WiFi configuration SSID myssid... 60I (426) phy: phy_version: 4000, b6198fa, Sep 3 2018, 15:11:06, 0, 0 61I (426) wifi: mode : sta (30:ae:a4:80:FF:FF) 62I (426) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 63I (1756) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1 64I (2736) wifi: state: init -> auth (b0) 65I (2756) wifi: state: auth -> assoc (0) 66I (2766) wifi: state: assoc -> run (10) 67I (2786) wifi: connected with myssid, channel 11 68I (2786) wifi: pm start, type: 1 69 70I (4786) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 71I (21126) mdns-test: Query A: esp32.local 72W (23176) mdns-test: ESP_ERR_NOT_FOUND: Host was not found! 73I (23176) mdns-test: Query PTR: _arduino._tcp.local 74W (26276) mdns-test: No results found! 75I (26276) mdns-test: Query PTR: _http._tcp.local 761: Interface: STA, Type: V6 77 PTR : HP Color LaserJet MFP M277dw (7C2E10) 78 SRV : NPI7C2E10.local:80 79 A : 254.128.0.0 802: Interface: STA, Type: V4 81 PTR : switch4e4919 82 SRV : switch4e4919.local:80 83 TXT : [1] path=/config/authentication_page.htm; 84 A : 192.168.0.118 85I (29396) mdns-test: Query PTR: _printer._tcp.local 861: Interface: STA, Type: V6 87 PTR : HP Color LaserJet MFP M277dw (7C2E10) 88 SRV : NPI7C2E10.local:515 89 A : 254.128.0.0 902: Interface: STA, Type: V4 91 PTR : HP Color LaserJet MFP M277dw (7C2E10) 92``` 93 94See the README.md file in the upper level 'examples' directory for more information about examples. 95