diff --git a/components/wifi-manager/code.js b/components/wifi-manager/code.js index 158a33f8..0f438865 100644 --- a/components/wifi-manager/code.js +++ b/components/wifi-manager/code.js @@ -15,7 +15,6 @@ var apList = null; var selectedSSID = ""; var refreshAPInterval = null; var checkStatusInterval = null; -var checkConfigInterval = null; var StatusIntervalActive = false; var ConfigIntervalActive = false; @@ -29,13 +28,6 @@ function stopCheckStatusInterval(){ } StatusIntervalActive = false; } -function stopCheckConfigInterval(){ - if(checkConfigInterval != null){ - clearTimeout(checkConfigInterval); - checkConfigInterval = null; - } - ConfigIntervalActive=false; -} function stopRefreshAPInterval(){ @@ -51,10 +43,6 @@ function startCheckStatusInterval(){ StatusIntervalActive = true; checkStatusInterval = setTimeout(checkStatus, 950); } -function startCheckConfigInterval(){ - ConfigIntervalActive = true; - checkConfigInterval = setTimeout(checkConfig, 950); -} function startRefreshAPInterval(){ RefreshAPIIntervalActive = true; @@ -78,8 +66,6 @@ function RepeatRefreshAPInterval(){ } $(document).ready(function(){ - - $("#wifi-status").on("click", ".ape", function() { $( "#wifi" ).slideUp( "fast", function() {}); $( "#connect-details" ).slideDown( "fast", function() {}); @@ -138,19 +124,10 @@ $(document).ready(function(){ $( "#wifi" ).slideDown( "fast", function() {}); }); - $("#update").on("click", function() { - - performUpdate(); - }); - $("#factory").on("click", function() { - - performFactory(); - }); $("#ok-credits").on("click", function() { $( "#credits" ).slideUp( "fast", function() {}); $( "#app" ).slideDown( "fast", function() {}); - }); $("#acredits").on("click", function(event) { @@ -196,48 +173,18 @@ $(document).ready(function(){ $( "#wifi" ).slideDown( "fast", function() {}) }); - - - - - - - + $("#update-command").on("click", function() { + updateAutoexec(); + }); + //first time the page loads: attempt get the connection status and start the wifi scan refreshAP(); startCheckStatusInterval(); startRefreshAPInterval(); - startCheckConfigInterval(); - - - - + getConfig(); }); -function performUpdate(){ - autoexec1 = $("#autoexec1").val(); - //reset connection -// -// $( "#ok-connect" ).prop("disabled",true); -// $( "#ssid-wait" ).text(selectedSSID); -// $( "#connect" ).slideUp( "fast", function() {}); -// $( "#connect_manual" ).slideUp( "fast", function() {}); -// $( "#connect-wait" ).slideDown( "fast", function() {}); -// // todo: should we update the UI here? - - $.ajax({ - url: '/config.json', - dataType: 'json', - method: 'POST', - cache: false, - headers: { 'X-Custom-autoexec1': autoexec1 }, - data: { 'timestamp': Date.now()} - }); - - -} - function performFactory(){ // $( "#ok-connect" ).prop("disabled",true); @@ -258,7 +205,6 @@ function performFactory(){ } - function performConnect(conntype){ //stop the status refresh. This prevents a race condition where a status @@ -302,7 +248,6 @@ function performConnect(conntype){ //now we can re-set the intervals regardless of result startCheckStatusInterval(); startRefreshAPInterval(); - } @@ -322,7 +267,6 @@ function rssiToIcon(rssi){ } } - function refreshAP(){ $.getJSON( "/ap.json", function( data ) { if(data.length > 0){ @@ -337,7 +281,6 @@ function refreshAP(){ } }); RepeatRefreshAPInterval(); - } function refreshAPHTML(data){ @@ -350,9 +293,6 @@ function refreshAPHTML(data){ $( "#wifi-list" ).html(h) } - - - function checkStatus(){ $.getJSON( "/status.json", function( data ) { if(data.hasOwnProperty('autoexec1') && data['autoexec1'] != ""){ @@ -424,31 +364,46 @@ function checkStatus(){ RepeatCheckStatusInterval(); } - -function checkConfig(){ - var h = ""; - //{ "autoexec" : 0, "list" : [{ 'autoexec1' : 'squeezelite -o "I2S" -b 500:2000 -d all=info -M esp32' }]} - $.getJSON( "/config.json", function( data ) { - if(data.hasOwnProperty('autoexec')) { - h+= '
Autoexec: {0}
'.format(data["autoexec"]===1?"Active":"Inactive"); - } - if(data.hasOwnProperty('list')) { - data["list"].forEach(function(e, idx, array) { - for (const [key, value] of Object.entries(e)) { - h+= '
'.format(key,value); - } - } - - ); - h += "\n"; - $( "#command-list" ).html(h); - } - +function getConfig() { + $.getJSON("/config.json", function(data) { + if (data.hasOwnProperty('autoexec')) { + if (data["autoexec"] === 1) { + console.log('turn on autoexec'); + $("#autoexec-cb")[0].checked=true; + } else { + console.log('turn off autoexec'); + $("#autoexec-cb")[0].checked=false; + $("#autoexec-command").hide(200); + } + } + if (data.hasOwnProperty('list')) { + data.list.forEach(function(line) { + let key = Object.keys(line)[0]; + let val = Object.values(line)[0]; + console.log(key, val); + if (key == 'autoexec1') { + $("#autoexec1").val(val); + } + }); + } }) .fail(function() { - //don't do anything, the server might be down while esp32 recalibrates radio + console.log("failed to fetch config!"); }); - - RepeatCheckConfigInterval(); - } + +function updateAutoexec(){ + autoexec = ($("#autoexec-cb")[0].checked)?1:0; + autoexec1 = $("#autoexec1").val(); + + $.ajax({ + url: '/config.json', + dataType: 'json', + method: 'POST', + cache: false, + headers: { "X-Custom-autoexec": autoexec, "X-Custom-autoexec1": autoexec1 }, + data: { 'timestamp': Date.now() } + }); + console.log('sent config JSON with headers:', autoexec, autoexec1); +} + diff --git a/components/wifi-manager/http_server.c b/components/wifi-manager/http_server.c index 38fc5147..39e2f617 100644 --- a/components/wifi-manager/http_server.c +++ b/components/wifi-manager/http_server.c @@ -38,9 +38,9 @@ function to process requests, decode URLs, serve files, etc. etc. /* @brief tag used for ESP serial console messages */ static const char TAG[] = "http_server"; -static const char json_start[] = "{ \"autoexec\" : %u, \"list\" : ["; +static const char json_start[] = "{ \"autoexec\": %u, \"list\": ["; static const char json_end[] = "]}"; -static const char template[] = "{ '%s' : '%s' }"; +static const char template[] = "{ \"%s\": \"%s\" }"; static const char array_separator[]=","; /* @brief task handle for the http server */ @@ -222,7 +222,6 @@ void http_server_netconn_serve(struct netconn *conn) { ESP_LOGI(TAG,"Serving config.json"); char autoexec_name[21]={0}; char * autoexec_value=NULL; - char * autoexec_flag_s=NULL; uint8_t autoexec_flag=0; int buflen=MAX_COMMAND_LINE_SIZE+strlen(template)+1; char * buff = malloc(buflen); @@ -251,7 +250,7 @@ void http_server_netconn_serve(struct netconn *conn) { ESP_LOGD(TAG,"%s", array_separator); } ESP_LOGI(TAG,"found command %s = %s", autoexec_name, autoexec_value); - snprintf(buff,buflen-1,template, autoexec_name,autoexec_value); + snprintf(buff, buflen-1, template, autoexec_name, autoexec_value); netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY); ESP_LOGD(TAG,"%s", buff); ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name); @@ -277,7 +276,8 @@ void http_server_netconn_serve(struct netconn *conn) { if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){ int i=1; int lenS = 0, lenA=0; - char autoexec_name[21]={0}; + char autoexec_name[22]={0}; + char autoexec_key[12]={0}; char * autoexec_value=NULL; char * autoexec_flag_s=NULL; uint8_t autoexec_flag=0; @@ -289,14 +289,16 @@ void http_server_netconn_serve(struct netconn *conn) { } do { - snprintf(autoexec_name,sizeof(autoexec_name)-1,"X-Custom-autoexec%u:",i++); - ESP_LOGD(TAG,"Looking for command name %s", autoexec_name); + snprintf(autoexec_name,sizeof(autoexec_name)-1,"X-Custom-autoexec%u: ",i); + snprintf(autoexec_key,sizeof(autoexec_key)-1,"autoexec%u",i++); + ESP_LOGD(TAG,"Looking for command name %s.", autoexec_name); autoexec_value = http_server_get_header(save_ptr, autoexec_name, &lenS); + snprintf(autoexec_value, lenS+1, autoexec_value); if(autoexec_value ){ if(lenS < MAX_COMMAND_LINE_SIZE ){ - ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ call, with %s: %s", autoexec_name, autoexec_value); - wifi_manager_save_autoexec_config(autoexec_value,autoexec_name,lenS); + ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ call, with %s: %s, length %i", autoexec_key, autoexec_value, lenS); + wifi_manager_save_autoexec_config(autoexec_value,autoexec_key,lenS); } else { diff --git a/components/wifi-manager/index.html b/components/wifi-manager/index.html index 7c976d6f..3161c8fa 100644 --- a/components/wifi-manager/index.html +++ b/components/wifi-manager/index.html @@ -9,6 +9,7 @@ esp32-wifi-manager + +
+
+
+

Startup command

+
+

+
+ +
+

+ +
+

Command to run

+
+ +
+
+ +
+ +
+
+
+

Firmware upgrade

+
+ Upload Progress + + + + +
+
+

Wi-Fi

@@ -179,36 +222,11 @@ function heartbeat()
Powered by esp32-wifi-manager.
-
-
-

Startup command

-
-

Squeezelite

- -
-
- -
- -
- -
-
-
-

Application

-
- Upload Progress - - - - -
-

Enter Details

-

Manual Connection

+

Manual Connection

@@ -307,4 +325,4 @@ function heartbeat()
- \ No newline at end of file + diff --git a/components/wifi-manager/style.css b/components/wifi-manager/style.css index dfaf8692..ad9a8e51 100644 --- a/components/wifi-manager/style.css +++ b/components/wifi-manager/style.css @@ -13,7 +13,6 @@ a:hover { color: red; } input { - display: none; font: 1.1em tahoma, arial, sans-serif; } input:focus, @@ -77,6 +76,7 @@ p { header { background-color: #fff; border-bottom: 1px solid #888; + border-top: 1px solid #888; } section { background-color: #fff; @@ -247,4 +247,99 @@ h3 { -webkit-transform: scale(1.0); } } -/* end of SpinKit */ \ No newline at end of file +/* end of SpinKit */ + +.toggle label { + position: relative; + display: inline-block; + height: 3.5em; +} + +.toggle input { + display: none; +} + +.toggle .slider { + /* Grundfläche */ + + position: absolute; + cursor: pointer; + top: 1.5em; + width: 4em; + height: 2em; + background-color: #c32e04; + /* red */ + + transition: all .3s ease-in-out; + border-radius: 1em; +} + +.toggle .slider:before { + /* verschiebbarer Button */ + + position: absolute; + content: ""; + height: 1.6em; + width: 1.6em; + left: 0.2em; + bottom: 0.2em; + background-color: white; + border-radius: 50%; + transition: all .3s ease-in-out; +} + +.toggle input:checked + .slider { + background-color: #5a9900; + /* green */ +} + +.toggle input:focus + .slider { + background-color: pink; + box-shadow: 0 0 1px #5a9900; +} + +.toggle input:checked + .slider:before { + -webkit-transform: translateX(1.9em); + /* Android 4 */ + + -ms-transform: translateX(1.9em); + /* IE9 */ + + transform: translateX(1.9em); +} + +.text .slider:after { + /* Text vor dem FlipFlop-Schalter */ + + position: absolute; + content: "AUS"; + color: #c32e04; + font-weight: bold; + height: 1.6em; + left: -2.5em; + bottom: 0.2em; +} + +.text input:checked + .slider:after { + /* Text hinter dem FlipFlop-Schalter */ + + position: absolute; + content: "AN"; + color: #5a9900; + left: 4.5em; +} + +input#autoexec1 { + border: none; + margin-left: 35px; + padding: 10px 0px 10px 10px; +} + +input#ota { + margin-top: 5px; + margin-bottom: 5px; +} + +#otadiv { + margin-bottom: 15px; +} diff --git a/components/wifi-manager/wifi_manager.c b/components/wifi-manager/wifi_manager.c index c002d64e..7d997700 100644 --- a/components/wifi-manager/wifi_manager.c +++ b/components/wifi-manager/wifi_manager.c @@ -250,7 +250,7 @@ esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len){ return esp_err; } - esp_err = nvs_set_str(handle, name, value); + esp_err = nvs_set_str(handle, name, value); if (esp_err != ESP_OK){ ESP_LOGE(TAG,"Unable to save value %s=%s",name,value); nvs_close(handle); @@ -265,7 +265,7 @@ esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len){ nvs_close(handle); - ESP_LOGD(TAG, "wifi_manager_wrote %s config %s",name,value); + ESP_LOGD(TAG, "wifi_manager_wrote %s=%s with length %i", name, value, len); return ESP_OK; diff --git a/main/console.c b/main/console.c index 61ec537b..96d1b1a6 100644 --- a/main/console.c +++ b/main/console.c @@ -145,7 +145,7 @@ void process_autoexec(){ { ESP_LOGD(TAG,"No matching command found for name autoexec. Adding default entries"); uint8_t autoexec_dft=0; - char autoexec1_dft[256]="squeezelite -o \"I2S\" -b 500:2000 -d all=info -M esp32"; + char autoexec1_dft[256]="squeezelite -o I2S -b 500:2000 -d all=info -M esp32"; store_nvs_value(NVS_TYPE_U8,"autoexec",&autoexec_dft); store_nvs_value(NVS_TYPE_STR,"autoexec1",autoexec1_dft); }