mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Clean code and add example
This commit is contained in:
parent
0426e000cf
commit
e09c939c42
@ -325,36 +325,38 @@ int MDNSResponder::queryService(char *service, char *proto) {
|
||||
}
|
||||
|
||||
String MDNSResponder::hostname(int idx) {
|
||||
if (_answers == 0) {
|
||||
MDNSAnswer *answer = _getAnswerFromIdx(idx);
|
||||
if (answer == 0) {
|
||||
return String();
|
||||
}
|
||||
MDNSAnswer *answer = _answers;
|
||||
while (answer != 0 && idx-- > 0) {
|
||||
answer = answer->next;
|
||||
}
|
||||
return answer->hostname;
|
||||
}
|
||||
|
||||
IPAddress MDNSResponder::IP(int idx) {
|
||||
if (_answers == 0) {
|
||||
MDNSAnswer *answer = _getAnswerFromIdx(idx);
|
||||
if (answer == 0) {
|
||||
return IPAddress();
|
||||
}
|
||||
MDNSAnswer *answer = _answers;
|
||||
while (answer != 0 && idx-- > 0) {
|
||||
answer = answer->next;
|
||||
}
|
||||
return IPAddress(answer->ip);
|
||||
}
|
||||
|
||||
uint16_t MDNSResponder::port(int idx) {
|
||||
if (_answers == 0) {
|
||||
MDNSAnswer *answer = _getAnswerFromIdx(idx);
|
||||
if (answer == 0) {
|
||||
return 0;
|
||||
}
|
||||
return answer->port;
|
||||
}
|
||||
|
||||
MDNSAnswer* MDNSResponder::_getAnswerFromIdx(int idx) {
|
||||
MDNSAnswer *answer = _answers;
|
||||
while (answer != 0 && idx-- > 0) {
|
||||
answer = answer->next;
|
||||
}
|
||||
return answer->port;
|
||||
if (idx > 0) {
|
||||
return 0;
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
MDNSTxt * MDNSResponder::_getServiceTxt(char *name, char *proto){
|
||||
@ -577,8 +579,6 @@ void MDNSResponder::_parsePacket(){
|
||||
}
|
||||
}
|
||||
|
||||
Serial.printf("Parts collected: %02x\n", partsCollected);
|
||||
|
||||
if ((partsCollected == 0x0F) && serviceMatch) {
|
||||
#ifdef MDNS_DEBUG_RX
|
||||
Serial.println("All answers parsed, adding to _answers list..");
|
||||
|
@ -123,6 +123,7 @@ private:
|
||||
void _parsePacket();
|
||||
void _reply(uint8_t replyMask, char * service, char *proto, uint16_t port);
|
||||
size_t advertiseServices(); // advertise all hosted services
|
||||
MDNSAnswer* _getAnswerFromIdx(int idx);
|
||||
};
|
||||
|
||||
extern MDNSResponder MDNS;
|
||||
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
ESP8266 mDNS-SD responder and query sample
|
||||
|
||||
This is an example of announcing and finding services.
|
||||
|
||||
Instructions:
|
||||
- Update WiFi SSID and password as necessary.
|
||||
- Flash the sketch to two ESP8266 boards
|
||||
- The last one powered on should now find the other.
|
||||
*/
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
|
||||
const char* ssid = "...";
|
||||
const char* password = "...";
|
||||
char hostString[16] = {0};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
Serial.println("\r\nsetup()");
|
||||
|
||||
sprintf(hostString, "ESP_%06X", ESP.getChipId());
|
||||
Serial.print("Hostname: ");
|
||||
Serial.println(hostString);
|
||||
WiFi.hostname(hostString);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(250);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
if (!MDNS.begin(hostString)) {
|
||||
Serial.println("Error setting up MDNS responder!");
|
||||
}
|
||||
Serial.println("mDNS responder started");
|
||||
MDNS.addService("esp", "tcp", 8080); // Announce esp tcp service on port 8080
|
||||
|
||||
Serial.println("Sending mDNS query");
|
||||
int n = MDNS.queryService("esp", "tcp"); // Send out query for esp tcp services
|
||||
Serial.println("mDNS query done");
|
||||
if (n == 0) {
|
||||
Serial.println("no services found");
|
||||
}
|
||||
else {
|
||||
Serial.print(n);
|
||||
Serial.println(" service(s) found");
|
||||
for (int i = 0; i < n; ++i) {
|
||||
// Print details for each service found
|
||||
Serial.print(i + 1);
|
||||
Serial.print(": ");
|
||||
Serial.print(MDNS.hostname(i));
|
||||
Serial.print(" (");
|
||||
Serial.print(MDNS.IP(i));
|
||||
Serial.print(":");
|
||||
Serial.print(MDNS.port(i));
|
||||
Serial.println(")");
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("loop() next");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user