diff --git a/wifi-fallback.js b/wifi-fallback.js new file mode 100644 index 0000000..fa171ea --- /dev/null +++ b/wifi-fallback.js @@ -0,0 +1,75 @@ +// WiFi AP Fallback Script v1.1 +// Fix: volledige AP-config meegeven bij SetConfig + +let CONFIG = { + checkInterval: 60, + apDisableOnReconnect: true, + debugLog: true +}; + +let apActive = false; +let apConfig = null; // Bewaar originele AP-config + +function log(msg) { + if (CONFIG.debugLog) print("[AP-Fallback] " + msg); +} + +function setAP(enable) { + if (enable === apActive || apConfig === null) return; + + log(enable ? "WiFi verloren - AP inschakelen..." : "WiFi hersteld - AP uitschakelen..."); + + // Volledige AP-config meesturen, alleen 'enable' aanpassen + let newApCfg = { + ssid: apConfig.ssid, + enable: enable + }; + + // Wachtwoord alleen meesturen als het ingesteld is + if (apConfig.is_open === false) { + newApCfg.pass = apConfig.pass || ""; + } + + Shelly.call("WiFi.SetConfig", { ap: newApCfg }, function (res, err) { + if (err !== 0) { + log("Fout bij instellen AP: " + JSON.stringify(err)); + } else { + apActive = enable; + log("AP is nu " + (enable ? "AAN" : "UIT")); + } + }); +} + +function checkWiFi() { + Shelly.call("WiFi.GetStatus", {}, function (res, err) { + if (err !== 0) { + log("Kan WiFi-status niet ophalen"); + return; + } + + let connected = (res.sta_ip !== null && res.sta_ip !== ""); + + if (!connected) { + setAP(true); + } else if (connected && CONFIG.apDisableOnReconnect && apActive) { + setAP(false); + } + }); +} + +// Eerst huidige AP-config ophalen als referentie +Shelly.call("WiFi.GetConfig", {}, function (res, err) { + if (err !== 0 || !res.ap) { + log("Kan AP-config niet ophalen - script stopt"); + return; + } + + apConfig = res.ap; + apActive = res.ap.enable; + log("Start - AP: " + (apActive ? "AAN" : "UIT") + ", SSID: " + apConfig.ssid); + + // Pas starten na ophalen config + Timer.set(CONFIG.checkInterval * 1000, true, checkWiFi); + checkWiFi(); + log("Controle elke " + CONFIG.checkInterval + "s"); +}); \ No newline at end of file