Output.cycle FIX

This commit is contained in:
Patrick Haßel 2025-09-01 09:46:08 +02:00
parent ae1c6f5cda
commit b88df843ad
2 changed files with 12 additions and 8 deletions

View File

@ -27,14 +27,15 @@ protected:
unsigned long stateMillis = 0;
void _write(const bool state) {
if (state != get()) {
void _write(const bool wanted) {
const auto current = get();
if (wanted != current) {
if (logState) {
Serial.printf("[RELAY] \"%s\" = %s\n", name.c_str(), state ? "ON" : "OFF");
Serial.printf("[RELAY] \"%s\" = %s\n", name.c_str(), wanted ? "ON" : "OFF");
}
stateMillis = millis();
}
digitalWrite(pin, state ^ inverted ? HIGH : LOW);
digitalWrite(pin, wanted ^ inverted ? HIGH : LOW);
}
void _applyInitial() {
@ -73,19 +74,21 @@ public:
}
void cycle(const unsigned long onMillis_, const unsigned long offMillis_, const unsigned long onCount_ = -1) {
set(false); // this sets onCount=0, so do this first
this->onMillis = onMillis_;
this->offMillis = offMillis_;
this->onCount = onCount_;
set(false);
}
void loop() {
if (get()) {
if (onMillis > 0 && millis() - stateMillis > onMillis) {
const auto status = get();
const auto ageMillis = millis() - stateMillis;
if (status) {
if (onMillis > 0 && ageMillis >= onMillis) {
_write(false);
}
} else {
if (offMillis > 0 && millis() - stateMillis > offMillis && onCount != 0) {
if (offMillis > 0 && ageMillis >= offMillis && onCount != 0) {
_write(true);
if (onCount > 0) {
onCount--;

View File

@ -30,6 +30,7 @@ void wifiConnect() {
const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID);
const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, true, true);
Serial.printf("[WiFi] Connecting: \"%s\"\n", wifiSSID.c_str());
WiFi.hostname(hostname);
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
wifiLast = max(1UL, millis());