Skip to content

Commit dbca6b6

Browse files
authored
Merge pull request #55 from tronbyt/update_config_portal
Update config portal html and saving operations. Only save non-blank values to nvs, auto fill url field from nvs during re-config.
2 parents 2b86ebe + 236dbde commit dbca6b6

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/main.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,30 @@ void app_main(void) {
228228
ESP_LOGE(TAG, "Failed to create AP shutdown timer");
229229
}
230230

231-
// Get the image URL from WiFi manager
232-
const char* image_url = wifi_get_image_url();
233-
const char* url_to_use = (image_url != NULL && strlen(image_url) > 0) ? image_url : DEFAULT_URL;
234-
231+
const char *image_url = NULL;
232+
233+
while (true) {
234+
image_url = wifi_get_image_url();
235+
236+
if (image_url != NULL && strlen(image_url) > 0 ) {
237+
// It's not blank now
238+
break;
239+
}
240+
241+
ESP_LOGW(TAG, "Image URL is not set. Waiting for configuration...");
242+
vTaskDelay(pdMS_TO_TICKS(5000));
243+
}
244+
245+
// image_url is now valid and usable here
246+
ESP_LOGI(TAG, "Proceeding with image URL: %s", image_url);
247+
235248
// Check for ws:// or wss:// in the URL
236-
if (strncmp(url_to_use, "ws://", 5) == 0 || strncmp(url_to_use, "wss://", 6) == 0) {
237-
ESP_LOGI(TAG, "Using websockets with URL: %s", url_to_use);
249+
if (strncmp(image_url, "ws://", 5) == 0 || strncmp(image_url, "wss://", 6) == 0) {
250+
ESP_LOGI(TAG, "Using websockets with URL: %s", image_url);
238251
use_websocket = true;
239252
// setup ws event handlers
240253
const esp_websocket_client_config_t ws_cfg = {
241-
.uri = url_to_use,
254+
.uri = image_url,
242255
.task_stack = 8192,
243256
.buffer_size = 10000,
244257
.crt_bundle_attach = esp_crt_bundle_attach,
@@ -264,14 +277,14 @@ void app_main(void) {
264277
}
265278
} else {
266279
// normal http
267-
ESP_LOGW(TAG, "HTTP Loop Start with URL: %s", url_to_use);
280+
ESP_LOGW(TAG, "HTTP Loop Start with URL: %s", image_url);
268281
for (;;) {
269282
uint8_t *webp;
270283
size_t len;
271284
static uint8_t brightness_pct = DISPLAY_DEFAULT_BRIGHTNESS;
272285

273-
ESP_LOGI(TAG, "Fetching from URL: %s", url_to_use);
274-
if (!wifi_is_connected() || remote_get(url_to_use, &webp, &len,
286+
ESP_LOGI(TAG, "Fetching from URL: %s", image_url);
287+
if (!wifi_is_connected() || remote_get(image_url, &webp, &len,
275288
&brightness_pct, &app_dwell_secs)) {
276289
ESP_LOGE(TAG, "No WiFi or Failed to get webp");
277290
vTaskDelay(pdMS_TO_TICKS(1 * 5000));

src/wifi.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void (*s_connect_callback)(void) = NULL;
6565
static void (*s_disconnect_callback)(void) = NULL;
6666

6767
// HTML for the configuration page
68-
static const char *s_html_page =
68+
const char *s_html_page_template =
6969
"<!DOCTYPE html>"
7070
"<html>"
7171
"<head>"
@@ -92,15 +92,15 @@ static const char *s_html_page =
9292
"enctype='application/x-www-form-urlencoded'>"
9393
"<div class='form-group'>"
9494
"<label for='ssid'>WiFi Network Name:</label>"
95-
"<input type='text' id='ssid' name='ssid' maxlength='32' required>"
95+
"<input type='text' id='ssid' name='ssid' maxlength='32'>"
9696
"</div>"
9797
"<div class='form-group'>"
9898
"<label for='password'>WiFi Password:</label>"
9999
"<input type='text' id='password' name='password' maxlength='64'>"
100100
"</div>"
101101
"<div class='form-group'>"
102102
"<label for='image_url'>Image URL:</label>"
103-
"<input type='text' id='image_url' name='image_url' maxlength='256'>"
103+
"<input type='text' id='image_url' name='image_url' maxlength='128' value='%s'>"
104104
"( If modifying Image URL reboot Tronbyt after saving. )"
105105
"</div>"
106106
"<button type='submit'>Save and Connect</button>"
@@ -109,6 +109,8 @@ static const char *s_html_page =
109109
"</body>"
110110
"</html>";
111111

112+
static char s_html_page[4096]; // Make sure this is large enough
113+
112114
// Success page HTML
113115
static const char *s_success_html = "<!DOCTYPE html>"
114116
"<html>"
@@ -206,8 +208,7 @@ int wifi_initialize(const char *ssid, const char *password) {
206208

207209
// Force the compiler to include these strings in the binary
208210
char placeholder_ssid[MAX_SSID_LEN + 1] = WIFI_SSID; // PATCH:SSID
209-
char placeholder_password[MAX_PASSWORD_LEN + 1] =
210-
WIFI_PASSWORD; // PATCH:PASS
211+
char placeholder_password[MAX_PASSWORD_LEN + 1] = WIFI_PASSWORD; // PATCH:PASS
211212
char placeholder_url[MAX_URL_LEN + 1] = REMOTE_URL;
212213

213214
ESP_LOGI(TAG, "Hardcoded WIFI_SSID: %s", placeholder_ssid);
@@ -462,25 +463,32 @@ static esp_err_t save_wifi_config_to_nvs(void) {
462463
return err;
463464
}
464465

465-
err = nvs_set_str(nvs_handle, NVS_KEY_SSID, s_wifi_ssid);
466-
if (err != ESP_OK) {
466+
if (s_wifi_ssid[0] != '\0') {
467+
err = nvs_set_str(nvs_handle, NVS_KEY_SSID, s_wifi_ssid);
468+
if (err != ESP_OK) {
467469
ESP_LOGE(TAG, "Error saving SSID to NVS: %s", esp_err_to_name(err));
468470
nvs_close(nvs_handle);
469471
return err;
472+
}
470473
}
471474

472-
err = nvs_set_str(nvs_handle, NVS_KEY_PASSWORD, s_wifi_password);
473-
if (err != ESP_OK) {
475+
if (s_wifi_password[0] != '\0') {
476+
err = nvs_set_str(nvs_handle, NVS_KEY_PASSWORD, s_wifi_password);
477+
if (err != ESP_OK) {
474478
ESP_LOGE(TAG, "Error saving password to NVS: %s", esp_err_to_name(err));
475479
nvs_close(nvs_handle);
476480
return err;
481+
}
477482
}
478483

479-
err = nvs_set_str(nvs_handle, NVS_KEY_IMAGE_URL, s_image_url);
480-
if (err != ESP_OK) {
481-
ESP_LOGE(TAG, "Error saving image URL to NVS: %s", esp_err_to_name(err));
484+
if (s_image_url[0] != '\0') {
485+
err = nvs_set_str(nvs_handle, NVS_KEY_IMAGE_URL, s_image_url);
486+
if (err != ESP_OK) {
487+
ESP_LOGE(TAG, "Error saving image URL to NVS: %s",
488+
esp_err_to_name(err));
482489
nvs_close(nvs_handle);
483490
return err;
491+
}
484492
}
485493

486494
err = nvs_commit(nvs_handle);
@@ -597,6 +605,8 @@ static esp_err_t stop_webserver(void) {
597605

598606
// Root page handler
599607
static esp_err_t root_handler(httpd_req_t *req) {
608+
ESP_LOGI(TAG, "Injecting image url (%s) to html template", s_image_url);
609+
snprintf(s_html_page, sizeof(s_html_page), s_html_page_template, s_image_url);
600610
ESP_LOGI(TAG, "Serving root page");
601611
httpd_resp_set_type(req, "text/html");
602612
httpd_resp_send(req, s_html_page, strlen(s_html_page));
@@ -681,8 +691,11 @@ static esp_err_t save_handler(httpd_req_t *req) {
681691
// Save the new configuration
682692
strncpy(s_wifi_ssid, ssid, MAX_SSID_LEN);
683693
strncpy(s_wifi_password, password, MAX_PASSWORD_LEN);
684-
strncpy(s_image_url, image_url, MAX_URL_LEN);
685-
694+
if (strlen(image_url) < 6) {
695+
ESP_LOGI(TAG, "new image url is blank, leaving as is");
696+
} else {
697+
strncpy(s_image_url, image_url, MAX_URL_LEN);
698+
}
686699
// Free the buffer as we don't need it anymore
687700
free(buf);
688701

0 commit comments

Comments
 (0)