Skip to content

Commit e09c939

Browse files
committed
Clean code and add example
1 parent 0426e00 commit e09c939

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,36 +325,38 @@ int MDNSResponder::queryService(char *service, char *proto) {
325325
}
326326

327327
String MDNSResponder::hostname(int idx) {
328-
if (_answers == 0) {
328+
MDNSAnswer *answer = _getAnswerFromIdx(idx);
329+
if (answer == 0) {
329330
return String();
330331
}
331-
MDNSAnswer *answer = _answers;
332-
while (answer != 0 && idx-- > 0) {
333-
answer = answer->next;
334-
}
335332
return answer->hostname;
336333
}
337334

338335
IPAddress MDNSResponder::IP(int idx) {
339-
if (_answers == 0) {
336+
MDNSAnswer *answer = _getAnswerFromIdx(idx);
337+
if (answer == 0) {
340338
return IPAddress();
341339
}
342-
MDNSAnswer *answer = _answers;
343-
while (answer != 0 && idx-- > 0) {
344-
answer = answer->next;
345-
}
346340
return IPAddress(answer->ip);
347341
}
348342

349343
uint16_t MDNSResponder::port(int idx) {
350-
if (_answers == 0) {
344+
MDNSAnswer *answer = _getAnswerFromIdx(idx);
345+
if (answer == 0) {
351346
return 0;
352347
}
348+
return answer->port;
349+
}
350+
351+
MDNSAnswer* MDNSResponder::_getAnswerFromIdx(int idx) {
353352
MDNSAnswer *answer = _answers;
354353
while (answer != 0 && idx-- > 0) {
355354
answer = answer->next;
356355
}
357-
return answer->port;
356+
if (idx > 0) {
357+
return 0;
358+
}
359+
return answer;
358360
}
359361

360362
MDNSTxt * MDNSResponder::_getServiceTxt(char *name, char *proto){
@@ -577,8 +579,6 @@ void MDNSResponder::_parsePacket(){
577579
}
578580
}
579581

580-
Serial.printf("Parts collected: %02x\n", partsCollected);
581-
582582
if ((partsCollected == 0x0F) && serviceMatch) {
583583
#ifdef MDNS_DEBUG_RX
584584
Serial.println("All answers parsed, adding to _answers list..");

libraries/ESP8266mDNS/ESP8266mDNS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class MDNSResponder {
123123
void _parsePacket();
124124
void _reply(uint8_t replyMask, char * service, char *proto, uint16_t port);
125125
size_t advertiseServices(); // advertise all hosted services
126+
MDNSAnswer* _getAnswerFromIdx(int idx);
126127
};
127128

128129
extern MDNSResponder MDNS;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
ESP8266 mDNS-SD responder and query sample
3+
4+
This is an example of announcing and finding services.
5+
6+
Instructions:
7+
- Update WiFi SSID and password as necessary.
8+
- Flash the sketch to two ESP8266 boards
9+
- The last one powered on should now find the other.
10+
*/
11+
12+
#include <ESP8266WiFi.h>
13+
#include <ESP8266mDNS.h>
14+
15+
const char* ssid = "...";
16+
const char* password = "...";
17+
char hostString[16] = {0};
18+
19+
void setup() {
20+
Serial.begin(115200);
21+
delay(100);
22+
Serial.println("\r\nsetup()");
23+
24+
sprintf(hostString, "ESP_%06X", ESP.getChipId());
25+
Serial.print("Hostname: ");
26+
Serial.println(hostString);
27+
WiFi.hostname(hostString);
28+
29+
WiFi.begin(ssid, password);
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(250);
32+
Serial.print(".");
33+
}
34+
Serial.println("");
35+
Serial.print("Connected to ");
36+
Serial.println(ssid);
37+
Serial.print("IP address: ");
38+
Serial.println(WiFi.localIP());
39+
40+
if (!MDNS.begin(hostString)) {
41+
Serial.println("Error setting up MDNS responder!");
42+
}
43+
Serial.println("mDNS responder started");
44+
MDNS.addService("esp", "tcp", 8080); // Announce esp tcp service on port 8080
45+
46+
Serial.println("Sending mDNS query");
47+
int n = MDNS.queryService("esp", "tcp"); // Send out query for esp tcp services
48+
Serial.println("mDNS query done");
49+
if (n == 0) {
50+
Serial.println("no services found");
51+
}
52+
else {
53+
Serial.print(n);
54+
Serial.println(" service(s) found");
55+
for (int i = 0; i < n; ++i) {
56+
// Print details for each service found
57+
Serial.print(i + 1);
58+
Serial.print(": ");
59+
Serial.print(MDNS.hostname(i));
60+
Serial.print(" (");
61+
Serial.print(MDNS.IP(i));
62+
Serial.print(":");
63+
Serial.print(MDNS.port(i));
64+
Serial.println(")");
65+
}
66+
}
67+
Serial.println();
68+
69+
Serial.println("loop() next");
70+
}
71+
72+
void loop() {
73+
// put your main code here, to run repeatedly:
74+
75+
}

0 commit comments

Comments
 (0)