From bf91443f38d273f5fae4868e608a898dd33746cf Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 31 Jan 2022 11:08:20 +1300 Subject: [PATCH] Improv_serial scan and send wifi networks list (#3116) --- esphome/components/esp32_improv/__init__.py | 2 +- esphome/components/improv_serial/__init__.py | 2 +- .../improv_serial/improv_serial_component.cpp | 23 +++++++++++++++++++ .../improv_serial/improv_serial_component.h | 2 +- platformio.ini | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/esphome/components/esp32_improv/__init__.py b/esphome/components/esp32_improv/__init__.py index 26e586e4ff..c95d4075bc 100644 --- a/esphome/components/esp32_improv/__init__.py +++ b/esphome/components/esp32_improv/__init__.py @@ -56,7 +56,7 @@ async def to_code(config): cg.add(ble_server.register_service_component(var)) cg.add_define("USE_IMPROV") - cg.add_library("esphome/Improv", "1.1.0") + cg.add_library("esphome/Improv", "1.2.0") cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION])) cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION])) diff --git a/esphome/components/improv_serial/__init__.py b/esphome/components/improv_serial/__init__.py index 4499bef1fd..4de4fe66a7 100644 --- a/esphome/components/improv_serial/__init__.py +++ b/esphome/components/improv_serial/__init__.py @@ -30,4 +30,4 @@ FINAL_VALIDATE_SCHEMA = validate_logger_baud_rate async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) - cg.add_library("esphome/Improv", "1.1.0") + cg.add_library("esphome/Improv", "1.2.0") diff --git a/esphome/components/improv_serial/improv_serial_component.cpp b/esphome/components/improv_serial/improv_serial_component.cpp index b55855abf9..0dab71060c 100644 --- a/esphome/components/improv_serial/improv_serial_component.cpp +++ b/esphome/components/improv_serial/improv_serial_component.cpp @@ -24,6 +24,8 @@ void ImprovSerialComponent::setup() { if (wifi::global_wifi_component->has_sta()) { this->state_ = improv::STATE_PROVISIONED; + } else { + wifi::global_wifi_component->start_scanning(); } } @@ -152,6 +154,27 @@ bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command this->send_response_(info); return true; } + case improv::GET_WIFI_NETWORKS: { + std::vector networks; + auto results = wifi::global_wifi_component->get_scan_result(); + for (auto &scan : results) { + if (scan.get_is_hidden()) + continue; + const std::string &ssid = scan.get_ssid(); + if (std::find(networks.begin(), networks.end(), ssid) != networks.end()) + continue; + // Send each ssid separately to avoid overflowing the buffer + std::vector data = improv::build_rpc_response( + improv::GET_WIFI_NETWORKS, {ssid, str_sprintf("%d", scan.get_rssi()), YESNO(scan.get_with_auth())}, false); + this->send_response_(data); + networks.push_back(ssid); + } + // Send empty response to signify the end of the list. + std::vector data = + improv::build_rpc_response(improv::GET_WIFI_NETWORKS, std::vector{}, false); + this->send_response_(data); + return true; + } default: { ESP_LOGW(TAG, "Unknown Improv payload"); this->set_error_(improv::ERROR_UNKNOWN_RPC); diff --git a/esphome/components/improv_serial/improv_serial_component.h b/esphome/components/improv_serial/improv_serial_component.h index 304afdaf75..c6b980ab99 100644 --- a/esphome/components/improv_serial/improv_serial_component.h +++ b/esphome/components/improv_serial/improv_serial_component.h @@ -32,7 +32,7 @@ class ImprovSerialComponent : public Component { void loop() override; void dump_config() override; - float get_setup_priority() const override { return setup_priority::HARDWARE; } + float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } protected: bool parse_improv_serial_byte_(uint8_t byte); diff --git a/platformio.ini b/platformio.ini index e7bf848f1b..927d58cb89 100644 --- a/platformio.ini +++ b/platformio.ini @@ -35,7 +35,7 @@ build_flags = lib_deps = esphome/noise-c@0.1.4 ; api makuna/NeoPixelBus@2.6.9 ; neopixelbus - esphome/Improv@1.1.0 ; improv_serial / esp32_improv + esphome/Improv@1.2.0 ; improv_serial / esp32_improv bblanchon/ArduinoJson@6.18.5 ; json wjtje/qr-code-generator-library@1.7.0 ; qr_code build_flags =