@@ -65,7 +65,7 @@ static void (*s_connect_callback)(void) = NULL;
65
65
static void (* s_disconnect_callback )(void ) = NULL ;
66
66
67
67
// HTML for the configuration page
68
- static const char * s_html_page =
68
+ const char * s_html_page_template =
69
69
"<!DOCTYPE html>"
70
70
"<html>"
71
71
"<head>"
@@ -92,15 +92,15 @@ static const char *s_html_page =
92
92
"enctype='application/x-www-form-urlencoded'>"
93
93
"<div class='form-group'>"
94
94
"<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'>"
96
96
"</div>"
97
97
"<div class='form-group'>"
98
98
"<label for='password'>WiFi Password:</label>"
99
99
"<input type='text' id='password' name='password' maxlength='64'>"
100
100
"</div>"
101
101
"<div class='form-group'>"
102
102
"<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 '>"
104
104
"( If modifying Image URL reboot Tronbyt after saving. )"
105
105
"</div>"
106
106
"<button type='submit'>Save and Connect</button>"
@@ -109,6 +109,8 @@ static const char *s_html_page =
109
109
"</body>"
110
110
"</html>" ;
111
111
112
+ static char s_html_page [4096 ]; // Make sure this is large enough
113
+
112
114
// Success page HTML
113
115
static const char * s_success_html = "<!DOCTYPE html>"
114
116
"<html>"
@@ -206,8 +208,7 @@ int wifi_initialize(const char *ssid, const char *password) {
206
208
207
209
// Force the compiler to include these strings in the binary
208
210
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
211
212
char placeholder_url [MAX_URL_LEN + 1 ] = REMOTE_URL ;
212
213
213
214
ESP_LOGI (TAG , "Hardcoded WIFI_SSID: %s" , placeholder_ssid );
@@ -462,25 +463,32 @@ static esp_err_t save_wifi_config_to_nvs(void) {
462
463
return err ;
463
464
}
464
465
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 ) {
467
469
ESP_LOGE (TAG , "Error saving SSID to NVS: %s" , esp_err_to_name (err ));
468
470
nvs_close (nvs_handle );
469
471
return err ;
472
+ }
470
473
}
471
474
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 ) {
474
478
ESP_LOGE (TAG , "Error saving password to NVS: %s" , esp_err_to_name (err ));
475
479
nvs_close (nvs_handle );
476
480
return err ;
481
+ }
477
482
}
478
483
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 ));
482
489
nvs_close (nvs_handle );
483
490
return err ;
491
+ }
484
492
}
485
493
486
494
err = nvs_commit (nvs_handle );
@@ -597,6 +605,8 @@ static esp_err_t stop_webserver(void) {
597
605
598
606
// Root page handler
599
607
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 );
600
610
ESP_LOGI (TAG , "Serving root page" );
601
611
httpd_resp_set_type (req , "text/html" );
602
612
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) {
681
691
// Save the new configuration
682
692
strncpy (s_wifi_ssid , ssid , MAX_SSID_LEN );
683
693
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
+ }
686
699
// Free the buffer as we don't need it anymore
687
700
free (buf );
688
701
0 commit comments