11/* ***************************************************************************************************************************
22 WiFiDiscoveringServices.ino
3-
3+
44 mDNS library to support mDNS (registering services) and DNS-SD (service discovery).
5-
5+
66 Based on and modified from https://github.com/arduino-libraries/ArduinoMDNS
77 Built by Khoi Hoang https://github.com/khoih-prog/MDNS_Generic
88 Licensed under MIT license
9-
9+
1010 Original Author: Georg Kaindl (http://gkaindl.com)
11-
11+
1212 This file is part of Arduino EthernetBonjour.
13-
13+
1414 EthernetBonjour is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
1515 as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16-
16+
1717 EthernetBonjour is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
1818 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19-
20- You should have received a copy of the GNU Lesser General Public License along with EthernetBonjour.
19+
20+ You should have received a copy of the GNU Lesser General Public License along with EthernetBonjour.
2121 If not, see <http://www.gnu.org/licenses/>.
2222 *****************************************************************************************************************************/
2323
3232
3333#else
3434 #if !defined(WIFI_NETWORK_TYPE)
35- // Select one, but WIFI_NETWORK_ESP
36- #warning You have to select a WiFi Network here, or default will be used => possibly creating error.
37-
38- #define WIFI_NETWORK_TYPE NETWORK_WIFI_DEFAULT
39- // #define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFININA
40- // #define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFI101
41- // #define WIFI_NETWORK_TYPE WIFI_NETWORK_ESPAT
42- // #define WIFI_NETWORK_TYPE WIFI_NETWORK_ESP
43- // #define WIFI_NETWORK_TYPE WIFI_NETWORK_PORTENTA_H7
35+ // Select one, but WIFI_NETWORK_ESP
36+ #warning You have to select a WiFi Network here, or default will be used => possibly creating error.
37+
38+ #define WIFI_NETWORK_TYPE NETWORK_WIFI_DEFAULT
39+ // #define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFININA
40+ // #define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFI101
41+ // #define WIFI_NETWORK_TYPE WIFI_NETWORK_ESPAT
42+ // #define WIFI_NETWORK_TYPE WIFI_NETWORK_ESP
43+ // #define WIFI_NETWORK_TYPE WIFI_NETWORK_PORTENTA_H7
4444 #endif
45-
45+
4646 String hostname = BOARD_TYPE;
47-
4847#endif
4948
5049#if (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFININA)
50+
5151 #include < SPI.h>
5252 #include < WiFiNINA_Generic.h>
5353 #include < WiFiUdp_Generic.h>
5656 WiFiServer server (80 );
5757
5858#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFI101)
59+
5960 #include < SPI.h>
6061 #include < WiFi101.h>
6162 #include < WiFiUdp.h>
9899#else
99100
100101 #error You have to select an appropriate WiFi network type from the list
101-
102+
102103#endif
103104
104105// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
@@ -134,57 +135,45 @@ void printWifiStatus()
134135 Serial.println (" dBm" );
135136}
136137
137- // This function is called when a name is resolved via mMDNS/Bonjour. We set
138- // this up in the setup() function above. The name you give to this callback
139- // function does not matter at all, but it must take exactly these arguments
140- // as below.
141- // If a service is discovered, name, ipAddr, port and (if available) txtContent
142- // will be set.
143- // If your specified discovery timeout is reached, the function will be called
144- // with name (and all successive arguments) being set to NULL.
145- void serviceFound (const char * type, MDNSServiceProtocol /* proto*/ ,
146- const char * name, IPAddress ip,
147- unsigned short port,
148- const char * txtContent)
138+ // This function is called when a name is resolved via MDNS/Bonjour. We set this up in the setup() function above. The name you give to this callback
139+ // function does not matter at all, but it must take exactly these arguments as below. If a service is discovered, name, ipAddr, port and (if available)
140+ // txtContent will be set. If your specified discovery timeout is reached, the function will be called with name (and all successive arguments) being set to NULL.
141+ void serviceFound (const char * type, MDNSServiceProtocol /* proto*/ , const char * name, IPAddress ip,
142+ unsigned short port, const char * txtContent)
149143{
150- if (NULL == name)
144+ if (NULL == name)
151145 {
152146 Serial.print (" Finished discovering services of type " );
153147 Serial.println (type);
154- }
155- else
148+ }
149+ else
156150 {
157- Serial.print (" Found: '" );
158- Serial.print (name);
159- Serial.print (" ' at " );
160- Serial.print (ip);
161- Serial.print (" , port " );
162- Serial.print (port);
163- Serial.println (" (TCP)" );
164-
165- // Check out http://www.zeroconf.org/Rendezvous/txtrecords.html for a
166- // primer on the structure of TXT records. Note that the Bonjour
167- // library will always return the txt content as a zero-terminated
168- // string, even if the specification does not require this.
169- if (txtContent)
151+ Serial.print (" Found: '" ); Serial.print (name);
152+ Serial.print (" ' at " ); Serial.print (ip);
153+ Serial.print (" , port " ); Serial.print (port); Serial.println (" (TCP)" );
154+
155+ // Check out http://www.zeroconf.org/Rendezvous/txtrecords.html for a primer on the structure of TXT records. Note that the Bonjour
156+ // library will always return the txt content as a zero-terminated string, even if the specification does not require this.
157+ if (txtContent)
170158 {
171159 Serial.print (" \t txt record: " );
172-
160+
173161 char buf[256 ];
174162 char len = *txtContent++;
175- int i = 0 ;
176-
177- while (len)
163+ int i= 0 ;
164+
165+ while (len)
178166 {
179167 i = 0 ;
180-
168+
181169 while (len--)
182170 buf[i++] = *txtContent++;
183-
171+
184172 buf[i] = ' \0 ' ;
173+
185174 Serial.print (buf);
186175 len = *txtContent++;
187-
176+
188177 if (len)
189178 Serial.print (" , " );
190179 else
@@ -198,7 +187,7 @@ void setup()
198187{
199188 // Initialize serial and wait for port to open:
200189 Serial.begin (115200 );
201- while (!Serial);
190+ while (!Serial && millis () < 5000 );
202191
203192 Serial.print (" \n Start WiFiDiscoveringServices on " ); Serial.println (BOARD_NAME);
204193 Serial.println (MDNS_GENERIC_VERSION);
@@ -238,18 +227,14 @@ void setup()
238227#endif
239228
240229 Serial.print (" Attempting to connect to SSID: " ); Serial.println (ssid);
241-
230+
242231 WiFi.begin (ssid, pass);
243-
232+
244233 delay (1000 );
245-
246- // attempt to connect to Wifi network:
247- while (status != WL_CONNECTED)
248- {
249- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
250- status = WiFi.status ();
251234
252- delay (1000 );
235+ while (WiFi.status () != WL_CONNECTED)
236+ {
237+ delay (500 );
253238 }
254239
255240 printWifiStatus ();
@@ -280,45 +265,43 @@ void setup()
280265}
281266
282267void loop ()
283- {
268+ {
284269 char serviceName[256 ];
285- int length = 0 ;
286-
270+ uint8_t length = 0 ;
271+
287272 // read in a service name from the Arduino IDE's serial monitor.
288- while (Serial.available ())
273+ while (Serial.available ())
289274 {
290275 serviceName[length] = Serial.read ();
291- length = (length + 1 ) % 256 ;
276+ length = (length+ 1 ) % 256 ;
292277 delay (5 );
293278 }
294279
280+ // Terminal adds "\r\n", need to remove
281+ if (length > 1 )
282+ length = (length - 2 ) % 256 ;
283+
295284 serviceName[length] = ' \0 ' ;
296-
297- // You can use the "isDiscoveringService()" function to find out whether the
298- // mDNS library is currently discovering service instances.
285+
286+ // You can use the "isDiscoveringService()" function to find out whether the Bonjour library is currently discovering service instances.
299287 // If so, we skip this input, since we want our previous request to continue.
300- if (!mdns.isDiscoveringService ())
288+ if (!mdns.isDiscoveringService ())
301289 {
302- if (length > 0 )
290+ if (length > 0 )
303291 {
304292 Serial.print (" Discovering services of type '" );
305293 Serial.print (serviceName);
306294 Serial.println (" ' via Multi-Cast DNS (Bonjour)..." );
307295
308- // Now we tell the mDNS library to discover the service. Below, I have
309- // hardcoded the TCP protocol, but you can also specify to discover UDP
310- // services.
311- // The last argument is a duration (in milliseconds) for which we will
312- // search (specify 0 to run the discovery indefinitely).
313- // Note that the library will resend the discovery message every 10
314- // seconds, so if you search for longer than that, you will receive
315- // duplicate instances.
296+ // Now we tell the mDNS library to discover the service. Below, I have hardcoded the TCP protocol, but you can also specify to discover UDP services.
297+ // The last argument is a duration (in milliseconds) for which we will search (specify 0 to run the discovery indefinitely). Note that the library
298+ // will resend the discovery message every 10 seconds, so if you search for longer than that, you will receive duplicate instances.
316299
317300 mdns.startDiscoveringService (serviceName, MDNSServiceTCP, 5000 );
318- }
301+ }
319302 }
320303
321- // This actually runs the mDNS module. YOU HAVE TO CALL THIS PERIODICALLY,
304+ // This actually runs the Bonjour module. YOU HAVE TO CALL THIS PERIODICALLY,
322305 // OR NOTHING WILL WORK!
323306 // Preferably, call it once per loop().
324307 mdns.run ();
0 commit comments