Skip to content

Commit 65456b6

Browse files
Check previous mDNS before registering new hostname.
1 parent 291bc6b commit 65456b6

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

libraries/ArduinoOTA/ArduinoOTA.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ void ArduinoOTAClass::begin() {
127127
if(!_udp_ota->listen(*IP_ADDR_ANY, _port))
128128
return;
129129
_udp_ota->onRx(std::bind(&ArduinoOTAClass::_onRx, this));
130-
MDNS.begin(_hostname.c_str());
130+
if(!MDNS.started()) {
131+
MDNS.begin(_hostname.c_str());
131132

132-
if (_password.length()) {
133-
MDNS.enableArduino(_port, true);
134-
} else {
135-
MDNS.enableArduino(_port);
133+
if (_password.length()) {
134+
MDNS.enableArduino(_port, true);
135+
} else {
136+
MDNS.enableArduino(_port);
137+
}
136138
}
137139
_initialized = true;
138140
_state = OTA_IDLE;
@@ -253,7 +255,7 @@ void ArduinoOTAClass::_runUpdate() {
253255
if (_error_callback) {
254256
_error_callback(OTA_BEGIN_ERROR);
255257
}
256-
258+
257259
StreamString ss;
258260
Update.printError(ss);
259261
_udp_ota->append("ERR: ", 5);

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ bool MDNSResponder::begin(const char* hostname){
171171
_restart();
172172
});
173173

174+
_started = true;
175+
174176
return _listen();
175177
}
176178

libraries/ESP8266mDNS/ESP8266mDNS.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ class MDNSResponder {
7979
void addService(String service, String proto, uint16_t port){
8080
addService(service.c_str(), proto.c_str(), port);
8181
}
82-
82+
8383
bool addServiceTxt(char *name, char *proto, char * key, char * value);
8484
void addServiceTxt(const char *name, const char *proto, const char *key,const char * value){
8585
addServiceTxt((char *)name, (char *)proto, (char *)key, (char *)value);
8686
}
8787
void addServiceTxt(String name, String proto, String key, String value){
8888
addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str());
8989
}
90-
90+
9191
int queryService(char *service, char *proto);
9292
int queryService(const char *service, const char *proto){
9393
return queryService((char *)service, (char *)proto);
@@ -98,7 +98,7 @@ class MDNSResponder {
9898
String hostname(int idx);
9999
IPAddress IP(int idx);
100100
uint16_t port(int idx);
101-
101+
102102
void enableArduino(uint16_t port, bool auth=false);
103103

104104
void setInstanceName(String name);
@@ -109,6 +109,10 @@ class MDNSResponder {
109109
setInstanceName(String(name));
110110
}
111111

112+
bool started() {
113+
return _started;
114+
}
115+
112116
private:
113117
struct MDNSService * _services;
114118
UdpContext* _conn;
@@ -120,7 +124,8 @@ class MDNSResponder {
120124
bool _waitingForAnswers;
121125
WiFiEventHandler _disconnectedHandler;
122126
WiFiEventHandler _gotIPHandler;
123-
127+
bool _started = false;
128+
124129

125130
uint16_t _getServicePort(char *service, char *proto);
126131
MDNSTxt * _getServiceTxt(char *name, char *proto);

0 commit comments

Comments
 (0)