mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Captive portal fixes (#766)
* Enable MDNS logs comment * Work around ESP8266 mDNS broken for AP See also https://github.com/esp8266/Arduino/issues/6114 * Enable captive_portal in AP-only mode Fixes https://github.com/esphome/issues/issues/671 * Make ESP32 connecting faster See also https://github.com/espressif/arduino-esp32/pull/2989 * Format
This commit is contained in:
		| @@ -123,6 +123,8 @@ def to_code(config): | |||||||
|             'TLS_MEM', |             'TLS_MEM', | ||||||
|             'UPDATER', |             'UPDATER', | ||||||
|             'WIFI', |             'WIFI', | ||||||
|  |             # Spams logs too much: | ||||||
|  |             # 'MDNS_RESPONDER', | ||||||
|         } |         } | ||||||
|         for comp in DEBUG_COMPONENTS: |         for comp in DEBUG_COMPONENTS: | ||||||
|             cg.add_build_flag("-DDEBUG_ESP_{}".format(comp)) |             cg.add_build_flag("-DDEBUG_ESP_{}".format(comp)) | ||||||
|   | |||||||
| @@ -49,10 +49,16 @@ void WiFiComponent::setup() { | |||||||
|     } |     } | ||||||
|   } else if (this->has_ap()) { |   } else if (this->has_ap()) { | ||||||
|     this->setup_ap_config_(); |     this->setup_ap_config_(); | ||||||
|  | #ifdef USE_CAPTIVE_PORTAL | ||||||
|  |     if (captive_portal::global_captive_portal != nullptr) | ||||||
|  |       captive_portal::global_captive_portal->start(); | ||||||
|  | #endif | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   this->wifi_apply_hostname_(); |   this->wifi_apply_hostname_(); | ||||||
|  | #ifdef ARDUINO_ARCH_ESP32 | ||||||
|   network_setup_mdns(); |   network_setup_mdns(); | ||||||
|  | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| void WiFiComponent::loop() { | void WiFiComponent::loop() { | ||||||
| @@ -103,7 +109,8 @@ void WiFiComponent::loop() { | |||||||
|         ESP_LOGI(TAG, "Starting fallback AP!"); |         ESP_LOGI(TAG, "Starting fallback AP!"); | ||||||
|         this->setup_ap_config_(); |         this->setup_ap_config_(); | ||||||
| #ifdef USE_CAPTIVE_PORTAL | #ifdef USE_CAPTIVE_PORTAL | ||||||
|         captive_portal::global_captive_portal->start(); |         if (captive_portal::global_captive_portal != nullptr) | ||||||
|  |           captive_portal::global_captive_portal->start(); | ||||||
| #endif | #endif | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
| @@ -157,6 +164,9 @@ void WiFiComponent::setup_ap_config_() { | |||||||
|  |  | ||||||
|   this->ap_setup_ = this->wifi_start_ap_(this->ap_); |   this->ap_setup_ = this->wifi_start_ap_(this->ap_); | ||||||
|   ESP_LOGCONFIG(TAG, "  IP Address: %s", this->wifi_soft_ap_ip().toString().c_str()); |   ESP_LOGCONFIG(TAG, "  IP Address: %s", this->wifi_soft_ap_ip().toString().c_str()); | ||||||
|  | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|  |   network_setup_mdns(this->wifi_soft_ap_ip(), 1); | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   if (!this->has_sta()) { |   if (!this->has_sta()) { | ||||||
|     this->state_ = WIFI_COMPONENT_STATE_AP; |     this->state_ = WIFI_COMPONENT_STATE_AP; | ||||||
| @@ -416,6 +426,9 @@ void WiFiComponent::check_connecting_finished() { | |||||||
|       ESP_LOGD(TAG, "Disabling AP..."); |       ESP_LOGD(TAG, "Disabling AP..."); | ||||||
|       this->wifi_mode_({}, false); |       this->wifi_mode_({}, false); | ||||||
|     } |     } | ||||||
|  | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|  |     network_setup_mdns(this->wifi_sta_ip_(), 0); | ||||||
|  | #endif | ||||||
|     this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED; |     this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED; | ||||||
|     this->num_retried_ = 0; |     this->num_retried_ = 0; | ||||||
|     return; |     return; | ||||||
|   | |||||||
| @@ -162,10 +162,16 @@ bool WiFiComponent::wifi_sta_connect_(WiFiAP ap) { | |||||||
|     conf.sta.channel = *ap.get_channel(); |     conf.sta.channel = *ap.get_channel(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   esp_err_t err = esp_wifi_disconnect(); |   wifi_config_t current_conf; | ||||||
|   if (err != ESP_OK) { |   esp_err_t err; | ||||||
|     ESP_LOGV(TAG, "esp_wifi_disconnect failed! %d", err); |   esp_wifi_get_config(WIFI_IF_STA, ¤t_conf); | ||||||
|     return false; |  | ||||||
|  |   if (memcmp(¤t_conf, &conf, sizeof(wifi_config_t)) != 0) { | ||||||
|  |     err = esp_wifi_disconnect(); | ||||||
|  |     if (err != ESP_OK) { | ||||||
|  |       ESP_LOGV(TAG, "esp_wifi_disconnect failed! %d", err); | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   err = esp_wifi_set_config(WIFI_IF_STA, &conf); |   err = esp_wifi_set_config(WIFI_IF_STA, &conf); | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
| #include "esphome/core/defines.h" | #include "esphome/core/defines.h" | ||||||
| #include "esphome/core/application.h" | #include "esphome/core/application.h" | ||||||
| #include "esphome/core/version.h" | #include "esphome/core/version.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
| #ifdef USE_WIFI | #ifdef USE_WIFI | ||||||
| #include "esphome/components/wifi/wifi_component.h" | #include "esphome/components/wifi/wifi_component.h" | ||||||
| @@ -38,40 +39,56 @@ bool network_is_connected() { | |||||||
|   return false; |   return false; | ||||||
| } | } | ||||||
|  |  | ||||||
| void network_setup_mdns() { |  | ||||||
|   MDNS.begin(App.get_name().c_str()); |  | ||||||
| #ifdef USE_API |  | ||||||
|   if (api::global_api_server != nullptr) { |  | ||||||
|     MDNS.addService("esphomelib", "tcp", api::global_api_server->get_port()); |  | ||||||
|     // DNS-SD (!=mDNS !) requires at least one TXT record for service discovery - let's add version |  | ||||||
|     MDNS.addServiceTxt("esphomelib", "tcp", "version", ESPHOME_VERSION); |  | ||||||
|     MDNS.addServiceTxt("esphomelib", "tcp", "address", network_get_address().c_str()); |  | ||||||
|   } else { |  | ||||||
| #endif |  | ||||||
|     // Publish "http" service if not using native API. |  | ||||||
|     // This is just to have *some* mDNS service so that .local resolution works |  | ||||||
|     MDNS.addService("http", "tcp", 80); |  | ||||||
|     MDNS.addServiceTxt("http", "tcp", "version", ESPHOME_VERSION); |  | ||||||
| #ifdef USE_API |  | ||||||
|   } |  | ||||||
| #endif |  | ||||||
| } |  | ||||||
| void network_tick_mdns() { |  | ||||||
| #ifdef ARDUINO_ARCH_ESP8266 | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|   MDNS.update(); | bool mdns_setup; | ||||||
| #endif | #endif | ||||||
| } |  | ||||||
|  |  | ||||||
| std::string network_get_address() { | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|  | void network_setup_mdns(IPAddress address, int interface) { | ||||||
|  |   // Latest arduino framework breaks mDNS for AP interface | ||||||
|  |   // see https://github.com/esp8266/Arduino/issues/6114 | ||||||
|  |   if (interface == 1) | ||||||
|  |     return; | ||||||
|  |   MDNS.begin(App.get_name().c_str(), address); | ||||||
|  |   mdns_setup = true; | ||||||
|  | #endif | ||||||
|  | #ifdef ARDUINO_ARCH_ESP32 | ||||||
|  |   void network_setup_mdns() { | ||||||
|  |     MDNS.begin(App.get_name().c_str()); | ||||||
|  | #endif | ||||||
|  | #ifdef USE_API | ||||||
|  |     if (api::global_api_server != nullptr) { | ||||||
|  |       MDNS.addService("esphomelib", "tcp", api::global_api_server->get_port()); | ||||||
|  |       // DNS-SD (!=mDNS !) requires at least one TXT record for service discovery - let's add version | ||||||
|  |       MDNS.addServiceTxt("esphomelib", "tcp", "version", ESPHOME_VERSION); | ||||||
|  |       MDNS.addServiceTxt("esphomelib", "tcp", "address", network_get_address().c_str()); | ||||||
|  |     } else { | ||||||
|  | #endif | ||||||
|  |       // Publish "http" service if not using native API. | ||||||
|  |       // This is just to have *some* mDNS service so that .local resolution works | ||||||
|  |       MDNS.addService("http", "tcp", 80); | ||||||
|  |       MDNS.addServiceTxt("http", "tcp", "version", ESPHOME_VERSION); | ||||||
|  | #ifdef USE_API | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  |   } | ||||||
|  |   void network_tick_mdns() { | ||||||
|  | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|  |     if (mdns_setup) | ||||||
|  |       MDNS.update(); | ||||||
|  | #endif | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   std::string network_get_address() { | ||||||
| #ifdef USE_ETHERNET | #ifdef USE_ETHERNET | ||||||
|   if (ethernet::global_eth_component != nullptr) |     if (ethernet::global_eth_component != nullptr) | ||||||
|     return ethernet::global_eth_component->get_use_address(); |       return ethernet::global_eth_component->get_use_address(); | ||||||
| #endif | #endif | ||||||
| #ifdef USE_WIFI | #ifdef USE_WIFI | ||||||
|   if (wifi::global_wifi_component != nullptr) |     if (wifi::global_wifi_component != nullptr) | ||||||
|     return wifi::global_wifi_component->get_use_address(); |       return wifi::global_wifi_component->get_use_address(); | ||||||
| #endif | #endif | ||||||
|   return ""; |     return ""; | ||||||
| } |   } | ||||||
|  |  | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include <string> | #include <string> | ||||||
|  | #include "IPAddress.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
|  |  | ||||||
| @@ -10,7 +11,12 @@ bool network_is_connected(); | |||||||
| std::string network_get_address(); | std::string network_get_address(); | ||||||
|  |  | ||||||
| /// Manually set up the network stack (outside of the App.setup() loop, for example in OTA safe mode) | /// Manually set up the network stack (outside of the App.setup() loop, for example in OTA safe mode) | ||||||
|  | #ifdef ARDUINO_ARCH_ESP8266 | ||||||
|  | void network_setup_mdns(IPAddress address, int interface); | ||||||
|  | #endif | ||||||
|  | #ifdef ARDUINO_ARCH_ESP32 | ||||||
| void network_setup_mdns(); | void network_setup_mdns(); | ||||||
|  | #endif | ||||||
| void network_tick_mdns(); | void network_tick_mdns(); | ||||||
|  |  | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user