126 lines
4.1 KiB
C++
126 lines
4.1 KiB
C++
// =============================================================================
|
|
// test_mqtt_client.cpp - Phase 6: MqttClient Verifikation
|
|
// Projekt: MQTT-Display LaserCutter
|
|
//
|
|
// Testet: WiFi (WifiManager) -> MQTT Connect -> Heartbeat -> publishSession
|
|
// -> Reset-Kommando empfangen
|
|
//
|
|
// Hardware:
|
|
// - Taste GPIO 4 (als Laser-Signal-Simulator, wie test_laser_tracker)
|
|
// - BOOT-Taste GPIO 0 (3s = Gesamtzeit loeschen + MQTT reset)
|
|
//
|
|
// Verwendung:
|
|
// 1. Flashen: pio run -e test-mqtt --target upload
|
|
// 2. Serial Monitor: pio device monitor -e test-mqtt
|
|
// 3. MQTT Explorer mit broker.hivemq.com verbinden
|
|
// - Subscribe: lasercutter/# -> Session/Status-Nachrichten beobachten
|
|
// - Publish: lasercutter/reset Payload: 1 -> Gesamt-Reset testen
|
|
// 4. Verifikation der Topics:
|
|
// lasercutter/session -> wird nach Laser-AUS veroeffentlicht
|
|
// lasercutter/status -> Heartbeat alle 60s (retained)
|
|
// =============================================================================
|
|
|
|
#include <Arduino.h>
|
|
#include "config.h"
|
|
#include "settings.h"
|
|
#include "wifi_connector.h"
|
|
#include "display_manager.h"
|
|
#include "laser_tracker.h"
|
|
#include "mqtt_client.h"
|
|
#include "mqtt_test_secrets.h" // LOKAL, gitignoriert - siehe mqtt_test_secrets.h.example
|
|
|
|
// BOOT-Taste
|
|
static const int BOOT_BTN = 0;
|
|
static uint32_t bootPressMs = 0;
|
|
|
|
// Status-Druck alle 5 Sekunden
|
|
static uint32_t lastStatusMs = 0;
|
|
|
|
// NVS-Fix: Stellt sicher, dass keine alten Werte stoeren.
|
|
// MQTT-Config wird auf Default zurueckgesetzt, damit ein alter NVS-Eintrag
|
|
// (z.B. 10.0.0.42:8883 aus frueherem Test) nicht den Public-Broker-Test stoert.
|
|
void applyDefaults() {
|
|
settings.saveGratisSeconds(DEFAULT_GRATIS_SECONDS);
|
|
// Broker-Config aus mqtt_test_secrets.h (gitignoriert)
|
|
settings.saveMqttConfig(TEST_MQTT_BROKER, TEST_MQTT_PORT,
|
|
TEST_MQTT_USER, TEST_MQTT_PASSWORD);
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(SERIAL_BAUD_RATE);
|
|
delay(500);
|
|
|
|
settings.begin();
|
|
applyDefaults();
|
|
LOG_I("TEST-MQTT", "=== Phase 6 MQTT Test gestartet ===");
|
|
LOG_I("TEST-MQTT", "Broker Default: %s:%d", DEFAULT_MQTT_BROKER, DEFAULT_MQTT_PORT);
|
|
settings.printToSerial();
|
|
|
|
display.begin();
|
|
display.printToSerial();
|
|
display.showWifiError(false);
|
|
display.showMqttError(false);
|
|
display.showLaserTime(0.0f);
|
|
display.showIdle();
|
|
|
|
laserTracker.begin();
|
|
laserTracker.printToSerial();
|
|
|
|
wifiConnector.begin();
|
|
wifiConnector.printToSerial();
|
|
|
|
mqttClient.begin();
|
|
mqttClient.printToSerial();
|
|
|
|
pinMode(BOOT_BTN, INPUT);
|
|
LOG_I("TEST-MQTT", "Setup abgeschlossen. Taste GPIO4=Laser, GPIO0=Reset");
|
|
}
|
|
|
|
void loop() {
|
|
laserTracker.loop();
|
|
wifiConnector.loop();
|
|
mqttClient.loop();
|
|
|
|
// MQTT: Session-Publish nach Burst-Ende
|
|
if (laserTracker.consumeBurstEnd()) {
|
|
int lastBurst = laserTracker.getLastBurstSeconds();
|
|
if (lastBurst > 0) {
|
|
LOG_I("TEST-MQTT", "BurstEnd erkannt -> publishSession (lastBurst=%ds)", lastBurst);
|
|
mqttClient.publishSession(lastBurst, settings.get().gratisSeconds);
|
|
} else {
|
|
LOG_I("TEST-MQTT", "BurstEnd erkannt, aber nur Gratiszeit -> kein Publish");
|
|
}
|
|
}
|
|
|
|
// Display
|
|
display.showLaserTime((float)laserTracker.getSessionMinutes());
|
|
int countdown = laserTracker.getCountdownRemaining();
|
|
if (laserTracker.isActive() && countdown > 0) {
|
|
display.showCountdown(countdown);
|
|
} else {
|
|
display.showIdle();
|
|
}
|
|
display.update();
|
|
|
|
// BOOT-Taste (GPIO 0): 3s halten -> Gesamtzeit loeschen
|
|
if (digitalRead(BOOT_BTN) == LOW) {
|
|
if (bootPressMs == 0) bootPressMs = millis();
|
|
if (millis() - bootPressMs >= 3000) {
|
|
LOG_I("TEST-MQTT", "BOOT 3s: Gesamtzeit loeschen");
|
|
laserTracker.resetTotal();
|
|
bootPressMs = 0;
|
|
}
|
|
} else {
|
|
bootPressMs = 0;
|
|
}
|
|
|
|
// Periodischer Status-Druck
|
|
if (millis() - lastStatusMs >= 5000) {
|
|
lastStatusMs = millis();
|
|
LOG_I("TEST-MQTT", "--- Status ---");
|
|
mqttClient.printToSerial();
|
|
laserTracker.printToSerial();
|
|
}
|
|
|
|
delay(50);
|
|
} |