Heartbead (muss noch angepasst werden)

This commit is contained in:
Carsten Graf
2025-06-07 12:21:09 +02:00
parent 01750f4347
commit e5c4094cfa
5 changed files with 90 additions and 23 deletions

View File

@@ -9,6 +9,18 @@
#include "timesync.h"
#include "buttonassigh.h"
#include "helper.h"
#include <debug.h>
#include <map>
struct TimestampData {
uint64_t lastMessageTimestamp; // Timestamp from the device
uint64_t lastLocalTimestamp; // Our local timestamp when message was received
uint64_t drift; // Calculated drift
};
// Map to store timestamp data for each MAC address
std::map<String, TimestampData> deviceTimestamps;
// Datenstruktur für ESP-NOW Nachrichten
@@ -23,6 +35,51 @@ typedef struct {
PicoMQTT::Server mqtt;
void processHeartbeat(const char* topic, const char* payload) {
String macAddress = String(topic).substring(15);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.printf("JSON parsing failed for MAC %s: %s\n", macAddress.c_str(), error.c_str());
return;
}
uint64_t messageTimestamp = doc["timestamp"] | 0;
uint64_t currentLocalTime = getCurrentTimestampMs();
// Update timestamps for current device
if (deviceTimestamps.count(macAddress) > 0) {
TimestampData& data = deviceTimestamps[macAddress];
uint64_t messageDiff = messageTimestamp - data.lastMessageTimestamp;
uint64_t localDiff = currentLocalTime - data.lastLocalTimestamp;
data.drift = localDiff - messageDiff;
}
// Calculate drift relative to all other devices
Serial.printf("\nDrift analysis for device %s:\n", macAddress.c_str());
Serial.println("----------------------------------------");
for (const auto& device : deviceTimestamps) {
if (device.first != macAddress) { // Skip comparing to self
int64_t timeDiff = messageTimestamp - device.second.lastMessageTimestamp;
int64_t relativeDrift = timeDiff - (currentLocalTime - device.second.lastLocalTimestamp);
Serial.printf("Relative to %s:\n", device.first.c_str());
Serial.printf(" Time difference: %lld ms\n", timeDiff);
Serial.printf(" Relative drift: %lld ms\n", relativeDrift);
Serial.println("----------------------------------------");
}
}
// Update stored timestamps for current device
deviceTimestamps[macAddress] = {
messageTimestamp,
currentLocalTime,
deviceTimestamps[macAddress].drift
};
}
void readButtonJSON(const char * topic, const char * payload) {
@@ -76,25 +133,26 @@ void readButtonJSON(const char * topic, const char * payload) {
}
}
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);
readButtonJSON(topic, payload);
updateStatusLED(3); // Flash LED on message received
});
// Add the button subscription
if (strncmp(topic, "heartbeat/alive/", 15) == 0) {
processHeartbeat(topic, payload);
} else if (strcmp(topic, "aquacross/button/press") == 0) {
readButtonJSON(topic, payload);
}
updateStatusLED(3);
});
// Start the MQTT server
mqtt.begin();
// Add the button subscription
// Start the MQTT server
mqtt.begin();
Serial.println("MQTT server started on port 1883");
Serial.println("MQTT server started on port 1883");
}
@@ -103,7 +161,7 @@ void loopMqttServer() {
mqtt.loop();
static unsigned long lastPublish = 0;
if (millis() - lastPublish > 30000) {
if (millis() - lastPublish > 5000) {
// Convert timestamp to string before publishing
char timeStr[32];
snprintf(timeStr, sizeof(timeStr), "%llu", getCurrentTimestampMs());
@@ -127,4 +185,5 @@ void sendMQTTJSONMessage(const char * topic, const JsonDocument & doc) {
serializeJson(doc, publish);
publish.send();
Serial.printf("Published JSON message to topic '%s': %s\n", topic, jsonString.c_str());
}
}