From 2635c37c4327c511126c4725c7602481b3fab4e5 Mon Sep 17 00:00:00 2001 From: hreintke Date: Sat, 5 Jan 2019 15:16:38 +0100 Subject: [PATCH 1/3] Update mDNS examples to use HTTP Server instead of TCP Server --- .../mDNS_ServiceMonitor.ino | 109 ++++++------------ 1 file changed, 36 insertions(+), 73 deletions(-) diff --git a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino index 3823b1c391..cf37f7d7e4 100644 --- a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino +++ b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino @@ -32,6 +32,7 @@ #include #include +#include /* Include the MDNSResponder (the library needs to be included also) @@ -69,8 +70,8 @@ MDNSResponder::hMDNSServiceQuery hMDNSServiceQuery = 0; const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!
"; 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); /* @@ -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 += "
  • "; 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 += "
    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 += "
    IP4: "; for (uint32_t u2 = 0; u2 < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) { if (0 != u2) { strHTTPServices += ", "; @@ -169,10 +168,10 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder, } // MDNS TXT items if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) { - strHTTPServices += " TXT: "; + strHTTPServices += "
    TXT: "; strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u); } - strHTTPServices += "
    "; + strHTTPServices += "
  • "; } } else { strHTTPServices = cstrNoHTTPServices; @@ -181,7 +180,6 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder, return true; } - /* MDNSProbeResultCallback @@ -235,69 +233,37 @@ bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder, } } } else { - // Change hostname, use '-' as divider between base name and index - if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { - p_pMDNSResponder->setHostname(pcHostDomain); - } else { - Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); + // Change hostname, use '-' as divider between base name and index + if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { + p_pMDNSResponder->setHostname(pcHostDomain); + } else { + Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); + } } - } } 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\r\nHello from "; - s += WiFi.hostname() + " at " + ipStr; - // Simple addition of the current time - s += "
    Local HTTP services:
    "; - s += strHTTPServices; - // done :-) - s += "\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"); + + IPAddress ip = WiFi.localIP(); + String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); + String s = "\r\n

    Hello from "; + s += WiFi.hostname() + ".local at " + ipStr + "

    "; + s += "

    Local HTTP services are :

      "; + s += strHTTPServices; + // done :-) + s += "
    "; + Serial.println("Sending 200"); + server.send(200, "text/html", s); + Serial.println("Done with request"); } - /* setup */ @@ -321,6 +287,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' @@ -333,9 +302,9 @@ 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"); } @@ -343,14 +312,8 @@ void setup(void) { 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(); } - - From 7de6f04d60a40104f20a11267bf3794a2ce5009a Mon Sep 17 00:00:00 2001 From: hreintke Date: Sat, 5 Jan 2019 15:49:40 +0100 Subject: [PATCH 2/3] Style fix --- .../mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino index cf37f7d7e4..90c2a82d81 100644 --- a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino +++ b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino @@ -233,13 +233,13 @@ bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder, } } } else { - // Change hostname, use '-' as divider between base name and index - if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { - p_pMDNSResponder->setHostname(pcHostDomain); - } else { - Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); - } + // Change hostname, use '-' as divider between base name and index + if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { + p_pMDNSResponder->setHostname(pcHostDomain); + } else { + Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); } + } } return true; } From 5f88f0b7646f4a246740114e9273b1625be99227 Mon Sep 17 00:00:00 2001 From: Develo Date: Tue, 22 Jan 2019 00:40:21 -0300 Subject: [PATCH 3/3] Update mDNS_ServiceMonitor.ino @d-a-v's feedback --- .../LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino index 90c2a82d81..1bfe7c3802 100644 --- a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino +++ b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino @@ -251,10 +251,8 @@ void handleHTTPRequest() { Serial.println(""); Serial.println("HTTP Request"); - IPAddress ip = WiFi.localIP(); - String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); String s = "\r\n

    Hello from "; - s += WiFi.hostname() + ".local at " + ipStr + "

    "; + s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + ""; s += "

    Local HTTP services are :

      "; s += strHTTPServices; // done :-)