-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wifi configuration can be changed via BLE
- Loading branch information
Showing
8 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
components/esp32_ble_controller/wifi_configuration_handler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "wifi_configuration_handler.h" | ||
|
||
#include "esphome/core/application.h" | ||
#include "esphome/core/log.h" | ||
#include "esphome/components/wifi/wifi_component.h" | ||
|
||
namespace esphome { | ||
namespace esp32_ble_controller { | ||
|
||
static const char *TAG = "wifi_configuration_handler"; | ||
|
||
void WifiSettingsHandler::setup() { | ||
// Hash with compilation time | ||
// This ensures the AP override is not applied for OTA | ||
uint32_t hash = fnv1_hash("wifi_settings#" + App.get_compilation_time()); | ||
wifi_settings_preference = global_preferences.make_preference<WifiSettings>(hash, true); | ||
|
||
WifiSettings settings; | ||
if (wifi_settings_preference.load(&settings)) { | ||
ESP_LOGI(TAG, "Overriding WIFI settings with stored preferences"); | ||
override_sta(settings); | ||
} | ||
} | ||
|
||
void WifiSettingsHandler::set_credentials(const std::string &ssid, const std::string &password, bool hidden_network) { | ||
ESP_LOGI(TAG, "Updating WIFI settings"); | ||
|
||
WifiSettings settings; | ||
|
||
strncpy(settings.ssid, ssid.c_str(), WIFI_SSID_LEN); | ||
strncpy(settings.password, password.c_str(), WIFI_PASSWORD_LEN); | ||
settings.hidden_network = hidden_network; | ||
|
||
if (!wifi_settings_preference.save(&settings)) { | ||
ESP_LOGE(TAG, "Could not save new WIFI settings"); | ||
} | ||
|
||
override_sta(settings); | ||
} | ||
|
||
void WifiSettingsHandler::override_sta(const WifiSettings& settings) { | ||
wifi::WiFiAP sta; | ||
|
||
sta.set_ssid(settings.ssid); | ||
sta.set_password(settings.password); | ||
sta.set_hidden(settings.hidden_network); | ||
|
||
wifi::global_wifi_component->set_sta(sta); | ||
} | ||
|
||
} // namespace esp32_ble_controller | ||
} // namespace esphome |
34 changes: 34 additions & 0 deletions
34
components/esp32_ble_controller/wifi_configuration_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "esphome/core/defines.h" | ||
#include "esphome/core/preferences.h" | ||
|
||
namespace esphome { | ||
namespace esp32_ble_controller { | ||
|
||
#define WIFI_SSID_LEN 33 | ||
#define WIFI_PASSWORD_LEN 65 | ||
|
||
struct WifiSettings { | ||
char ssid[WIFI_SSID_LEN]; | ||
char password[WIFI_PASSWORD_LEN]; | ||
bool hidden_network; | ||
} PACKED; // NOLINT | ||
|
||
class WifiSettingsHandler { | ||
public: | ||
void setup(); | ||
|
||
void set_credentials(const std::string& ssid, const std::string& password, bool hidden_network); | ||
|
||
private: | ||
void override_sta(const WifiSettings& settings); | ||
|
||
private: | ||
ESPPreferenceObject wifi_settings_preference; | ||
}; | ||
|
||
} // namespace esp32_ble_controller | ||
} // namespace esphome |