Skip to content

Commit 0d8451e

Browse files
OttoWintersilverchris
authored andcommitted
Captive portal fixes (esphome#766)
* Enable MDNS logs comment * Work around ESP8266 mDNS broken for AP See also esp8266/Arduino#6114 * Enable captive_portal in AP-only mode Fixes esphome/issues#671 * Make ESP32 connecting faster See also espressif/arduino-esp32#2989 * Format
1 parent bd073c2 commit 0d8451e

File tree

5 files changed

+73
-29
lines changed

5 files changed

+73
-29
lines changed

esphome/components/logger/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def to_code(config):
123123
'TLS_MEM',
124124
'UPDATER',
125125
'WIFI',
126+
# Spams logs too much:
127+
# 'MDNS_RESPONDER',
126128
}
127129
for comp in DEBUG_COMPONENTS:
128130
cg.add_build_flag("-DDEBUG_ESP_{}".format(comp))

esphome/components/wifi/wifi_component.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ void WiFiComponent::setup() {
4949
}
5050
} else if (this->has_ap()) {
5151
this->setup_ap_config_();
52+
#ifdef USE_CAPTIVE_PORTAL
53+
if (captive_portal::global_captive_portal != nullptr)
54+
captive_portal::global_captive_portal->start();
55+
#endif
5256
}
5357

5458
this->wifi_apply_hostname_();
59+
#ifdef ARDUINO_ARCH_ESP32
5560
network_setup_mdns();
61+
#endif
5662
}
5763

5864
void WiFiComponent::loop() {
@@ -103,7 +109,8 @@ void WiFiComponent::loop() {
103109
ESP_LOGI(TAG, "Starting fallback AP!");
104110
this->setup_ap_config_();
105111
#ifdef USE_CAPTIVE_PORTAL
106-
captive_portal::global_captive_portal->start();
112+
if (captive_portal::global_captive_portal != nullptr)
113+
captive_portal::global_captive_portal->start();
107114
#endif
108115
}
109116
}
@@ -157,6 +164,9 @@ void WiFiComponent::setup_ap_config_() {
157164

158165
this->ap_setup_ = this->wifi_start_ap_(this->ap_);
159166
ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().toString().c_str());
167+
#ifdef ARDUINO_ARCH_ESP8266
168+
network_setup_mdns(this->wifi_soft_ap_ip(), 1);
169+
#endif
160170

