Skip to content

Commit e9a6fd2

Browse files
hreintked-a-v
authored andcommitted
Update mDNS examples to use HTTP Server instead of TCP Server (#5589)
1 parent d7094f2 commit e9a6fd2

File tree

1 file changed

+28
-67
lines changed

1 file changed

+28
-67
lines changed

Diff for: libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino

+28-67
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <ESP8266WiFi.h>
3434
#include <WiFiClient.h>
35+
#include <ESP8266WebServer.h>
3536

3637
/*
3738
Include the MDNSResponder (the library needs to be included also)
@@ -69,8 +70,8 @@ MDNSResponder::hMDNSServiceQuery hMDNSServiceQuery = 0;
6970
const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!<br/>";
7071
String strHTTPServices = cstrNoHTTPServices;
7172

72-
// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
73-
WiFiServer server(SERVICE_PORT);
73+
// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests
74+
ESP8266WebServer server(SERVICE_PORT);
7475

7576

7677
/*
@@ -145,21 +146,19 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
145146
strHTTPServices = "";
146147
for (uint32_t u = 0; u < u32Answers; ++u) {
147148
// Index and service domain
148-
strHTTPServices += u;
149-
strHTTPServices += ": ";
149+
strHTTPServices += "<li>";
150150
strHTTPServices += p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, u);
151151
// Host domain and port
152152
if ((p_pMDNSResponder->hasAnswerHostDomain(p_hServiceQuery, u)) &&
153153
(p_pMDNSResponder->hasAnswerPort(p_hServiceQuery, u))) {
154-
155-
strHTTPServices += " at ";
154+
strHTTPServices += "<br/>Hostname: ";
156155
strHTTPServices += p_pMDNSResponder->answerHostDomain(p_hServiceQuery, u);
157156
strHTTPServices += ":";
158157
strHTTPServices += p_pMDNSResponder->answerPort(p_hServiceQuery, u);
159158
}
160159
// IP4 address
161160
if (p_pMDNSResponder->hasAnswerIP4Address(p_hServiceQuery, u)) {
162-
strHTTPServices += " IP4: ";
161+
strHTTPServices += "<br/>IP4: ";
163162
for (uint32_t u2 = 0; u2 < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) {
164163
if (0 != u2) {
165164
strHTTPServices += ", ";
@@ -169,10 +168,10 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
169168
}
170169
// MDNS TXT items
171170
if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) {
172-
strHTTPServices += " TXT: ";
171+
strHTTPServices += "<br/>TXT: ";
173172
strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u);
174173
}
175-
strHTTPServices += "<br/>";
174+
strHTTPServices += "</li>";
176175
}
177176
} else {
178177
strHTTPServices = cstrNoHTTPServices;
@@ -181,7 +180,6 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
181180
return true;
182181
}
183182

184-
185183
/*
186184
MDNSProbeResultCallback
187185
@@ -246,58 +244,24 @@ bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
246244
return true;
247245
}
248246

249-
250247
/*
251-
handleHTTPClient
248+
HTTP request function (not found is handled by server)
252249
*/
253-
void handleHTTPClient(WiFiClient& client) {
250+
void handleHTTPRequest() {
254251
Serial.println("");
255-
Serial.println("New client");
256-
257-
// Wait for data from client to become available
258-
while (client.connected() && !client.available()) {
259-
delay(1);
260-
}
261-
262-
// Read the first line of HTTP request
263-
String req = client.readStringUntil('\r');
264-
265-
// First line of HTTP request looks like "GET /path HTTP/1.1"
266-
// Retrieve the "/path" part by finding the spaces
267-
int addr_start = req.indexOf(' ');
268-
int addr_end = req.indexOf(' ', addr_start + 1);
269-
if (addr_start == -1 || addr_end == -1) {
270-
Serial.print("Invalid request: ");
271-
Serial.println(req);
272-
return;
273-
}
274-
req = req.substring(addr_start + 1, addr_end);
275-
Serial.print("Request: ");
276-
Serial.println(req);
277-
client.flush();
278-
279-
String s;
280-
if (req == "/") {
281-
IPAddress ip = WiFi.localIP();
282-
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
283-
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ";
284-
s += WiFi.hostname() + " at " + ipStr;
285-
// Simple addition of the current time
286-
s += "<br/>Local HTTP services:<br/>";
287-
s += strHTTPServices;
288-
// done :-)
289-
s += "</html>\r\n\r\n";
290-
Serial.println("Sending 200");
291-
} else {
292-
s = "HTTP/1.1 404 Not Found\r\n\r\n";
293-
Serial.println("Sending 404");
294-
}
295-
client.print(s);
296-
297-
Serial.println("Done with client");
252+
Serial.println("HTTP Request");
253+
254+
String s = "<!DOCTYPE HTML>\r\n<html><h3><head>Hello from ";
255+
s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "</h3></head>";
256+
s += "<br/><h4>Local HTTP services are :</h4><ol>";
257+
s += strHTTPServices;
258+
// done :-)
259+
s += "</ol></html>";
260+
Serial.println("Sending 200");
261+
server.send(200, "text/html", s);
262+
Serial.println("Done with request");
298263
}
299264

300-
301265
/*
302266
setup
303267
*/
@@ -321,6 +285,9 @@ void setup(void) {
321285
Serial.print("IP address: ");
322286
Serial.println(WiFi.localIP());
323287

288+
// Setup HTTP server
289+
server.on("/", handleHTTPRequest);
290+
324291
// Setup MDNS responder
325292
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
326293
// Init the (currently empty) host domain string with 'esp8266'
@@ -333,24 +300,18 @@ void setup(void) {
333300
}
334301
Serial.println("MDNS responder started");
335302

336-
// Start TCP (HTTP) server
303+
// Start HTTP server
337304
server.begin();
338-
Serial.println("TCP server started");
305+
Serial.println("HTTP server started");
339306
}
340307

341308

342309
/*
343310
loop
344311
*/
345312
void loop(void) {
346-
// Check if a client has connected
347-
WiFiClient client = server.available();
348-
if (client) {
349-
handleHTTPClient(client);
350-
}
351-
313+
// Check if a request has come in
314+
server.handleClient();
352315
// Allow MDNS processing
353316
MDNS.update();
354317
}
355-
356-

0 commit comments

Comments
 (0)