I made a program to change the access point but instead it made the esp restart even though there was no command to restart the access point, only the access point was having problems?
jws.ino.txt (134.9 KB)
index.html.txt (125.2 KB)
server.on("/setap", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("ssid", true) && request->hasParam("password", true)) {
String ssid = request->getParam("ssid", true)->value();
String pass = request->getParam("password", true)->value();
if (pass.length() > 0 && pass.length() < 8) {
request->send(400, "text/plain", "Password minimal 8 karakter");
return;
}
Serial.println("\n========================================");
Serial.println("Simpan AP");
Serial.println("========================================");
Serial.println("New AP SSID: " + ssid);
ssid.toCharArray(wifiConfig.apSSID, 33);
pass.toCharArray(wifiConfig.apPassword, 65);
saveAPCredentials();
Serial.println("Stopping old AP...");
WiFi.softAPdisconnect(false);
delay(200);
Serial.println("Forcing AP_STA mode...");
WiFi.mode(WIFI_AP_STA);
delay(100);
WiFi.softAP(wifiConfig.apSSID, wifiConfig.apPassword);
delay(200);
IPAddress newAPIP = WiFi.softAPIP();
Serial.println("========================================");
Serial.println("AP Settings updated");
Serial.println("New SSID: " + String(wifiConfig.apSSID));
Serial.println("New IP: " + newAPIP.toString());
Serial.println("========================================\n");
request->send(200, "text/plain", "OK");
} else {
request->send(400, "text/plain", "Missing parameters");
}
});
function setAP() {
const ssid = document.getElementById('apSSID').value.trim();
const password = document.getElementById('apPassword').value;
if (!ssid) {
showToast('SSID Access Point tidak boleh kosong', 'error');
return;
}
if (password.length > 0 && password.length < 8) {
showToast('Password minimal 8 karakter', 'error');
return;
}
const btn = event.target;
const originalText = btn.textContent;
btn.disabled = true;
btn.textContent = 'Menyimpan...';
fetch('/setap', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `ssid=${encodeURIComponent(ssid)}&password=${encodeURIComponent(password)}`
})
.then(response => {
if (response.ok) {
savedWiFiConfig.apSSID = ssid;
if (password) {
savedWiFiConfig.apPassword = password;
}
showToast('Pengaturan Access Point berhasil disimpan', 'success');
document.getElementById('apPassword').value = '';
setTimeout(() => {
loadWiFiConfig();
}, 1000);
setTimeout(() => {
showToast('Sambungkan ke AP: ' + ssid, 'warning');
}, 3500);
setTimeout(() => {
btn.disabled = false;
btn.textContent = originalText;
}, 6000);
setTimeout(() => {
showToast('Halaman akan di-refresh otomatis...', 'warning');
setTimeout(() => {
window.location.reload();
}, 2000);
}, 18000);
} else {
return response.text().then(text => {
throw new Error(text);
});
}
})
.catch(error => {
showToast('Gagal menyimpan AP: ' + error.message, 'error');
btn.disabled = false;
btn.textContent = originalText;
});
}