161171
if (!this->has_sta()) {
162172
this->state_ = WIFI_COMPONENT_STATE_AP;
@@ -416,6 +426,9 @@ void WiFiComponent::check_connecting_finished() {
416426
ESP_LOGD(TAG, "Disabling AP...");
417427
this->wifi_mode_({}, false);
418428
}
429+
#ifdef ARDUINO_ARCH_ESP8266
430+
network_setup_mdns(this->wifi_sta_ip_(), 0);
431+
#endif
419432
this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED;
420433
this->num_retried_ = 0;
421434
return;

esphome/components/wifi/wifi_component_esp32.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,16 @@ bool WiFiComponent::wifi_sta_connect_(WiFiAP ap) {
162162
conf.sta.channel = *ap.get_channel();
163163
}
164164

165-
esp_err_t err = esp_wifi_disconnect();
166-
if (err != ESP_OK) {
167-
ESP_LOGV(TAG, "esp_wifi_disconnect failed! %d", err);
168-
return false;
165+
wifi_config_t current_conf;
166+
esp_err_t err;
167+
esp_wifi_get_config(WIFI_IF_STA, &current_conf);
168+
169+
if (memcmp(&current_conf, &conf, sizeof(wifi_config_t)) != 0) {
170+
err = esp_wifi_disconnect();
171+
if (err != ESP_OK) {
172+
ESP_LOGV(TAG, "esp_wifi_disconnect failed! %d", err);
173+
return false;
174+
}
169175
}
170176

171177
err = esp_wifi_set_config(WIFI_IF_STA, &conf);

esphome/core/util.cpp

+41-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "esphome/core/defines.h"
33
#include "esphome/core/application.h"
44
#include "esphome/core/version.h"
5+
#include "esphome/core/log.h"
56

67
#ifdef USE_WIFI
78
#include "esphome/components/wifi/wifi_component.h"
@@ -38,40 +39,56 @@ bool network_is_connected() {
3839
return false;
3940
}
4041

41-
void network_setup_mdns() {
42-
MDNS.begin(App.get_name().c_str());
42+
#ifdef ARDUINO_ARCH_ESP8266
43+
bool mdns_setup;
44+
#endif
45+
46+
#ifdef ARDUINO_ARCH_ESP8266
47+
void network_setup_mdns(IPAddress address, int interface) {
48+
// Latest arduino framework breaks mDNS for AP interface
49+
// see https://github.com/esp8266/Arduino/issues/6114
50+
if (interface == 1)
51+
return;
52+
MDNS.begin(App.get_name().c_str(), address);
53+
mdns_setup = true;
54+
#endif
55+
#ifdef ARDUINO_ARCH_ESP32
56+
void network_setup_mdns() {
57+
MDNS.begin(App.get_name().c_str());
58+
#endif
4359
#ifdef USE_API
44-
if (api::global_api_server != nullptr) {
45-
MDNS.addService("esphomelib", "tcp", api::global_api_server->get_port());
46-
// DNS-SD (!=mDNS !) requires at least one TXT record for service discovery - let's add version
47-
MDNS.addServiceTxt("esphomelib", "tcp", "version", ESPHOME_VERSION);
48-
MDNS.addServiceTxt("esphomelib", "tcp", "address", network_get_address().c_str());
49-
} else {
60+
if (api::global_api_server != nullptr) {
61+
MDNS.addService("esphomelib", "tcp", api::global_api_server->get_port());
62+
// DNS-SD (!=mDNS !) requires at least one TXT record for service discovery - let's add version
63+
MDNS.addServiceTxt("esphomelib", "tcp", "version", ESPHOME_VERSION);
64+
MDNS.addServiceTxt("esphomelib", "tcp", "address", network_get_address().c_str());
65+
} else {
5066
#endif
51-
// Publish "http" service if not using native API.
52-
// This is just to have *some* mDNS service so that .local resolution works
53-
MDNS.addService("http", "tcp", 80);
54-
MDNS.addServiceTxt("http", "tcp", "version", ESPHOME_VERSION);
67+
// Publish "http" service if not using native API.
68+
// This is just to have *some* mDNS service so that .local resolution works
69+
MDNS.addService("http", "tcp", 80);
70+
MDNS.addServiceTxt("http", "tcp", "version", ESPHOME_VERSION);
5571
#ifdef USE_API
56-
}
72+
}
5773
#endif
58-
}
59-
void network_tick_mdns() {
74+
}
75+
void network_tick_mdns() {
6076
#ifdef ARDUINO_ARCH_ESP8266
61-
MDNS.update();
77+
if (mdns_setup)
78+
MDNS.update();
6279
#endif
63-
}
80+
}
6481

65-
std::string network_get_address() {
82+
std::string network_get_address() {
6683
#ifdef USE_ETHERNET
67-
if (ethernet::global_eth_component != nullptr)
68-
return ethernet::global_eth_component->get_use_address();
84+
if (ethernet::global_eth_component != nullptr)
85+
return ethernet::global_eth_component->get_use_address();
6986
#endif
7087
#ifdef USE_WIFI
71-
if (wifi::global_wifi_component != nullptr)
72-
return wifi::global_wifi_component->get_use_address();
88+
if (wifi::global_wifi_component != nullptr)
89+
return wifi::global_wifi_component->get_use_address();
7390
#endif
74-
return "";
75-
}
91+
return "";
92+
}
7693

7794
} // namespace esphome

esphome/core/util.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string>
4+
#include "IPAddress.h"
45

56
namespace esphome {
67

@@ -10,7 +11,12 @@ bool network_is_connected();
1011
std::string network_get_address();
1112

1213
/// Manually set up the network stack (outside of the App.setup() loop, for example in OTA safe mode)
14+
#ifdef ARDUINO_ARCH_ESP8266
15+
void network_setup_mdns(IPAddress address, int interface);
16+
#endif
17+
#ifdef ARDUINO_ARCH_ESP32
1318
void network_setup_mdns();
19+
#endif
1420
void network_tick_mdns();
1521

1622
} // namespace esphome

0 commit comments

Comments
 (0)