Skip to content

Commit 82789d2

Browse files
hreintkedevyte
authored andcommitted
Functional update, host and service probes (#5653)
* Functional update, host and service probes * Fix ServiceMonitor.ino warnings * Adding MDNSServiceQueryCallback functional ServiceMonitor.ino needs some updates for web page but works on Serial output. * DynamicServices Functional * Fix ServiceMonitor to match latest MDNSServiceInfo * Fix unused variable in LEAdns.h * mDNS_Clock.ino fix * example restyle * Add keyValues and answerInfo * Waring and formattin fix * Change struct MDNSServiceInfo { MDNSServiceInfo(MDNSResponder ... to struct MDNSServiceInfo { MDNSServiceInfo(MDNSResponder& * Make AnswerType user friendly * Update ServiceInfo example * Code cleanup * AnswerType update, Astyle update servicemonitor * Update clock example to webserver * Second typedef for probe callbacks Change String -> const char* at multiple locations * Optimizations * Update callbacks to void * esp32 compatibility * std::map to const char* * Fix emplace_back call * Change Dynamic callback to void(...) * Add WiFi events reset() in close()
1 parent 848fbf5 commit 82789d2

File tree

7 files changed

+409
-366
lines changed

7 files changed

+409
-366
lines changed

libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino

+49-99
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <ESP8266WiFi.h>
3636
#include <WiFiClient.h>
37+
#include <ESP8266WebServer.h>
3738
#include <time.h>
3839

3940
/*
@@ -72,9 +73,8 @@ char* pcHostDomain = 0; // Negociated
7273
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
7374
MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder
7475

75-
// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
76-
WiFiServer server(SERVICE_PORT);
77-
76+
// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests
77+
ESP8266WebServer server(SERVICE_PORT);
7878

7979
/*
8080
getTimeString
@@ -135,18 +135,13 @@ bool setStationHostname(const char* p_pcHostname) {
135135
This can be triggered by calling MDNS.announce().
136136
137137
*/
138-
bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder,
139-
const MDNSResponder::hMDNSService p_hService,
140-
void* p_pUserdata) {
138+
void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
141139
Serial.println("MDNSDynamicServiceTxtCallback");
142-
(void) p_pUserdata;
143140

144-
if ((p_pMDNSResponder) &&
145-
(hMDNSService == p_hService)) {
141+
if (hMDNSService == p_hService) {
146142
Serial.printf("Updating curtime TXT item to: %s\n", getTimeString());
147-
p_pMDNSResponder->addDynamicServiceTxt(p_hService, "curtime", getTimeString());
143+
MDNS.addDynamicServiceTxt(p_hService, "curtime", getTimeString());
148144
}
149-
return true;
150145
}
151146

152147

@@ -160,105 +155,66 @@ bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder,
160155
restarted via p_pMDNSResponder->setHostname().
161156
162157
*/
163-
bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
164-
const char* p_pcDomainName,
165-
const MDNSResponder::hMDNSService p_hService,
166-
bool p_bProbeResult,
167-
void* p_pUserdata) {
158+
void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
159+
168160
Serial.println("MDNSProbeResultCallback");
169-
(void) p_pUserdata;
170-
171-
if ((p_pMDNSResponder) &&
172-
(0 == p_hService)) { // Called for host domain
173-
Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName, (p_bProbeResult ? "free" : "already USED!"));
174-
if (true == p_bProbeResult) {
175-
// Set station hostname
176-
setStationHostname(pcHostDomain);
177-
178-
if (!bHostDomainConfirmed) {
179-
// Hostname free -> setup clock service
180-
bHostDomainConfirmed = true;
181-
182-
if (!hMDNSService) {
183-
// Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
184-
hMDNSService = p_pMDNSResponder->addService(0, "espclk", "tcp", SERVICE_PORT);
185-
if (hMDNSService) {
186-
// Add a simple static MDNS service TXT item
187-
p_pMDNSResponder->addServiceTxt(hMDNSService, "port#", SERVICE_PORT);
188-
// Set the callback function for dynamic service TXTs
189-
p_pMDNSResponder->setDynamicServiceTxtCallback(hMDNSService, MDNSDynamicServiceTxtCallback, 0);
190-
}
161+
Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName.c_str(), (p_bProbeResult ? "free" : "already USED!"));
162+
if (true == p_bProbeResult) {
163+
// Set station hostname
164+
setStationHostname(pcHostDomain);
165+
166+
if (!bHostDomainConfirmed) {
167+
// Hostname free -> setup clock service
168+
bHostDomainConfirmed = true;
169+
170+
if (!hMDNSService) {
171+
// Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
172+
hMDNSService = MDNS.addService(0, "espclk", "tcp", SERVICE_PORT);
173+
if (hMDNSService) {
174+
// Add a simple static MDNS service TXT item
175+
MDNS.addServiceTxt(hMDNSService, "port#", SERVICE_PORT);
176+
// Set the callback function for dynamic service TXTs
177+
MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
191178
}
192179
}
180+
}
181+
} else {
182+
// Change hostname, use '-' as divider between base name and index
183+
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
184+
MDNS.setHostname(pcHostDomain);
193185
} else {
194-
// Change hostname, use '-' as divider between base name and index
195-
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
196-
p_pMDNSResponder->setHostname(pcHostDomain);
197-
} else {
198-
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
199-
}
186+
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
200187
}
201188
}
202-
return true;
203189
}
204190

205191

206192
/*
207193
handleHTTPClient
208194
*/
209-
void handleHTTPClient(WiFiClient& client) {
210-
Serial.println("");
211-
Serial.println("New client");
212-
213-
// Wait for data from client to become available
214-
while (client.connected() && !client.available()) {
215-
delay(1);
216-
}
217195

218-
// Read the first line of HTTP request
219-
String req = client.readStringUntil('\r');
220-
221-
// First line of HTTP request looks like "GET /path HTTP/1.1"
222-
// Retrieve the "/path" part by finding the spaces
223-
int addr_start = req.indexOf(' ');
224-
int addr_end = req.indexOf(' ', addr_start + 1);
225-
if (addr_start == -1 || addr_end == -1) {
226-
Serial.print("Invalid request: ");
227-
Serial.println(req);
228-
return;
229-
}
230-
req = req.substring(addr_start + 1, addr_end);
231-
Serial.print("Request: ");
232-
Serial.println(req);
233-
client.flush();
196+
void handleHTTPRequest() {
197+
Serial.println("");
198+
Serial.println("HTTP Request");
234199

235200
// Get current time
236201
time_t now = time(nullptr);;
237202
struct tm timeinfo;
238203
gmtime_r(&now, &timeinfo);
239204

240205
String s;
241-
if (req == "/") {
242-
IPAddress ip = WiFi.localIP();
243-
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
244-
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ";
245-
s += WiFi.hostname() + " at " + ipStr;
246-
// Simple addition of the current time
247-
s += "\r\nCurrent time is: ";
248-
s += getTimeString();
249-
// done :-)
250-
s += "</html>\r\n\r\n";
251-
Serial.println("Sending 200");
252-
} else {
253-
s = "HTTP/1.1 404 Not Found\r\n\r\n";
254-
Serial.println("Sending 404");
255-
}
256-
client.print(s);
257206

258-
Serial.println("Done with client");
207+
s = "<!DOCTYPE HTML>\r\n<html>Hello from ";
208+
s += WiFi.hostname() + " at " + WiFi.localIP().toString();
209+
// Simple addition of the current time
210+
s += "\r\nCurrent time is: ";
211+
s += getTimeString();
212+
// done :-)
213+
s += "</html>\r\n\r\n";
214+
Serial.println("Sending 200");
215+
server.send(200, "text/html", s);
259216
}
260217

261-
262218
/*
263219
setup
264220
*/
@@ -285,7 +241,7 @@ void setup(void) {
285241
setClock();
286242

287243
// Setup MDNS responder
288-
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
244+
MDNS.setHostProbeResultCallback(hostProbeResult);
289245
// Init the (currently empty) host domain string with 'esp8266'
290246
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
291247
(!MDNS.begin(pcHostDomain))) {
@@ -296,26 +252,22 @@ void setup(void) {
296252
}
297253
Serial.println("MDNS responder started");
298254

299-
// Start TCP (HTTP) server
255+
// Setup HTTP server
256+
server.on("/", handleHTTPRequest);
300257
server.begin();
301-
Serial.println("TCP server started");
258+
Serial.println("HTTP server started");
302259
}
303260

304-
305261
/*
306262
loop
307263
*/
308264
void loop(void) {
309-
// Check if a client has connected
310-
WiFiClient client = server.available();
311-
if (client) {
312-
handleHTTPClient(client);
313-
}
314265

266+
// Check if a request has come in
267+
server.handleClient();
315268
// Allow MDNS processing
316269
MDNS.update();
317270

318-
// Update time (if needed)
319271
static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE);
320272
if (timeout.expired()) {
321273

@@ -326,5 +278,3 @@ void loop(void) {
326278
}
327279
}
328280
}
329-
330-

0 commit comments

Comments
 (0)