Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import/export settings #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions config_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const char config_html[] PROGMEM = R"=====(
<h1>** ESP32 Radio **</h1>
<p>You can edit the configuration here. <i>Note that this will be effective on the next restart of the radio.</i></p>
<h3>Available WiFi networks
<select class="select" onChange="handletone(this)" id="ssid"></select>
<select class="select" id="ssid"></select>
</h3>
<textarea rows="20" cols="100" id="prefs">Loading preferences</textarea>
<br>
Expand All @@ -32,10 +32,37 @@ const char config_html[] PROGMEM = R"=====(
<button class="button buttonr" onclick="httpGet('reset')">Restart</button>
&nbsp;&nbsp;
<button class="button" onclick="ldef('getdefs')">Default</button>
<br><input type="text" size="80" id="resultstr" placeholder="Waiting for input....">
<br>
&nbsp;&nbsp;
<button class="button buttonb" onclick="wFile()">Export</button>
&nbsp;&nbsp;
<button class="button buttonb" onclick="f.click()">Import</button>
<input type="file" id="f" style="display:none;" accept="text/*"/>
<br><input type="text" size="80" id="resultstr" placeholder="Waiting for input....">

<script>

f.addEventListener('change', rFile, false);

function rFile(e) {
if (!e.target.files[0]) return;
var r = new FileReader();
r.onload = function(e) {
prefs.value = e.target.result;

};
r.readAsText(e.target.files[0]);
}

function wFile() {
var e = document.createElement('a');
e.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(prefs.value));
e.setAttribute('download', 'esp32radio_settings.txt');
e.style.display = 'none';
document.body.appendChild(e);
e.click();
document.body.removeChild(e);
}

function httpGet ( theReq )
{
var theUrl = "/?" + theReq + "&version=" + Math.random() ;
Expand All @@ -50,7 +77,6 @@ const char config_html[] PROGMEM = R"=====(
xhr.send() ;
}

// Load preferences or default preferences
function ldef ( source )
{
var xhr = new XMLHttpRequest() ;
Expand All @@ -65,7 +91,6 @@ const char config_html[] PROGMEM = R"=====(
xhr.send() ;
}

// Save the preferences
function fsav()
{
var str = prefs.value ;
Expand All @@ -78,7 +103,6 @@ const char config_html[] PROGMEM = R"=====(
resultstr.value = xhr.responseText ;
}
}
// Remove empty lines
while ( str.indexOf ( "\r\n\r\n" ) >= 0 )
{
str = str.replace ( /\r\n\r\n/g, "\r\n" )
Expand All @@ -92,32 +116,30 @@ const char config_html[] PROGMEM = R"=====(
xhr.send ( str + "\n" ) ;
}

// Fill configuration initially
// First the available WiFi networks
var i, select, opt, networks, params ;

select = document.getElementById("selnet") ;
var theUrl = "/?getnetworks" + "&version=" + Math.random() ;
var xhr = new XMLHttpRequest() ;
xhr.onreadystatechange = function() {
if ( xhr.readyState == XMLHttpRequest.DONE )
{
networks = xhr.responseText.split ( "|" ) ;

for ( i = 0 ; i < ( networks.length - 1 ) ; i++ )
function lnet(){
var i, opt, networks ;
var xhr = new XMLHttpRequest() ;
xhr.onreadystatechange = function() {
if ( xhr.readyState == XMLHttpRequest.DONE )
{
opt = document.createElement( "OPTION" ) ;
opt.value = i ;
opt.text = networks[i] ;
ssid.add( opt ) ;
networks = xhr.responseText.split ( "|" ) ;

for ( i = 0 ; i < ( networks.length - 1 ) ; i++ )
{
opt = document.createElement( "OPTION" ) ;
opt.value = i ;
opt.text = networks[i] ;
ssid.add( opt ) ;
}
ldef ( "getprefs" ) ;
}
}
xhr.open ( "GET", "/?getnetworks" + "&version=" + Math.random(), false ) ;
xhr.send() ;
}
xhr.open ( "GET", theUrl, false ) ;
xhr.send() ;
// Now get the configuration parameters from preferences
ldef ( "getprefs" ) ;

lnet();
</script>
</body>
</html>
)=====" ;
)=====" ;
4 changes: 3 additions & 1 deletion radio_css.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ li a:hover, a.active {

.buttonr {background-color: #D62C1A;}

.buttonb {background-color: #1E90FF;}

.select {
width: 160px;
height: 32px;
Expand Down Expand Up @@ -122,4 +124,4 @@ textarea {
font-family: Courier, "Lucida Console", monospace;
background-color: white;
resize: none;
})=====" ;
})=====" ;