Heartbead (muss noch angepasst werden)
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
src/debug.h
10
src/debug.h
@@ -7,11 +7,13 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "communication.h"
|
||||
|
||||
|
||||
|
||||
|
||||
void setupDebugAPI(AsyncWebServer& server);
|
||||
|
||||
|
||||
void setupDebugAPI(AsyncWebServer& server) {
|
||||
|
||||
//DEBUG
|
||||
@@ -36,6 +38,12 @@ server.on("/api/debug/stop2", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
});
|
||||
|
||||
|
||||
|
||||
Serial.println("Debug-API initialisiert");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//DEBUG END
|
||||
@@ -31,13 +31,6 @@ void setupRFID() {
|
||||
SPI.begin();
|
||||
mfrc522.PCD_Init();
|
||||
|
||||
|
||||
// Gespeicherte Daten laden
|
||||
loadUsersFromFile();
|
||||
|
||||
// Route Definitionen
|
||||
setupRoutes(AsyncWebServer& server);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "master.h"
|
||||
#include "licenceing.h"
|
||||
|
||||
String uniqueSSID;
|
||||
const char* ssidAP;
|
||||
const char* passwordAP = nullptr;
|
||||
|
||||
@@ -21,7 +22,8 @@ String getUniqueSSID();
|
||||
|
||||
void setupWifi() {
|
||||
|
||||
ssidAP = getUniqueSSID().c_str();
|
||||
uniqueSSID = getUniqueSSID();
|
||||
ssidAP = uniqueSSID.c_str();
|
||||
|
||||
WiFi.mode(WIFI_MODE_APSTA);
|
||||
WiFi.softAP(ssidAP, passwordAP);
|
||||
@@ -37,6 +39,8 @@ void setupWifi() {
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
Serial.println("WiFi AP gestartet");
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.softAPSSID());
|
||||
Serial.print("IP Adresse: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
Serial.println("PrettyOTA can be accessed at: http://" + WiFi.softAPIP().toString() + "/update");
|
||||
|
||||
Reference in New Issue
Block a user