70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
#pragma once
|
|
|
|
// =============================================================================
|
|
// mqtt_client.h - MQTT-Verbindung, Publish und Subscribe
|
|
// Projekt: MQTT-Display LaserCutter
|
|
//
|
|
// Wrapper um PubSubClient mit Non-Blocking Reconnect.
|
|
//
|
|
// Publish:
|
|
// publishSession() - beim Ende eines Laser-Bursts (aus LaserTracker)
|
|
// publishStatus() - Heartbeat alle MQTT_HEARTBEAT_MS
|
|
//
|
|
// Subscribe:
|
|
// lasercutter/reset - Payload "1" -> laserTracker.resetTotal()
|
|
//
|
|
// Verwendung:
|
|
// mqttClient.begin(); // einmalig in setup(), nach WiFi-Connect
|
|
// mqttClient.loop(); // in jedem loop()-Aufruf
|
|
// =============================================================================
|
|
|
|
#include <Arduino.h>
|
|
#include <PubSubClient.h>
|
|
#include <WiFi.h>
|
|
#include <WiFiClientSecure.h>
|
|
#include "config.h"
|
|
#include "settings.h"
|
|
|
|
class MqttClient {
|
|
public:
|
|
MqttClient();
|
|
|
|
// MQTT konfigurieren und erste Verbindung versuchen (einmalig in setup())
|
|
void begin();
|
|
|
|
// Verbindung halten, Reconnect, Heartbeat - in jedem loop()-Aufruf
|
|
void loop();
|
|
|
|
// Publish: Session-Ende (wird von LaserTracker-Logik in main aufgerufen)
|
|
// lastBurstSec : Netto-Sekunden des letzten Bursts
|
|
// totalMinutes : Gesamtzeit inkl. aktuelle Session (aus LaserTracker)
|
|
// gratisSec : konfigurierte Gratiszeit
|
|
void publishSession(int lastBurstSec, float totalMinutes, int gratisSec);
|
|
|
|
// Verbindungsstatus
|
|
bool isConnected();
|
|
|
|
// Debug-Ausgabe auf Serial
|
|
void printToSerial();
|
|
|
|
private:
|
|
WiFiClient _wifiClient;
|
|
WiFiClientSecure _secureClient; // fuer TLS (Port 8883)
|
|
PubSubClient _client;
|
|
|
|
uint32_t _lastReconnectMs;
|
|
uint32_t _lastHeartbeatMs;
|
|
char _clientId[32]; // MQTT_CLIENT_ID + MAC-Suffix (eindeutig auf Public Broker)
|
|
|
|
// Verbindungsaufbau (intern, non-blocking: gibt true zurueck wenn verbunden)
|
|
bool reconnect();
|
|
|
|
// Heartbeat-Publish (lasercutter/status)
|
|
void publishHeartbeat();
|
|
|
|
// Callback fuer eingehende Nachrichten (static wegen PubSubClient-API)
|
|
static void onMessage(const char* topic, byte* payload, unsigned int length);
|
|
};
|
|
|
|
// Globale Instanz
|
|
extern MqttClient mqttClient; |