8000 ESP8266mDNS using the provided IP in the begin method (#2349) · esp8266/Arduino@b682d59 · GitHub
[go: up one dir, main page]

Skip to content

Commit b682d59

Browse files
haiduc32igrr
authored andcommitted
ESP8266mDNS using the provided IP in the begin method (#2349)
this fix forces the mDNS to use the provided IP in the begin method instead of the auto detected IP. this is required if the ESP8266 starts in the AP_STA mode and activates only the AP initially.
1 parent c4c207a commit b682d59

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,39 @@ MDNSResponder::~MDNSResponder() {
142142
_answers = 0;
143143
}
144144

145-
bool MDNSResponder::begin(const char* hostname){
146-
size_t n = strlen(hostname);
145+
bool MDNSResponder::begin(const char* hostName){
146+
return _begin(hostName, 0, 120);
147+
}
148+
149+
bool MDNSResponder::begin(const char* hostName, IPAddress ip, uint32_t ttl){
150+
return _begin(hostName, ip, ttl);
151+
}
152+
153+
bool MDNSResponder::_begin(const char *hostName, uint32_t ip, uint32_t ttl){
154+
size_t n = strlen(hostName);
147155
if (n > 63) { // max size for a single label.
148156
return false;
149157
}
150158

159+
_ip = ip;
160+
151161
// Copy in hostname characters as lowercase
152-
_hostName = hostname;
162+
_hostName = hostName;
153163
_hostName.toLowerCase();
154164

155165
// If instance name is not already set copy hostname to instance name
156-
if (_instanceName.equals("") ) _instanceName=hostname;
166+
if (_instanceName.equals("") ) _instanceName=hostName;
157167

158-
_gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){
159-
_restart();
160-
});
168+
//only if the IP hasn't been set manually, use the events
169+
if (ip == 0) {
170+
_gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){
171+
_restart();
172+
});
161173

162-
_disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) {
163-
_restart();
164-
});
174+
_disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) {
175+
_restart();
176+
});
177+
}
165178

166179
return _listen();
167180
}
@@ -442,7 +455,11 @@ uint16_t MDNSResponder::_getServicePort(char *name, char *proto){
442455

443456
uint32_t MDNSResponder::_getOurIp(){
444457
int mode = wifi_get_opmode();
445-
if(mode & STATION_MODE){
458+
459+
//if has a manually set IP use this
460+
if(_ip){
461+
return _ip;
462+
} else if(mode & STATION_MODE){
446463
struct ip_info staIpInfo;
447464
wifi_get_ip_info(STATION_IF, &staIpInfo);
448465
return staIpInfo.ip.addr;

libraries/ESP8266mDNS/ESP8266mDNS.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ class MDNSResponder {
6363
~MDNSResponder();
6464
bool begin(const char* hostName);
6565
//for compatibility
66-
bool begin(const char* hostName, IPAddress ip, uint32_t ttl=120){
67-
return begin(hostName);
68-
}
66+
bool begin(const char* hostName, IPAddress ip, uint32_t ttl=120);
6967
void update();
7068

7169
void addService(char *service, char *proto, uint16_t port);
@@ -116,8 +114,9 @@ class MDNSResponder {
116114
bool _waitingForAnswers;
117115
WiFiEventHandler _disconnectedHandler;
118116
WiFiEventHandler _gotIPHandler;
119-
117+
uint32_t _ip;
120118

119+
bool _begin(const char* hostName, uint32_t ip, uint32_t ttl);
121120
uint32_t _getOurIp();
122121
uint16_t _getServicePort(char *service, char *proto);
123122
MDNSTxt * _getServiceTxt(char *name, char *proto);

0 commit comments

Comments
 (0)
0