Files

73 lines
2.2 KiB
C++
Raw Permalink Normal View History

2026-06-28 13:15:53 -05:00
#include "app.h"
#include <ESPmDNS.h>
#include <LittleFS.h>
void applyLed() {
uint8_t pct = constrain(ledBrightness, 0, 100);
uint8_t duty = map(pct, 0, 100, 0, 255);
if (ledInverted) duty = 255 - duty;
analogWrite(LED_BUILTIN, duty);
}
2026-06-28 16:25:55 -05:00
void applyPump() {
digitalWrite(PUMP_PIN, pumpEnabled ? PUMP_ACTIVE_LEVEL : !PUMP_ACTIVE_LEVEL);
}
2026-06-28 13:15:53 -05:00
void factoryReset() {
prefs.clear();
LittleFS.remove(LOG_FILE_PATH);
appendLog(LOG_SECURITY_AUDIT, "factory reset requested");
}
void checkFactoryResetPin() {
pinMode(FACTORY_RESET_PIN, INPUT_PULLUP);
if (digitalRead(FACTORY_RESET_PIN) != FACTORY_RESET_ACTIVE_LEVEL) return;
uint32_t start = millis();
while (millis() - start < FACTORY_RESET_HOLD_MS) {
delay(100);
if (digitalRead(FACTORY_RESET_PIN) != FACTORY_RESET_ACTIVE_LEVEL) {
return;
}
}
factoryReset();
ESP.restart();
}
bool connectWifi() {
String ssid = prefString("wifiSsid", "");
String pass = prefString("wifiPass", "");
if (!ssid.length()) return false;
String name = deviceName();
WiFi.mode(WIFI_STA);
WiFi.setHostname(name.c_str());
if (!networkDhcp) {
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
IPAddress dns1;
IPAddress dns2;
bool ok = ip.fromString(networkIp) && gateway.fromString(networkGateway) && subnet.fromString(networkSubnet);
if (ok) {
if (!networkDns1.length() || !dns1.fromString(networkDns1)) dns1 = gateway;
if (!networkDns2.length() || !dns2.fromString(networkDns2)) dns2 = IPAddress(0, 0, 0, 0);
if (!WiFi.config(ip, gateway, subnet, dns1, dns2)) appendLog(LOG_WARN, "static IP configuration failed; continuing with DHCP");
} else {
appendLog(LOG_WARN, "static IP configuration is incomplete or invalid; continuing with DHCP");
}
}
WiFi.begin(ssid.c_str(), pass.c_str());
appendLog(LOG_INFO, "connecting to WiFi " + ssid);
uint32_t start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) {
delay(250);
}
if (WiFi.status() == WL_CONNECTED) {
MDNS.begin(name.c_str());
appendLog(LOG_INFO, "WiFi connected " + WiFi.localIP().toString());
return true;
}
appendLog(LOG_WARN, "WiFi connection failed; entering setup AP");
return false;
}