mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Update mDNS examples to use HTTP Server instead of TCP Server (#5589)
This commit is contained in:
parent
d7094f2269
commit
e9a6fd2f82
@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Include the MDNSResponder (the library needs to be included also)
|
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!<br/>";
|
const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!<br/>";
|
||||||
String strHTTPServices = cstrNoHTTPServices;
|
String strHTTPServices = cstrNoHTTPServices;
|
||||||
|
|
||||||
// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
|
// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests
|
||||||
WiFiServer server(SERVICE_PORT);
|
ESP8266WebServer server(SERVICE_PORT);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -145,21 +146,19 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
strHTTPServices = "";
|
strHTTPServices = "";
|
||||||
for (uint32_t u = 0; u < u32Answers; ++u) {
|
for (uint32_t u = 0; u < u32Answers; ++u) {
|
||||||
// Index and service domain
|
// Index and service domain
|
||||||
strHTTPServices += u;
|
strHTTPServices += "<li>";
|
||||||
strHTTPServices += ": ";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, u);
|
strHTTPServices += p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, u);
|
||||||
// Host domain and port
|
// Host domain and port
|
||||||
if ((p_pMDNSResponder->hasAnswerHostDomain(p_hServiceQuery, u)) &&
|
if ((p_pMDNSResponder->hasAnswerHostDomain(p_hServiceQuery, u)) &&
|
||||||
(p_pMDNSResponder->hasAnswerPort(p_hServiceQuery, u))) {
|
(p_pMDNSResponder->hasAnswerPort(p_hServiceQuery, u))) {
|
||||||
|
strHTTPServices += "<br/>Hostname: ";
|
||||||
strHTTPServices += " at ";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerHostDomain(p_hServiceQuery, u);
|
strHTTPServices += p_pMDNSResponder->answerHostDomain(p_hServiceQuery, u);
|
||||||
strHTTPServices += ":";
|
strHTTPServices += ":";
|
||||||
strHTTPServices += p_pMDNSResponder->answerPort(p_hServiceQuery, u);
|
strHTTPServices += p_pMDNSResponder->answerPort(p_hServiceQuery, u);
|
||||||
}
|
}
|
||||||
// IP4 address
|
// IP4 address
|
||||||
if (p_pMDNSResponder->hasAnswerIP4Address(p_hServiceQuery, u)) {
|
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) {
|
for (uint32_t u2 = 0; u2 < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) {
|
||||||
if (0 != u2) {
|
if (0 != u2) {
|
||||||
strHTTPServices += ", ";
|
strHTTPServices += ", ";
|
||||||
@ -169,10 +168,10 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
}
|
}
|
||||||
// MDNS TXT items
|
// MDNS TXT items
|
||||||
if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) {
|
if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) {
|
||||||
strHTTPServices += " TXT: ";
|
strHTTPServices += "<br/>TXT: ";
|
||||||
strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u);
|
strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u);
|
||||||
}
|
}
|
||||||
strHTTPServices += "<br/>";
|
strHTTPServices += "</li>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
strHTTPServices = cstrNoHTTPServices;
|
strHTTPServices = cstrNoHTTPServices;
|
||||||
@ -181,7 +180,6 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
MDNSProbeResultCallback
|
MDNSProbeResultCallback
|
||||||
|
|
||||||
@ -246,58 +244,24 @@ bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
handleHTTPClient
|
HTTP request function (not found is handled by server)
|
||||||
*/
|
*/
|
||||||
void handleHTTPClient(WiFiClient& client) {
|
void handleHTTPRequest() {
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("New client");
|
Serial.println("HTTP Request");
|
||||||
|
|
||||||
// Wait for data from client to become available
|
String s = "<!DOCTYPE HTML>\r\n<html><h3><head>Hello from ";
|
||||||
while (client.connected() && !client.available()) {
|
s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "</h3></head>";
|
||||||
delay(1);
|
s += "<br/><h4>Local HTTP services are :</h4><ol>";
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
s += strHTTPServices;
|
||||||
// done :-)
|
// done :-)
|
||||||
s += "</html>\r\n\r\n";
|
s += "</ol></html>";
|
||||||
Serial.println("Sending 200");
|
Serial.println("Sending 200");
|
||||||
} else {
|
server.send(200, "text/html", s);
|
||||||
s = "HTTP/1.1 404 Not Found\r\n\r\n";
|
Serial.println("Done with request");
|
||||||
Serial.println("Sending 404");
|
|
||||||
}
|
|
||||||
client.print(s);
|
|
||||||
|
|
||||||
Serial.println("Done with client");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
setup
|
setup
|
||||||
*/
|
*/
|
||||||
@ -321,6 +285,9 @@ void setup(void) {
|
|||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Setup HTTP server
|
||||||
|
server.on("/", handleHTTPRequest);
|
||||||
|
|
||||||
// Setup MDNS responder
|
// Setup MDNS responder
|
||||||
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
|
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
|
||||||
// Init the (currently empty) host domain string with 'esp8266'
|
// Init the (currently empty) host domain string with 'esp8266'
|
||||||
@ -333,9 +300,9 @@ void setup(void) {
|
|||||||
}
|
}
|
||||||
Serial.println("MDNS responder started");
|
Serial.println("MDNS responder started");
|
||||||
|
|
||||||
// Start TCP (HTTP) server
|
// Start HTTP server
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("TCP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -343,14 +310,8 @@ void setup(void) {
|
|||||||
loop
|
loop
|
||||||
*/
|
*/
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
// Check if a client has connected
|
// Check if a request has come in
|
||||||
WiFiClient client = server.available();
|
server.handleClient();
|
||||||
if (client) {
|
|
||||||
handleHTTPClient(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow MDNS processing
|
// Allow MDNS processing
|
||||||
MDNS.update();
|
MDNS.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user