MQTT messages. Button anleren, Wifi frontend

This commit is contained in:
Carsten Graf
2025-06-05 23:34:11 +02:00
parent 3b4f63f072
commit e0d3031d6f
10 changed files with 269 additions and 114 deletions

View File

@@ -1,3 +1,4 @@
#pragma once
#include <Arduino.h>
#include "master.h"
#include <ArduinoJson.h>
@@ -6,6 +7,8 @@
#include <statusled.h>
#include "timesync.h"
#include "buttonassigh.h"
#include "helper.h"
// Datenstruktur für ESP-NOW Nachrichten
@@ -19,31 +22,110 @@ typedef struct {
} ButtonMessage;
PicoMQTT::Server mqtt;
PicoMQTT::ServerLocalSubscribe localsubscribe;
void readButtonJSON(const char * topic, const char * payload) {
if(strcmp(topic, "aquacross/button/press") == 0){
// Create a JSON document to parse the incoming message
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("JSON parsing failed: ");
Serial.println(error.c_str());
return;
}
// Extract values from JSON
int pressType = doc["type"] | 0;
const char* buttonId = doc["buttonmac"] | "unknown";
const char* messageId = doc["messageId"] | "unknown";
uint64_t timestamp = doc["timestamp"] | 0;
// Print received data
Serial.printf("Button Press Received:\n");
Serial.printf(" Type: %d\n", pressType);
Serial.printf(" Button MAC: %s\n", buttonId);
Serial.printf(" Message ID: %s\n", messageId);
Serial.printf(" Timestamp: %llu\n", timestamp);
auto macBytes = macStringToBytes(buttonId);
if (learningMode) {
handleLearningMode(macBytes.data());
return;
}
// Button-Zuordnung prüfen und entsprechende Aktion ausführen
if (memcmp(macBytes.data(), buttonConfigs.start1.mac, 6) == 0 && (pressType == 2)) {
handleStart1();
} else if (memcmp(macBytes.data(), buttonConfigs.stop1.mac, 6) == 0 && (pressType == 2)) {
handleStop1();
} else if (memcmp(macBytes.data(), buttonConfigs.start2.mac, 6) == 0 && (pressType == 2)) {
handleStart2();
} else if (memcmp(macBytes.data(), buttonConfigs.stop2.mac, 6) == 0 && (pressType == 2)) {
handleStop2();
}
// Flash status LED to indicate received message
updateStatusLED(3);
}
}
void setupMqttServer() {
// Set up the MQTT server with the desired port
// Subscribe to a topic pattern and attach a callback
mqtt.subscribe("#", [](const char * topic, const char * payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload);
updateStatusLED(3); // Flash LED on message received
});
Serial.printf("Received message in topic '%s': %s\n", topic, payload);
readButtonJSON(topic, payload);
updateStatusLED(3); // Flash LED on message received
});
// Add the button subscription
// Start the MQTT server
mqtt.begin();
Serial.println("MQTT server started on port 1883");
}
void loopMqttServer() {
// Handle incoming MQTT messages
mqtt.loop();
// Optionally, you can publish a message periodically
static unsigned long lastPublish = 0;
if (millis() - lastPublish > 5000) { // Publish every 5 seconds
mqtt.publish("heartbeat/live", "Alive!");
lastPublish = millis();
}
mqtt.loop();
static unsigned long lastPublish = 0;
if (millis() - lastPublish > 30000) {
// Convert timestamp to string before publishing
char timeStr[32];
snprintf(timeStr, sizeof(timeStr), "%llu", getCurrentTimestampMs());
mqtt.publish("sync/time", timeStr);
lastPublish = millis();
}
}
void sendMQTTMessage(const char * topic, const char * message) {
// Publish a message to the specified topic
mqtt.publish(topic, message);
Serial.printf("Published message to topic '%s': %s\n", topic, message);
}
void sendMQTTJSONMessage(const char * topic, const JsonDocument & doc) {
String jsonString;
serializeJson(doc, jsonString);
// Publish the JSON string to the specified topic
auto publish = mqtt.begin_publish(topic, measureJson(doc));
serializeJson(doc, publish);
publish.send();
Serial.printf("Published JSON message to topic '%s': %s\n", topic, jsonString.c_str());
}