1
1
/* ***************************************************************************************************************************
2
2
WiFiDiscoveringServices.ino
3
-
3
+
4
4
mDNS library to support mDNS (registering services) and DNS-SD (service discovery).
5
-
5
+
6
6
Based on and modified from https://github.com/arduino-libraries/ArduinoMDNS
7
7
Built by Khoi Hoang https://github.com/khoih-prog/MDNS_Generic
8
8
Licensed under MIT license
9
-
9
+
10
10
Original Author: Georg Kaindl (http://gkaindl.com)
11
-
11
+
12
12
This file is part of Arduino EthernetBonjour.
13
-
13
+
14
14
EthernetBonjour is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
15
15
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16
-
16
+
17
17
EthernetBonjour is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
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.
21
21
If not, see <http://www.gnu.org/licenses/>.
22
22
*****************************************************************************************************************************/
23
23
32
32
33
33
#else
34
34
#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
44
44
#endif
45
-
45
+
46
46
String hostname = BOARD_TYPE;
47
-
48
47
#endif
49
48
50
49
#if (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFININA)
50
+
51
51
#include < SPI.h>
52
52
#include < WiFiNINA_Generic.h>
53
53
#include < WiFiUdp_Generic.h>
56
56
WiFiServer server (80 );
57
57
58
58
#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFI101)
59
+
59
60
#include < SPI.h>
60
61
#include < WiFi101.h>
61
62
#include < WiFiUdp.h>
98
99
#else
99
100
100
101
#error You have to select an appropriate WiFi network type from the list
101
-
102
+
102
103
#endif
103
104
104
105
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
@@ -134,57 +135,45 @@ void printWifiStatus()
134
135
Serial.println (" dBm" );
135
136
}
136
137
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)
149
143
{
150
- if (NULL == name)
144
+ if (NULL == name)
151
145
{
152
146
Serial.print (" Finished discovering services of type " );
153
147
Serial.println (type);
154
- }
155
- else
148
+ }
149
+ else
156
150
{
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)
170
158
{
171
159
Serial.print (" \t txt record: " );
172
-
160
+
173
161
char buf[256 ];
174
162
char len = *txtContent++;
175
- int i = 0 ;
176
-
177
- while (len)
163
+ int i= 0 ;
164
+
165
+ while (len)
178
166
{
179
167
i = 0 ;
180
-
168
+
181
169
while (len--)
182
170
buf[i++] = *txtContent++;
183
-
171
+
184
172
buf[i] = ' \0 ' ;
173
+
185
174
Serial.print (buf);
186
175
len = *txtContent++;
187
-
176
+
188
177
if (len)
189
178
Serial.print (" , " );
190
179
else
@@ -198,7 +187,7 @@ void setup()
198
187
{
199
188
// Initialize serial and wait for port to open:
200
189
Serial.begin (115200 );
201
- while (!Serial);
190
+ while (!Serial && millis () < 5000 );
202
191
203
192
Serial.print (" \n Start WiFiDiscoveringServices on " ); Serial.println (BOARD_NAME);
204
193
Serial.println (MDNS_GENERIC_VERSION);
@@ -238,18 +227,14 @@ void setup()
238
227
#endif
239
228
240
229
Serial.print (" Attempting to connect to SSID: " ); Serial.println (ssid);
241
-
230
+
242
231
WiFi.begin (ssid, pass);
243
-
232
+
244
233
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 ();
251
234
252
- delay (1000 );
235
+ while (WiFi.status () != WL_CONNECTED)
236
+ {
237
+ delay (500 );
253
238
}
254
239
255
240
printWifiStatus ();
@@ -280,45 +265,43 @@ void setup()
280
265
}
281
266
282
267
void loop ()
283
- {
268
+ {
284
269
char serviceName[256 ];
285
- int length = 0 ;
286
-
270
+ uint8_t length = 0 ;
271
+
287
272
// read in a service name from the Arduino IDE's serial monitor.
288
- while (Serial.available ())
273
+ while (Serial.available ())
289
274
{
290
275
serviceName[length] = Serial.read ();
291
- length = (length + 1 ) % 256 ;
276
+ length = (length+ 1 ) % 256 ;
292
277
delay (5 );
293
278
}
294
279
280
+ // Terminal adds "\r\n", need to remove
281
+ if (length > 1 )
282
+ length = (length - 2 ) % 256 ;
283
+
295
284
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.
299
287
// If so, we skip this input, since we want our previous request to continue.
300
- if (!mdns.isDiscoveringService ())
288
+ if (!mdns.isDiscoveringService ())
301
289
{
302
- if (length > 0 )
290
+ if (length > 0 )
303
291
{
304
292
Serial.print (" Discovering services of type '" );
305
293
Serial.print (serviceName);
306
294
Serial.println (" ' via Multi-Cast DNS (Bonjour)..." );
307
295
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.
316
299
317
300
mdns.startDiscoveringService (serviceName, MDNSServiceTCP, 5000 );
318
- }
301
+ }
319
302
}
320
303
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,
322
305
// OR NOTHING WILL WORK!
323
306
// Preferably, call it once per loop().
324
307
mdns.run ();
0 commit comments