Umstellung auf MQTT

This commit is contained in:
Carsten Graf
2025-06-01 21:41:57 +02:00
parent 2d2ee0a41a
commit 5f86308367
4 changed files with 15 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
#include <Arduino.h> #include <Arduino.h>
#include "master.h" #include "master.h"
#include <ArduinoJson.h>
#include <PicoMQTT.h> #include <PicoMQTT.h>
@@ -21,14 +22,18 @@ PicoMQTT::Server mqtt;
void setupMqttServer() { void setupMqttServer() {
// Set up the MQTT server with the desired port // Set up the MQTT server with the desired port
// Subscribe to a topic pattern and attach a callback // Subscribe to a topic pattern and attach a callback
mqtt.subscribe("#", [](const char * topic, const char * payload) { mqtt.subscribe("#", [](const char * topic, const char * payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload); Serial.printf("Received message in topic '%s': %s\n", topic, payload);
updateStatusLED(3); // Flash LED on message received
}); });
// Start the MQTT server // Start the MQTT server
mqtt.begin(); mqtt.begin();
Serial.println("MQTT server started on port 1883");
} }
void loopMqttServer() { void loopMqttServer() {
@@ -38,7 +43,7 @@ void loopMqttServer() {
// Optionally, you can publish a message periodically // Optionally, you can publish a message periodically
static unsigned long lastPublish = 0; static unsigned long lastPublish = 0;
if (millis() - lastPublish > 5000) { // Publish every 5 seconds if (millis() - lastPublish > 5000) { // Publish every 5 seconds
mqtt.publish("test/topic", "Hello from ESP32!"); mqtt.publish("heartbeat/live", "Alive!");
lastPublish = millis(); lastPublish = millis();
} }
} }

View File

@@ -298,6 +298,7 @@ String getTimerDataJSON() {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
if (!SPIFFS.begin(true)) { if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed"); Serial.println("SPIFFS Mount Failed");
return; return;
@@ -318,7 +319,7 @@ void setup() {
setupOTA(&server); setupOTA(&server);
setupRoutes(); setupRoutes();
setupLED();
setupMqttServer(); // MQTT Server initialisieren setupMqttServer(); // MQTT Server initialisieren

View File

@@ -3,10 +3,15 @@
#define LED_PIN LED_BUILTIN #define LED_PIN LED_BUILTIN
// Status LED // Status LED
unsigned long lastLedBlink = 0; unsigned long lastLedBlink = 0;
bool ledState = false; bool ledState = false;
void setupLED() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // LED aus
}
void updateStatusLED(int blinkPattern) { void updateStatusLED(int blinkPattern) {
unsigned long currentTime = millis(); unsigned long currentTime = millis();

View File

@@ -9,7 +9,7 @@
#include "licenceing.h" #include "licenceing.h"
const char* ssidAP = "AquaCross-Timer"; const char* ssidAP = "AquaCross-Timer";
const char* passwordAP = "aquacross123"; const char* passwordAP = nullptr;
const char* ssidSTA = "Obiwlankenobi"; const char* ssidSTA = "Obiwlankenobi";
const char* passwordSTA = "Delfine1!"; const char* passwordSTA = "Delfine1!";