Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mDNS examples to use HTTP Server instead of TCP Server #5589

Merged
merged 4 commits into from
Jan 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

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

// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
WiFiServer server(SERVICE_PORT);
// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests
ESP8266WebServer server(SERVICE_PORT);


/*
Expand Down Expand Up @@ -145,21 +146,19 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
strHTTPServices = "";
for (uint32_t u = 0; u < u32Answers; ++u) {
// Index and service domain
strHTTPServices += u;
strHTTPServices += ": ";
strHTTPServices += "<li>";
strHTTPServices += p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, u);
// Host domain and port
if ((p_pMDNSResponder->hasAnswerHostDomain(p_hServiceQuery, u)) &&
(p_pMDNSResponder->hasAnswerPort(p_hServiceQuery, u))) {

strHTTPServices += " at ";
strHTTPServices += "<br/>Hostname: ";
strHTTPServices += p_pMDNSResponder->answerHostDomain(p_hServiceQuery, u);
strHTTPServices += ":";
strHTTPServices += p_pMDNSResponder->answerPort(p_hServiceQuery, u);
}
// IP4 address
if (p_pMDNSResponder->hasAnswerIP4Address(p_hServiceQuery, u)) {
strHTTPServices += " IP4: ";
strHTTPServices += "<br/>IP4: ";
for (uint32_t u2 = 0; u2 < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) {
if (0 != u2) {
strHTTPServices += ", ";
Expand All @@ -169,10 +168,10 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
}
// MDNS TXT items
if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) {
strHTTPServices += " TXT: ";
strHTTPServices += "<br/>TXT: ";
strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u);
}
strHTTPServices += "<br/>";
strHTTPServices += "</li>";
}
} else {
strHTTPServices = cstrNoHTTPServices;
Expand All @@ -181,7 +180,6 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
return true;
}


/*
MDNSProbeResultCallback

Expand Down Expand Up @@ -246,58 +244,24 @@ bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
return true;
}


/*
handleHTTPClient
HTTP request function (not found is handled by server)
*/
void handleHTTPClient(WiFiClient& client) {
void handleHTTPRequest() {
Serial.println("");
Serial.println("New client");

// Wait for data from client to become available
while (client.connected() && !client.available()) {
delay(1);
}

// Read the first line of HTTP request
String req = client.readStringUntil('\r');

// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();

String s;
if (req == "/") {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ";
s += WiFi.hostname() + " at " + ipStr;
// Simple addition of the current time
s += "<br/>Local HTTP services:<br/>";
s += strHTTPServices;
// done :-)
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
} else {
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
client.print(s);

Serial.println("Done with client");
Serial.println("HTTP Request");

String s = "<!DOCTYPE HTML>\r\n<html><h3><head>Hello from ";
s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "</h3></head>";
s += "<br/><h4>Local HTTP services are :</h4><ol>";
s += strHTTPServices;
// done :-)
s += "</ol></html>";
Serial.println("Sending 200");
server.send(200, "text/html", s);
Serial.println("Done with request");
}


/*
setup
*/
Expand All @@ -321,6 +285,9 @@ void setup(void) {
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

// Setup HTTP server
server.on("/", handleHTTPRequest);

// Setup MDNS responder
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
// Init the (currently empty) host domain string with 'esp8266'
Expand All @@ -333,24 +300,18 @@ void setup(void) {
}
Serial.println("MDNS responder started");

// Start TCP (HTTP) server
// Start HTTP server
server.begin();
Serial.println("TCP server started");
Serial.println("HTTP server started");
}


/*
loop
*/
void loop(void) {
// Check if a client has connected
WiFiClient client = server.available();
if (client) {
handleHTTPClient(client);
}

// Check if a request has come in
server.handleClient();
// Allow MDNS processing
MDNS.update();
}