console
<!DOCTYPE html>
<html>
<head>
<title>ESP32 WIFI Config</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
</head>
<style>
.b1 {
width: 94%;
border: 1px solid #ccc;
padding: 10px 2%;
margin: 10px auto;
font-size: 15px;
}
h6 {
margin: 0 auto;
padding: 5px 0px;
font-size: 14px;
}
h6 label {
width: 18%;
padding: 3px 10px 0 0;
float: left;
}
h6 input {
height: 20px;
width: 60%;
float: left;
}
.clr {
font-size: 0px;
clear: both;
}
</style>
<body style="max-width:600px;margin:0 auto;">
<h1 style="width:100%;margin-top:0;padding:30px 0;text-align: center;background: linear-gradient(145deg, #4d94ff 10%,#ff3385 60%,#ff4000 100%);">
ESP32 WIFI Config
<div style="font-size:10px;text-align:center;color:#333;padding-top:15px;">DouYin:红薯(hong.wang)</div>
</h1>
<form class="b1">
<h6>STA设置,链接上无线路由器</h6>
<h6>
<label>状态:</label>
<input type="text" disabled id="stainfo">
<div class="clr"></div>
</h6>
<h6>
<label>SSID:</label>
<input type="text" id="stassid">
<div class="clr"></div>
</h6>
<h6>
<label>PASS:</label>
<input type="text" id="stapass">
<div class="clr"></div>
</h6>
<h6>
<label>DHCP:</label>
<input type="checkbox" id="isdhcp" checked/>
<div class="clr"></div>
</h6>
<h6>
<label>IP:</label>
<input type="text" id="staip">
<div class="clr"></div>
</h6>
<h6>
<label>GATEWAY:</label>
<input type="text" id="stagateway">
<div class="clr"></div>
</h6>
<h6>
<label>SUBNET:</label>
<input type="text" id="stasubnet">
<div class="clr"></div>
</h6>
<h6>
<label>DNS1:</label>
<input type="text" id="stadns1">
<div class="clr"></div>
</h6>
<h6>
<label>DNS2:</label>
<input type="text" id="stadns2">
<div class="clr"></div>
</h6>
<input type="button" value="保存并链接" onclick="sta()" style="font-size: 15px;margin: 20px auto;">
<div class="clr"> </div>
</form>
<form class="b1">
<h6>AP设置,修改芯片无线信号</h6>
<h6>
<label>状态:</label>
<input type="text" disabled id="apinfo">
<div class="clr"></div>
</h6>
<h6>
<label>SSID:</label>
<input type="text" id="apssid">
<div class="clr"></div>
</h6>
<h6>
<label>PASS:</label>
<input type="text" id="appass">
<div class="clr"></div>
</h6>
<input type="button" value="保存并重启" onclick="ap()" style="font-size: 15px;margin: 20px auto;">
<div class="clr"> </div>
</form>
<form id="uf" method="post" action="/" enctype="multipart/form-data" class="b1">
<h6>固件更新(bin文件)</h6>
<h5>
<p>上传成功后会自动更新并且重启。</p>
<p>更新成功需要重新链接此wifi才能进行其它操作。</p>
<p>谨慎操作! 谨慎操作!! 谨慎操作!!!</p>
</h5>
<input type="file" name="file" style="width: 60%;font-size: 15px;float: left;">
<input type="button" onclick="uf()" value="上传" id="s" style="float: right;font-size: 15px;margin-right: 10px;">
<div style="clear:both;font-size:0px;"> </div>
</form>
<script>
function ap(){
if(confirm("你确定要修改吗?\r\n确定后芯片会重启\r\n请重新链接修改后的wifi.")){
apssid = document.getElementById("apssid").value;
appass = document.getElementById("appass").value;
if (
!(/^[0-9a-zA-Z_-]+$/.test(apssid)) || ssid.length > 16 || ssid.length < 4 ||
!(/^[0-9a-zA-Z_-]+$/.test(appass)) || pass.length > 16 || pass.length > 8
){
alert("AP配置ssid[4-16]和pass[8-16]应为数字、字母!");
return;
}
base64str = btoa("{\"ssid\":\""+apssid+"\",\"pass\":\""+appass+"\"}");
var xhr = new XMLHttpRequest();
xhr.open('POST', '/apconfig', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert("请等待并手动链接上新的wifi.");
} else {
alert("请求异常." + xhr.status);
}
}
};
xhr.send('datas='+base64str);
}
}
function sta(){
if(confirm("你确定要设置STA并尝试链接吗?")){
ssid = document.getElementById("stassid").value;
pass = document.getElementById("stapass").value;
isdhcp = document.getElementById("isdhcp").checked?"true":"false";
staip = document.getElementById("staip").value;
stagateway = document.getElementById("stagateway").value;
stasubnet = document.getElementById("stasubnet").value;
stadns1 = document.getElementById("stadns1").value;
stadns2 = document.getElementById("stadns2").value;
if (
!(/^[0-9a-zA-Z_-]+$/.test(ssid)) || !(/^[0-9a-zA-Z_-]+$/.test(pass))
){
alert("STA配置ssid和pass应为数字、字母!");
return;
}
base64str = btoa("{\"ssid\":\""+ssid+"\",\"pass\":\""+pass+"\",\"staip\":\""+staip+"\",\"stagateway\":\""+stagateway+"\",\"stasubnet\":\""+stasubnet+"\",\"stadns1\":\""+stadns1+"\",\"stadns2\":\""+stadns2+"\",\"isdhcp\":\""+isdhcp+"\"}");
var xhr = new XMLHttpRequest();
xhr.open('POST', '/staconfig', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert("设置成功.");
t();
} else {
alert("请求异常." + xhr.status);
}
}
};
xhr.send('datas='+base64str);
}
}
function t(){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/ap_sta_state', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var j = JSON.parse(atob(xhr.responseText));
document.getElementById('apinfo').value = j.ap_state;
document.getElementById('apssid').value = j.ap_ssid;
document.getElementById('appass').value = j.ap_pwd;
document.getElementById('stainfo').value = j.sta_state;
document.getElementById('stassid').value = j.sta_ssid;
document.getElementById('stapass').value = j.sta_pwd;
document.getElementById('staip').value = j.sta_ip;
document.getElementById('stagateway').value = j.sta_gateway;
document.getElementById('stasubnet').value = j.sta_subnet;
document.getElementById('stadns1').value = j.sta_dns1;
document.getElementById('stadns2').value = j.sta_dns2;
document.getElementById('isdhcp').checked = j.sta_is_dhcp=="true"?true:false;
} else {
alert("获取芯片WIFI状态失败." + xhr.status);
}
}
};
xhr.send();
}
function uf(){
var uf = document.getElementById('uf');
var s = document.getElementById('s');
var fileInput = uf.querySelector('input[type=file]');
s.disabled = true;
var file = fileInput.files[0];
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upfile_ota', true);
xhr.onload = function() {
if (xhr.status === 200) {
// console.log('Response:', xhr.responseText);
// alert(xhr.responseText);
if(xhr.responseText=="OK"){
s.value="操作完成!";
}else{
alert("操作失败!");
s.disabled = false;
}
} else {
// console.log('Error:', xhr.status);
alert("上传失败" + xhr.status);
s.disabled = false;
}
};
xhr.send(formData);
}
document.addEventListener('DOMContentLoaded', function() {
t();
});
</script>
</body>
</html>