This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWiFiResolvingHostNames.ino
304 lines (238 loc) · 9.05 KB
/
WiFiResolvingHostNames.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/****************************************************************************************************************************
WiFiResolvingHostNames.ino
mDNS library to support mDNS (registering services) and DNS-SD (service discovery).
Based on and modified from https://github.com/arduino-libraries/ArduinoMDNS
Built by Khoi Hoang https://github.com/khoih-prog/MDNS_Generic
Licensed under MIT license
Original Author: Georg Kaindl (http://gkaindl.com)
This file is part of Arduino EthernetBonjour.
EthernetBonjour is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
EthernetBonjour is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with EthernetBonjour.
If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************************************************************/
// Illustrates how to resolve host names via MDNS (Multicast DNS)
#include "defines.h"
#include "arduino_secrets.h"
#if defined(ESP32)
String hostname = "ESP_" + String(ESP_getChipId(), HEX);
#else
#if !defined(WIFI_NETWORK_TYPE)
// Select one, but WIFI_NETWORK_ESP
#warning You have to select a WiFi Network here, or default will be used => possibly creating error.
#define WIFI_NETWORK_TYPE NETWORK_WIFI_DEFAULT
//#define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFININA
//#define WIFI_NETWORK_TYPE WIFI_NETWORK_WIFI101
//#define WIFI_NETWORK_TYPE WIFI_NETWORK_ESPAT
//#define WIFI_NETWORK_TYPE WIFI_NETWORK_ESP
//#define WIFI_NETWORK_TYPE WIFI_NETWORK_PORTENTA_H7
#endif
String hostname = BOARD_TYPE;
#endif
#if (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFININA)
#include <SPI.h>
#include <WiFiNINA_Generic.h>
#include <WiFiUdp_Generic.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFININA
WiFiUDP udp;
WiFiServer server(80);
#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_WIFI101)
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFI101
WiFiUDP udp;
WiFiServer server(80);
#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_ESPAT)
#include <WiFiEspAT.h>
#include <WiFiUdp.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFI_ESPAT
WiFiUDP udp;
WiFiServer server(80);
#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_ESP)
#include <WiFi.h>
#include <WiFiUdp.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFI_ESP
WiFiUDP udp;
WiFiServer server(80);
#elif (WIFI_NETWORK_TYPE == WIFI_NETWORK_PORTENTA_H7)
#include <WiFi.h>
#include <WiFiUdp.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFI_PORTENTA_H7
WiFiUDP udp;
WiFiServer server(80);
#elif (WIFI_NETWORK_TYPE == NETWORK_WIFI_DEFAULT)
//#error You must select a WiFi network type
#include <WiFi.h>
#include <WiFiUdp.h>
#warning WIFI_NETWORK_TYPE == NETWORK_WIFI_DEFAULT
WiFiUDP udp;
WiFiServer server(80);
#else
#error You have to select an appropriate WiFi network type from the list
#endif
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <MDNS_Generic.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP), length must be 8
int status = WL_IDLE_STATUS; // the Wifi radio's status
MDNS mdns(udp);
void nameFound(const char* name, IPAddress ip);
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// 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
// function does not matter at all, but it must take exactly these arguments
// (a const char*, which is the hostName you wanted resolved, and a const
// byte[4], which contains the IP address of the host on success, or NULL if
// the name resolution timed out).
void nameFound(const char* name, IPAddress ip)
{
if ( (ip[0] != 0) && (ip[1] != 0) && (ip[2] != 0) && (ip[3] != 0) )
//if ( (ip[0] != 0) || (ip[1] != 0) || (ip[2] != 0) || (ip[3] != 0) )
{
Serial.print("The IP address for '");
Serial.print(name);
Serial.print("' is ");
Serial.println(ip);
for (int i = 0; i < 4; i++)
{
MDNS_LOGDEBUG3("IP[", i, "]=", ip[i]);
}
}
else if (ip[0] == 0)
{
Serial.print("Resolving '");
Serial.print(name);
Serial.println("' error.");
}
else
{
Serial.print("Resolving '");
Serial.print(name);
Serial.println("' timed out.");
}
}
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial);
Serial.print("\nStart WiFiResolvingHostNames on "); Serial.println(BOARD_NAME);
Serial.println(MDNS_GENERIC_VERSION);
#if ( WIFI_NETWORK_WIFININA || WIFI_NETWORK_WIFI101 )
Serial.println("Used/default SPI pinout:");
Serial.print("MOSI:");
Serial.println(MOSI);
Serial.print("MISO:");
Serial.println(MISO);
Serial.print("SCK:");
Serial.println(SCK);
Serial.print("SS:");
Serial.println(SS);
// check for the WiFi module:
#if WIFI_NETWORK_WIFININA
if (WiFi.status() == WL_NO_MODULE)
#else
if (WiFi.status() == WL_NO_SHIELD)
#endif
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
#if WIFI_NETWORK_WIFININA
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
{
Serial.println("Please upgrade the firmware");
}
#endif
#endif
Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
WiFi.begin(ssid, pass);
delay(1000);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
printWifiStatus();
// Initialize the mDNS library. You can now reach or ping this
// Arduino via the host name "arduino.local", provided that your operating
// system is mDNS/Bonjour-enabled (such as MacOS X).
// Always call this before any other method!
//hostname.toUpperCase();
hostname.toLowerCase();
hostname.replace(" ", "-");
hostname.replace("_", "-");
Serial.print("Registering mDNS hostname: "); Serial.println(hostname);
Serial.print("To access, using "); Serial.print(hostname); Serial.println(".local");
// Initialize the mDNS library. You can now reach or ping this
// Arduino via the host name "hostname.local", provided that your operating
// system is mDNS/Bonjour-enabled (such as MacOS X).
// Always call this before any other method!
mdns.begin(WiFi.localIP(), hostname.c_str());
// We specify the function that the mDNS library will call when it
// resolves a host name. In this case, we will call the function named
// "nameFound".
mdns.setNameResolvedCallback(nameFound);
Serial.println("Enter a mDNS host name via the Arduino Serial Monitor to "
"have it resolved.");
Serial.println("Do not postfix the name with \".local\"");
}
void loop()
{
static char hostName[256];
//char hostName[512];
uint8_t length = 0;
// read in a host name from the Arduino IDE's serial monitor.
while (Serial.available())
{
hostName[length] = Serial.read();
length = (length + 1) % 256;
delay(5);
}
// Terminal adds "\r\n", need to remove
if (length > 1)
length = (length - 2) % 256;
hostName[length] = '\0';
// You can use the "isResolvingName()" function to find out whether the
// mDNS library is currently resolving a host name.
// If so, we skip this input, since we want our previous request to continue.
if (!mdns.isResolvingName())
{
if (length > 0)
{
MDNS_LOGDEBUG3("Receiving len = ", length, ", hostname", hostName);
Serial.print("Resolving '");
Serial.print(hostName);
Serial.println("' via Multicast DNS (Bonjour)...");
// Now we tell the mDNS library to resolve the host name. We give it a
// timeout of 5 seconds (e.g. 5000 milliseconds) to find an answer. The
// library will automatically resend the query every second until it
// either receives an answer or your timeout is reached - In either case,
// the callback function you specified in setup() will be called.
mdns.resolveName(hostName, 5000);
}
}
// This actually runs the mDNS module. YOU HAVE TO CALL THIS PERIODICALLY,
// OR NOTHING WILL WORK! Preferably, call it once per loop().
mdns.run();
//delay(1000);
}