mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[esp32_improv] Fix state not transitioning to PROVISIONED when WiFi configured via captive portal (#11181)
This commit is contained in:
		
				
					committed by
					
						 Jesse Hills
						Jesse Hills
					
				
			
			
				
	
			
			
			
						parent
						
							e4bc465a3d
						
					
				
				
					commit
					742c9cbb53
				
			| @@ -143,6 +143,7 @@ void ESP32ImprovComponent::loop() { | |||||||
| #else | #else | ||||||
|       this->set_state_(improv::STATE_AUTHORIZED); |       this->set_state_(improv::STATE_AUTHORIZED); | ||||||
| #endif | #endif | ||||||
|  |       this->check_wifi_connection_(); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case improv::STATE_AUTHORIZED: { |     case improv::STATE_AUTHORIZED: { | ||||||
| @@ -156,31 +157,12 @@ void ESP32ImprovComponent::loop() { | |||||||
|       if (!this->check_identify_()) { |       if (!this->check_identify_()) { | ||||||
|         this->set_status_indicator_state_((now % 1000) < 500); |         this->set_status_indicator_state_((now % 1000) < 500); | ||||||
|       } |       } | ||||||
|  |       this->check_wifi_connection_(); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case improv::STATE_PROVISIONING: { |     case improv::STATE_PROVISIONING: { | ||||||
|       this->set_status_indicator_state_((now % 200) < 100); |       this->set_status_indicator_state_((now % 200) < 100); | ||||||
|       if (wifi::global_wifi_component->is_connected()) { |       this->check_wifi_connection_(); | ||||||
|         wifi::global_wifi_component->save_wifi_sta(this->connecting_sta_.get_ssid(), |  | ||||||
|                                                    this->connecting_sta_.get_password()); |  | ||||||
|         this->connecting_sta_ = {}; |  | ||||||
|         this->cancel_timeout("wifi-connect-timeout"); |  | ||||||
|         this->set_state_(improv::STATE_PROVISIONED); |  | ||||||
|  |  | ||||||
|         std::vector<std::string> urls = {ESPHOME_MY_LINK}; |  | ||||||
| #ifdef USE_WEBSERVER |  | ||||||
|         for (auto &ip : wifi::global_wifi_component->wifi_sta_ip_addresses()) { |  | ||||||
|           if (ip.is_ip4()) { |  | ||||||
|             std::string webserver_url = "http://" + ip.str() + ":" + to_string(USE_WEBSERVER_PORT); |  | ||||||
|             urls.push_back(webserver_url); |  | ||||||
|             break; |  | ||||||
|           } |  | ||||||
|         } |  | ||||||
| #endif |  | ||||||
|         std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls); |  | ||||||
|         this->send_response_(data); |  | ||||||
|         this->stop(); |  | ||||||
|       } |  | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case improv::STATE_PROVISIONED: { |     case improv::STATE_PROVISIONED: { | ||||||
| @@ -392,6 +374,36 @@ void ESP32ImprovComponent::on_wifi_connect_timeout_() { | |||||||
|   wifi::global_wifi_component->clear_sta(); |   wifi::global_wifi_component->clear_sta(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void ESP32ImprovComponent::check_wifi_connection_() { | ||||||
|  |   if (!wifi::global_wifi_component->is_connected()) { | ||||||
|  |     return; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (this->state_ == improv::STATE_PROVISIONING) { | ||||||
|  |     wifi::global_wifi_component->save_wifi_sta(this->connecting_sta_.get_ssid(), this->connecting_sta_.get_password()); | ||||||
|  |     this->connecting_sta_ = {}; | ||||||
|  |     this->cancel_timeout("wifi-connect-timeout"); | ||||||
|  |  | ||||||
|  |     std::vector<std::string> urls = {ESPHOME_MY_LINK}; | ||||||
|  | #ifdef USE_WEBSERVER | ||||||
|  |     for (auto &ip : wifi::global_wifi_component->wifi_sta_ip_addresses()) { | ||||||
|  |       if (ip.is_ip4()) { | ||||||
|  |         std::string webserver_url = "http://" + ip.str() + ":" + to_string(USE_WEBSERVER_PORT); | ||||||
|  |         urls.push_back(webserver_url); | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  |     std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls); | ||||||
|  |     this->send_response_(data); | ||||||
|  |   } else if (this->is_active() && this->state_ != improv::STATE_PROVISIONED) { | ||||||
|  |     ESP_LOGD(TAG, "WiFi provisioned externally"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   this->set_state_(improv::STATE_PROVISIONED); | ||||||
|  |   this->stop(); | ||||||
|  | } | ||||||
|  |  | ||||||
| void ESP32ImprovComponent::advertise_service_data_() { | void ESP32ImprovComponent::advertise_service_data_() { | ||||||
|   uint8_t service_data[IMPROV_SERVICE_DATA_SIZE] = {}; |   uint8_t service_data[IMPROV_SERVICE_DATA_SIZE] = {}; | ||||||
|   service_data[0] = IMPROV_PROTOCOL_ID_1;  // PR |   service_data[0] = IMPROV_PROTOCOL_ID_1;  // PR | ||||||
|   | |||||||
| @@ -111,6 +111,7 @@ class ESP32ImprovComponent : public Component { | |||||||
|   void send_response_(std::vector<uint8_t> &response); |   void send_response_(std::vector<uint8_t> &response); | ||||||
|   void process_incoming_data_(); |   void process_incoming_data_(); | ||||||
|   void on_wifi_connect_timeout_(); |   void on_wifi_connect_timeout_(); | ||||||
|  |   void check_wifi_connection_(); | ||||||
|   bool check_identify_(); |   bool check_identify_(); | ||||||
|   void advertise_service_data_(); |   void advertise_service_data_(); | ||||||
| #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG | #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user