From 50a330d3cc495d6b00c6c4f032d9bd26f9d872c2 Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 26 Oct 2026 01:39:42 +0100 Subject: [PATCH] Add OTA (Over-The-Air) Updates + Button-Debouncing Fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OTA-Updates: - ArduinoOTA integriert (Hostname: kitchenlight.local) - Passwort-geschützter OTA-Upload (Passwort: kitchenlight) - PlatformIO OTA-Umgebung hinzugefügt (esp_wroom_02_ota) - Zukünftige Updates ohne USB-Kabel möglich Bugfixes: - Button-Debouncing korrigiert (ISR setzte Zeit bei jedem Prell-Impuls) - Entprellung jetzt in loop() basierend auf letzter Verarbeitung - Debug: Interrupt-Zähler für Button Debug-Features: - Serielle Test-Befehle: t=toggle, 1-4=Streifen, s=Status, b=Button-Info - Periodischer Status-Print alle 10 Sekunden - GPIO0 Pegel wird mit ausgegeben .gitignore: - compile_commands.json und .vscode/extensions.json hinzugefügt --- .gitignore | 2 + platformio.ini | 14 ++++- src/main.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 154 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index cb42aaa..a5215ad 100755 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ # WiFi-Credentials nicht committen! src/secrets.h +compile_commands.json +.vscode/extensions.json diff --git a/platformio.ini b/platformio.ini index c3176c7..735b25c 100755 --- a/platformio.ini +++ b/platformio.ini @@ -12,5 +12,17 @@ platform = espressif8266 board = esp_wroom_02 framework = arduino -upload_speed = 256000 +upload_speed = 115200 + +; OTA (Over-The-Air) Upload über WLAN +; IP-Adresse des ESP im Serial Monitor ablesen oder im Router nachschauen +; Alternativ mDNS-Name verwenden: kitchenlight.local (funktioniert auf Linux/Mac) +[env:esp_wroom_02_ota] +extends = env:esp_wroom_02 +upload_protocol = espota +; upload_port = 192.168.1.XXX ; IP-Adresse hier eintragen +upload_port = kitchenlight.local ; oder mDNS-Name (einfacher!) +upload_flags = + --auth=kitchenlight + --port=8266 diff --git a/src/main.cpp b/src/main.cpp index af862b3..f5262e3 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "secrets.h" @@ -20,6 +21,10 @@ const int PWM_MIN = 0; const unsigned long SERIAL_BAUD = 115200; +// OTA (Over-The-Air) Konfiguration +const char* OTA_HOSTNAME = "kitchenlight"; +const char* OTA_PASSWORD = "kitchenlight"; // Für Produktivbetrieb ändern! + const unsigned long WIFI_CONNECT_TIMEOUT_MS = 30000; // 30 Sekunden Timeout const unsigned long WIFI_RECONNECT_INTERVAL_MS = 5000; // Alle 5 Sekunden neu versuchen const unsigned long BUTTON_DEBOUNCE_MS = 50; // Entprellzeit für Taster @@ -33,7 +38,7 @@ const int FADE_STEP_SIZE = 1; // Schrittweite pro Updat // ISR-Variable - muss volatile sein, da in Interrupt-Routine verändert! volatile bool buttonPressed = false; -volatile unsigned long buttonPressTime = 0; +volatile uint32_t buttonInterruptCount = 0; // Debug: Anzahl Interrupts // WiFi-Zustand enum WifiState { @@ -76,15 +81,20 @@ void toggleStrip(uint8_t index); void toggleStripByPin(uint8_t pin); void setStripByPin(uint8_t pin, bool on); void runStartupFade(); +void handleButton(); +void handleSerialInput(); +void printStatus(); +void setupOTA(); // ============================================================================ // Interrupt Service Routine (ISR) // ============================================================================ void IRAM_ATTR onButtonPress() { // Nur Flag setzen - die eigentliche Verarbeitung erfolgt in loop() - // Debouncing wird in loop() gemacht + // WICHTIG: Bei prellendem Taster feuert dieser Interrupt mehrfach! + // Deshalb nur das Flag setzen, Debouncing passiert in loop() buttonPressed = true; - buttonPressTime = millis(); + buttonInterruptCount++; } // ============================================================================ @@ -240,29 +250,128 @@ void updateFade() { // Button-Handling mit Debouncing // ============================================================================ void handleButton() { - static unsigned long lastDebounceTime = 0; + static unsigned long lastProcessedTime = 0; + static uint32_t lastInterruptCount = 0; if (!buttonPressed) return; - // Debouncing: Nur reagieren wenn genug Zeit seit letztem Tastendruck vergangen unsigned long now = millis(); - if (now - buttonPressTime < BUTTON_DEBOUNCE_MS) { + + // Debug: Zeige wie oft der Interrupt seit letztem Druck gefeuert hat + uint32_t interrupts = buttonInterruptCount - lastInterruptCount; + lastInterruptCount = buttonInterruptCount; + + // Debouncing: Nur verarbeiten wenn seit dem letzten verarbeiteten Druck + // mindestens BUTTON_DEBOUNCE_MS vergangen sind. + // Dadurch werden Prell-Impulse (mehrere Interrupts innerhalb weniger ms) ignoriert. + if (now - lastProcessedTime < BUTTON_DEBOUNCE_MS) { + // Noch innerhalb der Debounce-Zeit - verwerfen buttonPressed = false; + Serial.printf("[Debounce] Impuls verworfen (%lu Interrupts)\n", interrupts); return; } - if (now - lastDebounceTime < BUTTON_DEBOUNCE_MS) { - buttonPressed = false; - return; - } - - lastDebounceTime = now; + // Gültiger Tastendruck! + lastProcessedTime = now; buttonPressed = false; - Serial.println("Taster gedrückt!"); + Serial.printf("Taster gedrückt! (%lu Interrupts, GPIO0=%d)\n", + interrupts, digitalRead(PIN_BUTTON)); toggleStrip(0); // Streifen 1 (Index 0) umschalten } +// ============================================================================ +// Debug & Test-Funktionen +// ============================================================================ + +// Serielle Eingabe verarbeiten (zum Testen ohne Hardware-Taster) +// 't' = Toggle Streifen 1, '1'-'4' = Toggle Streifen 1-4, 's' = Status +void handleSerialInput() { + if (!Serial.available()) return; + + char c = Serial.read(); + Serial.printf("[Serial] Empfangangen: '%c'\n", c); + + switch (c) { + case 't': + case 'T': + Serial.println("Toggle Streifen 1 (über Serial)"); + toggleStrip(0); + break; + case '1': + toggleStrip(0); + break; + case '2': + toggleStrip(1); + break; + case '3': + toggleStrip(2); + break; + case '4': + toggleStrip(3); + break; + case 's': + case 'S': + printStatus(); + break; + case 'b': + case 'B': + Serial.printf("Button Interrupts gesamt: %lu\n", buttonInterruptCount); + Serial.printf("GPIO0 (Button) aktueller Pegel: %d (0=gedrückt, 1=losgelassen)\n", + digitalRead(PIN_BUTTON)); + break; + default: + Serial.println("Verfügbare Befehle: t=toggle, 1-4=streifen, s=status, b=button-info"); + break; + } +} + +// Status aller Streifen und des Buttons ausgeben +void printStatus() { + Serial.println("--- Status ---"); + for (uint8_t i = 0; i < 4; i++) { + Serial.printf("Streifen %d (GPIO%d): %s\n", + i + 1, stripPins[i], stripStates[i] ? "AN" : "AUS"); + } + Serial.printf("GPIO0 (Button): %d (0=gedrückt, 1=losgelassen)\n", + digitalRead(PIN_BUTTON)); + Serial.printf("WiFi: %s\n", + wifiState == WIFI_CONNECTED ? "verbunden" : + wifiState == WIFI_CONNECTING ? "verbinde..." : "getrennt"); + Serial.printf("Button Interrupts: %lu\n", buttonInterruptCount); + Serial.println("--------------"); +} + +// ============================================================================ +// OTA (Over-The-Air) Update-Funktionen +// ============================================================================ +void setupOTA() { + ArduinoOTA.setHostname(OTA_HOSTNAME); + ArduinoOTA.setPassword(OTA_PASSWORD); + + ArduinoOTA.onStart([]() { + Serial.println("OTA Update gestartet..."); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nOTA Update beendet!"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("OTA Fortschritt: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("OTA Fehler[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Authentifizierung fehlgeschlagen"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin fehlgeschlagen"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Verbindung fehlgeschlagen"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Empfang fehlgeschlagen"); + else if (error == OTA_END_ERROR) Serial.println("Beenden fehlgeschlagen"); + }); + + ArduinoOTA.begin(); + Serial.println("OTA bereit!"); + Serial.printf("Hostname: %s.local\n", OTA_HOSTNAME); +} + // ============================================================================ // Setup // ============================================================================ @@ -291,6 +400,9 @@ void setup() { // WiFi-Verbindung starten (non-blocking!) startWifiConnect(); + // OTA starten (wartet auf WiFi-Verbindung) + setupOTA(); + Serial.println("Setup abgeschlossen. Warte auf WiFi..."); } @@ -298,6 +410,9 @@ void setup() { // Main Loop // ============================================================================ void loop() { + // OTA-Updates verarbeiten (muss regelmäßig aufgerufen werden!) + ArduinoOTA.handle(); + // WiFi-Verbindung verwalten (non-blocking) checkWifiConnection(); @@ -313,4 +428,15 @@ void loop() { // Taster verarbeiten (mit Debouncing) handleButton(); + + // Serielle Eingabe verarbeiten (zum Testen) + handleSerialInput(); + + // Alle 10 Sekunden Status ausgeben (Debug) + static unsigned long lastStatusPrint = 0; + if (millis() - lastStatusPrint >= 10000) { + lastStatusPrint = millis(); + Serial.printf("[Status] GPIO0=%d, Interrupts=%lu, WiFi=%d\n", + digitalRead(PIN_BUTTON), buttonInterruptCount, wifiState); + } }