### Integrate mDNS with WiFi Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt This example demonstrates using the library with WiFi shields by substituting EthernetUDP with WiFiUDP. ```cpp #include #include #include #include char ssid[] = "yourNetwork"; char pass[] = "yourPassword"; int status = WL_IDLE_STATUS; WiFiUDP udp; MDNS mdns(udp); WiFiServer server(80); void setup() { Serial.begin(9600); // Connect to WiFi while (status != WL_CONNECTED) { Serial.print("Connecting to "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } Serial.println("Connected to WiFi"); server.begin(); // Initialize mDNS with WiFi IP mdns.begin(WiFi.localIP(), "arduino-wifi"); // Register service (same as Ethernet) mdns.addServiceRecord("Arduino WiFi Server._http", 80, MDNSServiceTCP); } void loop() { mdns.run(); WiFiClient client = server.available(); if (client) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("Hello from WiFi Arduino!"); delay(1); client.stop(); } } ``` -------------------------------- ### Discover Services with setServiceFoundCallback Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Use this pattern to discover network services by defining a callback function to handle results and calling startDiscoveringService in the setup. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Callback for discovered services void serviceFound(const char* type, MDNSServiceProtocol proto, const char* name, IPAddress ip, unsigned short port, const char* txtContent) { if (name == NULL) { // Discovery timeout reached Serial.print("Finished discovering "); Serial.println(type); return; } Serial.print("Found service: '"); Serial.print(name); Serial.print("' at "); Serial.print(ip); Serial.print(":"); Serial.print(port); Serial.println(proto == MDNSServiceTCP ? " (TCP)" : " (UDP)"); // Parse TXT record if present if (txtContent) { Serial.print(" TXT: "); char len = *txtContent++; while (len) { while (len--) Serial.print(*txtContent++); len = *txtContent++; if (len) Serial.print(", "); } Serial.println(); } } void setup() { Serial.begin(9600); Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Set callback for service discovery mdns.setServiceFoundCallback(serviceFound); // Discover HTTP services for 5 seconds // Common types: _http, _ssh, _ftp, _printer, _afpovertcp mdns.startDiscoveringService("_http", MDNSServiceTCP, 5000); } void loop() { mdns.run(); // Check if still discovering if (mdns.isDiscoveringService()) { // Discovery in progress } // Stop ongoing discovery if needed // mdns.stopDiscoveringService(); } ``` -------------------------------- ### begin() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Initializes the mDNS responder with the device's IP address and optional hostname. ```APIDOC ## begin() ### Description Initializes the mDNS responder with the device's IP address and optional hostname. After calling begin(), the Arduino becomes reachable via the specified hostname with a .local suffix. ### Parameters - **ip** (IPAddress) - Required - The IP address of the device. - **hostname** (String) - Optional - The desired hostname (without .local suffix). ``` -------------------------------- ### Initialize MDNS Responder with begin() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Initializes the mDNS responder with the device's IP address and an optional hostname. The device becomes reachable via a `.local` suffix. This method must be called before other mDNS operations. The run() method must be called periodically in the loop. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); // Initialize mDNS with IP and hostname // Device will be reachable at "arduino.local" mdns.begin(Ethernet.localIP(), "arduino"); // Alternative: use default name "arduino" // mdns.begin(Ethernet.localIP()); } void loop() { // Required: call run() periodically for mDNS to function mdns.run(); } ``` -------------------------------- ### run() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Processes incoming mDNS queries and handles service announcements. ```APIDOC ## run() ### Description Processes incoming mDNS queries and handles service announcements. This method must be called periodically in the main loop for the mDNS responder to function properly. ``` -------------------------------- ### Register Service with TXT Record using Arduino mDNS Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Registers a service with additional TXT record metadata for richer service information. TXT records are key-value pairs. Ensure necessary libraries are included. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); EthernetServer server(80); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); server.begin(); mdns.begin(Ethernet.localIP(), "arduino"); // Register service without TXT record mdns.addServiceRecord( "Arduino Main Page._http", 80, MDNSServiceTCP ); // Register service with TXT record pointing to specific path // TXT format: length byte followed by content (e.g., "\x7path=/2") mdns.addServiceRecord( "Arduino Page 2._http", 80, MDNSServiceTCP, "\x7path=/2" // TXT record: path=/2 ); } void loop() { mdns.run(); EthernetClient client = server.available(); if (client) { // Handle HTTP request... client.stop(); } } ``` -------------------------------- ### Initialize MDNS with UDP Object Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Creates an MDNS instance using a UDP object for network communication. This constructor accepts any UDP-derived object, supporting both Ethernet and WiFi connections. Ensure Ethernet.begin() is called before initializing MDNS. ```cpp #include #include #include #include // Create UDP object for network communication EthernetUDP udp; // Initialize MDNS with the UDP object MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); // MDNS is ready to be configured } ``` -------------------------------- ### MDNS Constructor Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Initializes an MDNS instance using a UDP object for network communication. ```APIDOC ## MDNS Constructor ### Description Creates an MDNS instance using a UDP object for network communication. The constructor accepts any UDP-derived object, allowing the library to work with both Ethernet (EthernetUDP) and WiFi (WiFiUDP) connections. ### Parameters - **udp** (UDP object) - Required - A UDP-derived object for network communication. ``` -------------------------------- ### Process MDNS Queries with run() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Processes incoming mDNS queries and handles service announcements. This method must be called periodically in the main loop for mDNS to function correctly. It manages query processing, service re-announcements, and timeouts. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); mdns.addServiceRecord("Arduino Web Server._http", 80, MDNSServiceTCP); } void loop() { // CRITICAL: Call run() every iteration for mDNS to work // This processes queries and handles automatic re-announcements mdns.run(); // Your application code here // ... } ``` -------------------------------- ### Register TCP and UDP Services Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Use MDNSServiceTCP or MDNSServiceUDP constants when registering services to specify the protocol type. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Register TCP service (HTTP, SSH, etc.) mdns.addServiceRecord("Web Server._http", 80, MDNSServiceTCP); // Register UDP service (DNS, NTP, etc.) mdns.addServiceRecord("Time Server._ntp", 123, MDNSServiceUDP); } void loop() { mdns.run(); } ``` -------------------------------- ### addServiceRecord() - Register Service Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Registers a network service for DNS-SD discovery, making it visible to Bonjour-enabled clients. The service name format is 'ServiceName._servicetype'. ```APIDOC ## addServiceRecord() ### Description Registers a network service for DNS-SD discovery. The service becomes visible to Bonjour-enabled clients on the network. The name format is "ServiceName._servicetype" where common service types include `_http`, `_ssh`, `_ftp`, and `_printer`. Protocol can be `MDNSServiceTCP` or `MDNSServiceUDP`. Returns 1 on success, 0 on failure. ### Method `addServiceRecord(const char* name, uint16_t port, uint8_t protocol)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); EthernetServer server(80); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); server.begin(); mdns.begin(Ethernet.localIP(), "arduino"); // Register HTTP service on port 80 (TCP) int result = mdns.addServiceRecord( "Arduino mDNS Webserver._http", // Service name and type 80, // Port number MDNSServiceTCP // Protocol (TCP or UDP) ); if (result == 1) { Serial.println("Service registered successfully"); } } void loop() { mdns.run(); // Handle web server requests... } ``` ### Response #### Success Response (1) Returns 1 on success. #### Error Response (0) Returns 0 on failure. #### Response Example None ``` -------------------------------- ### setName() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Changes the mDNS hostname after initialization. ```APIDOC ## setName() ### Description Changes the mDNS hostname after initialization. The name should not include the .local suffix. ### Parameters - **name** (String) - Required - The new hostname. ### Response - **result** (int) - Returns 1 on success, 0 on failure. ``` -------------------------------- ### Remove Services using Arduino mDNS Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Demonstrates removing registered services from DNS-SD. Services can be removed by port and protocol, or by name, port, and protocol. Includes removing all services. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Register services mdns.addServiceRecord("Web Server._http", 80, MDNSServiceTCP); mdns.addServiceRecord("SSH Service._ssh", 22, MDNSServiceTCP); } void loop() { mdns.run(); // Remove service by port and protocol mdns.removeServiceRecord(80, MDNSServiceTCP); // Remove service by name, port, and protocol mdns.removeServiceRecord("SSH Service._ssh", 22, MDNSServiceTCP); // Remove all registered services mdns.removeAllServiceRecords(); } ``` -------------------------------- ### Change MDNS Hostname with setName() Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Changes the mDNS hostname after initialization. Returns 1 on success, 0 on failure. The `.local` suffix is appended automatically, so it should not be included in the provided name. This function is useful for dynamic hostname updates. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Change hostname to "mydevice.local" int result = mdns.setName("mydevice"); if (result == 1) { Serial.println("Hostname changed to mydevice.local"); } else { Serial.println("Failed to change hostname"); } } void loop() { mdns.run(); } ``` -------------------------------- ### Resolve mDNS Hostname to IP with Callback Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Resolves a mDNS hostname to an IP address and uses a callback function to handle the results. The callback receives the hostname and IP address, or INADDR_NONE on failure. Do not include the '.local' suffix when resolving. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Callback function for name resolution results void nameFound(const char* name, IPAddress ip) { if (ip != INADDR_NONE) { Serial.print("Resolved '"); Serial.print(name); Serial.print("' to "); Serial.println(ip); } else { Serial.print("Failed to resolve '"); Serial.print(name); Serial.println("'"); } } void setup() { Serial.begin(9600); Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Set callback for name resolution mdns.setNameResolvedCallback(nameFound); // Resolve a hostname with 5 second timeout // Do NOT include ".local" suffix mdns.resolveName("otherdevice", 5000); } void loop() { mdns.run(); // Check if still resolving if (mdns.isResolvingName()) { // Resolution in progress } // Cancel ongoing resolution if needed // mdns.cancelResolveName(); } ``` -------------------------------- ### Register HTTP Service with Arduino mDNS Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Registers an HTTP service on port 80 for DNS-SD discovery. This service will be visible to Bonjour-enabled clients. Ensure SPI, Ethernet, EthernetUdp, and ArduinoMDNS libraries are included. ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); EthernetServer server(80); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); server.begin(); mdns.begin(Ethernet.localIP(), "arduino"); // Register HTTP service on port 80 (TCP) // Will appear in Safari's Bonjour bookmarks int result = mdns.addServiceRecord( "Arduino mDNS Webserver._http", // Service name and type 80, // Port number MDNSServiceTCP // Protocol (TCP or UDP) ); if (result == 1) { Serial.println("Service registered successfully"); } } void loop() { mdns.run(); // Handle web server requests EthernetClient client = server.available(); if (client) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("Hello from Arduino!"); delay(1); client.stop(); } } ``` -------------------------------- ### addServiceRecord() with TXT Record - Register Service with Metadata Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Registers a service with additional TXT record metadata, allowing for extra information like paths or versions. ```APIDOC ## addServiceRecord() with TXT Record ### Description Registers a service with additional TXT record metadata. TXT records can contain key-value pairs providing extra information about the service, such as paths, versions, or configuration details. ### Method `addServiceRecord(const char* name, uint16_t port, uint8_t protocol, const char* txt)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); EthernetServer server(80); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); server.begin(); mdns.begin(Ethernet.localIP(), "arduino"); // Register service without TXT record mdns.addServiceRecord( "Arduino Main Page._http", 80, MDNSServiceTCP ); // Register service with TXT record pointing to specific path // TXT format: length byte followed by content (e.g., "\x7path=/2") mdns.addServiceRecord( "Arduino Page 2._http", 80, MDNSServiceTCP, "\x7path=/2" // TXT record: path=/2 ); } void loop() { mdns.run(); // Handle HTTP request... } ``` ### Response #### Success Response None explicitly mentioned, assumes success if no error is returned. #### Response Example None ``` -------------------------------- ### setNameResolvedCallback() and resolveName() - Resolve Hostname Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Resolves a mDNS hostname to an IP address. A callback function can be set to receive resolution results. ```APIDOC ## setNameResolvedCallback() and resolveName() ### Description Resolves a mDNS hostname to an IP address. Set a callback function to receive resolution results, then call `resolveName()` with the hostname (without `.local` suffix) and a timeout in milliseconds. The callback receives the hostname and IP address (or `INADDR_NONE` on timeout). ### Method `setNameResolvedCallback(void (*callback)(const char* name, IPAddress ip))` `resolveName(const char* name, uint32_t timeout)` `isResolvingName()` `cancelResolveName()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Callback function for name resolution results void nameFound(const char* name, IPAddress ip) { if (ip != INADDR_NONE) { Serial.print("Resolved '" ); Serial.print(name); Serial.print("' to "); Serial.println(ip); } else { Serial.print("Failed to resolve '" ); Serial.print(name); Serial.println("'"); } } void setup() { Serial.begin(9600); Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Set callback for name resolution mdns.setNameResolvedCallback(nameFound); // Resolve a hostname with 5 second timeout // Do NOT include ".local" suffix mdns.resolveName("otherdevice", 5000); } void loop() { mdns.run(); // Check if still resolving if (mdns.isResolvingName()) { // Resolution in progress } // Cancel ongoing resolution if needed // mdns.cancelResolveName(); } ``` ### Response #### Success Response Callback function `nameFound` is invoked with the resolved IP address. #### Error Response Callback function `nameFound` is invoked with `INADDR_NONE` if resolution fails or times out. #### Response Example ``` Resolved 'otherdevice' to 192.168.1.100 ``` ``` -------------------------------- ### removeServiceRecord() - Remove Service Source: https://context7.com/arduino-libraries/arduinomdns/llms.txt Removes a previously registered service from DNS-SD. Services can be removed by port and protocol, or by name, port, and protocol. ```APIDOC ## removeServiceRecord() ### Description Removes a previously registered service from DNS-SD. Can remove by port and protocol, or by name, port, and protocol. The library sends a service release message with TTL of 0 before removing. ### Method `removeServiceRecord(uint16_t port, uint8_t protocol)` `removeServiceRecord(const char* name, uint16_t port, uint8_t protocol)` `removeAllServiceRecords()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include #include #include EthernetUDP udp; MDNS mdns(udp); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Ethernet.begin(mac); mdns.begin(Ethernet.localIP(), "arduino"); // Register services mdns.addServiceRecord("Web Server._http", 80, MDNSServiceTCP); mdns.addServiceRecord("SSH Service._ssh", 22, MDNSServiceTCP); } void loop() { mdns.run(); // Remove service by port and protocol mdns.removeServiceRecord(80, MDNSServiceTCP); // Remove service by name, port, and protocol mdns.removeServiceRecord("SSH Service._ssh", 22, MDNSServiceTCP); // Remove all registered services mdns.removeAllServiceRecords(); } ``` ### Response #### Success Response None explicitly mentioned, assumes success if no error is returned. #### Response Example None ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.