mirror of
https://github.com/esphome/esphome.git
synced 2025-09-03 20:02:22 +01:00
Add Captive Portal (#624)
* WIP: Captive Portal * Updates * Updates * Lint * Fixes
This commit is contained in:
@@ -18,6 +18,10 @@
|
||||
#include "esphome/core/util.h"
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
#ifdef USE_CAPTIVE_PORTAL
|
||||
#include "esphome/components/captive_portal/captive_portal.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace wifi {
|
||||
|
||||
@@ -28,6 +32,8 @@ float WiFiComponent::get_setup_priority() const { return setup_priority::WIFI; }
|
||||
void WiFiComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up WiFi...");
|
||||
|
||||
this->last_connected_ = millis();
|
||||
|
||||
this->wifi_register_callbacks_();
|
||||
|
||||
bool ret = this->wifi_mode_(this->has_sta(), false);
|
||||
@@ -99,6 +105,16 @@ void WiFiComponent::loop() {
|
||||
break;
|
||||
}
|
||||
|
||||
if (this->has_ap() && !this->ap_setup_) {
|
||||
if (now - this->last_connected_ > this->ap_timeout_) {
|
||||
ESP_LOGI(TAG, "Starting fallback AP!");
|
||||
this->setup_ap_config_();
|
||||
#ifdef USE_CAPTIVE_PORTAL
|
||||
captive_portal::global_captive_portal->start();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->has_ap() && this->reboot_timeout_ != 0) {
|
||||
if (now - this->last_connected_ > this->reboot_timeout_) {
|
||||
ESP_LOGE(TAG, "Can't connect to WiFi, rebooting...");
|
||||
@@ -119,7 +135,7 @@ IPAddress WiFiComponent::get_ip_address() {
|
||||
if (this->has_sta())
|
||||
return this->wifi_sta_ip_();
|
||||
if (this->has_ap())
|
||||
return this->wifi_soft_ap_ip_();
|
||||
return this->wifi_soft_ap_ip();
|
||||
return {};
|
||||
}
|
||||
std::string WiFiComponent::get_use_address() const {
|
||||
@@ -147,7 +163,7 @@ void WiFiComponent::setup_ap_config_() {
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
if (!this->has_sta()) {
|
||||
this->state_ = WIFI_COMPONENT_STATE_AP;
|
||||
@@ -159,6 +175,10 @@ float WiFiComponent::get_loop_priority() const {
|
||||
}
|
||||
void WiFiComponent::set_ap(const WiFiAP &ap) { this->ap_ = ap; }
|
||||
void WiFiComponent::add_sta(const WiFiAP &ap) { this->sta_.push_back(ap); }
|
||||
void WiFiComponent::set_sta(const WiFiAP &ap) {
|
||||
this->sta_.clear();
|
||||
this->add_sta(ap);
|
||||
}
|
||||
|
||||
void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
|
||||
ESP_LOGI(TAG, "WiFi Connecting to '%s'...", ap.get_ssid().c_str());
|
||||
@@ -377,10 +397,15 @@ void WiFiComponent::check_connecting_finished() {
|
||||
wl_status_t status = this->wifi_sta_status_();
|
||||
|
||||
if (status == WL_CONNECTED) {
|
||||
ESP_LOGI(TAG, "WiFi connected!");
|
||||
ESP_LOGI(TAG, "WiFi Connected!");
|
||||
this->print_connect_params_();
|
||||
|
||||
if (this->has_ap()) {
|
||||
#ifdef USE_CAPTIVE_PORTAL
|
||||
if (this->is_captive_portal_active_()) {
|
||||
captive_portal::global_captive_portal->end();
|
||||
}
|
||||
#endif
|
||||
ESP_LOGD(TAG, "Disabling AP...");
|
||||
this->wifi_mode_({}, false);
|
||||
}
|
||||
@@ -426,7 +451,7 @@ void WiFiComponent::check_connecting_finished() {
|
||||
|
||||
void WiFiComponent::retry_connect() {
|
||||
delay(10);
|
||||
if (this->num_retried_ > 5 || this->error_from_callback_) {
|
||||
if (!this->is_captive_portal_active_() && (this->num_retried_ > 5 || this->error_from_callback_)) {
|
||||
// If retry failed for more than 5 times, let's restart STA
|
||||
ESP_LOGW(TAG, "Restarting WiFi adapter...");
|
||||
this->wifi_mode_(false, {});
|
||||
@@ -443,9 +468,6 @@ void WiFiComponent::retry_connect() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->has_ap()) {
|
||||
this->setup_ap_config_();
|
||||
}
|
||||
this->state_ = WIFI_COMPONENT_STATE_COOLDOWN;
|
||||
this->action_started_ = millis();
|
||||
}
|
||||
@@ -461,11 +483,6 @@ bool WiFiComponent::is_connected() {
|
||||
return this->state_ == WIFI_COMPONENT_STATE_STA_CONNECTED && this->wifi_sta_status_() == WL_CONNECTED &&
|
||||
!this->error_from_callback_;
|
||||
}
|
||||
bool WiFiComponent::ready_for_ota() {
|
||||
if (this->has_ap())
|
||||
return true;
|
||||
return this->is_connected();
|
||||
}
|
||||
void WiFiComponent::set_power_save_mode(WiFiPowerSaveMode power_save) { this->power_save_ = power_save; }
|
||||
|
||||
std::string WiFiComponent::format_mac_addr(const uint8_t *mac) {
|
||||
@@ -473,20 +490,12 @@ std::string WiFiComponent::format_mac_addr(const uint8_t *mac) {
|
||||
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
bool sta_field_equal(const uint8_t *field_a, const uint8_t *field_b, int len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
uint8_t a = field_a[i];
|
||||
uint8_t b = field_b[i];
|
||||
if (a == b && a == 0)
|
||||
break;
|
||||
if (a == b)
|
||||
continue;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
bool WiFiComponent::is_captive_portal_active_() {
|
||||
#ifdef USE_CAPTIVE_PORTAL
|
||||
return captive_portal::global_captive_portal != nullptr && captive_portal::global_captive_portal->is_active();
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; }
|
||||
|
Reference in New Issue
Block a user