Heartbead (muss noch angepasst werden)
This commit is contained in:
@@ -31,6 +31,7 @@ lib_deps =
|
|||||||
lostincompilation/PrettyOTA@^1.1.3
|
lostincompilation/PrettyOTA@^1.1.3
|
||||||
esp32async/AsyncTCP@^3.4.2
|
esp32async/AsyncTCP@^3.4.2
|
||||||
mlesniew/PicoMQTT@^1.3.0
|
mlesniew/PicoMQTT@^1.3.0
|
||||||
|
miguelbalboa/MFRC522@^1.4.12
|
||||||
|
|
||||||
[env:wemos_d1_mini32_OTA]
|
[env:wemos_d1_mini32_OTA]
|
||||||
board = wemos_d1_mini32
|
board = wemos_d1_mini32
|
||||||
@@ -41,6 +42,7 @@ lib_deps =
|
|||||||
lostincompilation/PrettyOTA@^1.1.3
|
lostincompilation/PrettyOTA@^1.1.3
|
||||||
esp32async/AsyncTCP@^3.4.2
|
esp32async/AsyncTCP@^3.4.2
|
||||||
mlesniew/PicoMQTT@^1.3.0
|
mlesniew/PicoMQTT@^1.3.0
|
||||||
|
miguelbalboa/MFRC522@^1.4.12
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.1.94
|
upload_port = 192.168.1.94
|
||||||
|
|
||||||
@@ -58,3 +60,4 @@ lib_deps =
|
|||||||
lostincompilation/PrettyOTA@^1.1.3
|
lostincompilation/PrettyOTA@^1.1.3
|
||||||
esp32async/AsyncTCP@^3.4.2
|
esp32async/AsyncTCP@^3.4.2
|
||||||
mlesniew/PicoMQTT@^1.3.0
|
mlesniew/PicoMQTT@^1.3.0
|
||||||
|
miguelbalboa/MFRC522@^1.4.12
|
||||||
|
|||||||
@@ -9,6 +9,18 @@
|
|||||||
#include "timesync.h"
|
#include "timesync.h"
|
||||||
#include "buttonassigh.h"
|
#include "buttonassigh.h"
|
||||||
#include "helper.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
|
// Datenstruktur für ESP-NOW Nachrichten
|
||||||
@@ -23,6 +35,51 @@ typedef struct {
|
|||||||
|
|
||||||
PicoMQTT::Server mqtt;
|
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) {
|
void readButtonJSON(const char * topic, const char * payload) {
|
||||||
|
|
||||||
@@ -76,19 +133,20 @@ void readButtonJSON(const char * topic, const char * payload) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
if (strncmp(topic, "heartbeat/alive/", 15) == 0) {
|
||||||
|
processHeartbeat(topic, payload);
|
||||||
|
} else if (strcmp(topic, "aquacross/button/press") == 0) {
|
||||||
readButtonJSON(topic, payload);
|
readButtonJSON(topic, payload);
|
||||||
updateStatusLED(3); // Flash LED on message received
|
}
|
||||||
|
updateStatusLED(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Add the button subscription
|
// Add the button subscription
|
||||||
|
|
||||||
// Start the MQTT server
|
// Start the MQTT server
|
||||||
@@ -103,7 +161,7 @@ void loopMqttServer() {
|
|||||||
mqtt.loop();
|
mqtt.loop();
|
||||||
|
|
||||||
static unsigned long lastPublish = 0;
|
static unsigned long lastPublish = 0;
|
||||||
if (millis() - lastPublish > 30000) {
|
if (millis() - lastPublish > 5000) {
|
||||||
// Convert timestamp to string before publishing
|
// Convert timestamp to string before publishing
|
||||||
char timeStr[32];
|
char timeStr[32];
|
||||||
snprintf(timeStr, sizeof(timeStr), "%llu", getCurrentTimestampMs());
|
snprintf(timeStr, sizeof(timeStr), "%llu", getCurrentTimestampMs());
|
||||||
@@ -128,3 +186,4 @@ void sendMQTTJSONMessage(const char * topic, const JsonDocument & doc) {
|
|||||||
publish.send();
|
publish.send();
|
||||||
Serial.printf("Published JSON message to topic '%s': %s\n", topic, jsonString.c_str());
|
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 <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#include "communication.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setupDebugAPI(AsyncWebServer& server);
|
void setupDebugAPI(AsyncWebServer& server);
|
||||||
|
|
||||||
|
|
||||||
void setupDebugAPI(AsyncWebServer& server) {
|
void setupDebugAPI(AsyncWebServer& server) {
|
||||||
|
|
||||||
//DEBUG
|
//DEBUG
|
||||||
@@ -36,6 +38,12 @@ server.on("/api/debug/stop2", HTTP_GET, [](AsyncWebServerRequest *request){
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Debug-API initialisiert");
|
Serial.println("Debug-API initialisiert");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//DEBUG END
|
//DEBUG END
|
||||||
@@ -32,13 +32,6 @@ void setupRFID() {
|
|||||||
mfrc522.PCD_Init();
|
mfrc522.PCD_Init();
|
||||||
|
|
||||||
|
|
||||||
// Gespeicherte Daten laden
|
|
||||||
loadUsersFromFile();
|
|
||||||
|
|
||||||
// Route Definitionen
|
|
||||||
setupRoutes(AsyncWebServer& server);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "master.h"
|
#include "master.h"
|
||||||
#include "licenceing.h"
|
#include "licenceing.h"
|
||||||
|
|
||||||
|
String uniqueSSID;
|
||||||
const char* ssidAP;
|
const char* ssidAP;
|
||||||
const char* passwordAP = nullptr;
|
const char* passwordAP = nullptr;
|
||||||
|
|
||||||
@@ -21,7 +22,8 @@ String getUniqueSSID();
|
|||||||
|
|
||||||
void setupWifi() {
|
void setupWifi() {
|
||||||
|
|
||||||
ssidAP = getUniqueSSID().c_str();
|
uniqueSSID = getUniqueSSID();
|
||||||
|
ssidAP = uniqueSSID.c_str();
|
||||||
|
|
||||||
WiFi.mode(WIFI_MODE_APSTA);
|
WiFi.mode(WIFI_MODE_APSTA);
|
||||||
WiFi.softAP(ssidAP, passwordAP);
|
WiFi.softAP(ssidAP, passwordAP);
|
||||||
@@ -37,6 +39,8 @@ void setupWifi() {
|
|||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
Serial.println("WiFi AP gestartet");
|
Serial.println("WiFi AP gestartet");
|
||||||
|
Serial.print("SSID: ");
|
||||||
|
Serial.println(WiFi.softAPSSID());
|
||||||
Serial.print("IP Adresse: ");
|
Serial.print("IP Adresse: ");
|
||||||
Serial.println(WiFi.softAPIP());
|
Serial.println(WiFi.softAPIP());
|
||||||
Serial.println("PrettyOTA can be accessed at: http://" + WiFi.softAPIP().toString() + "/update");
|
Serial.println("PrettyOTA can be accessed at: http://" + WiFi.softAPIP().toString() + "/update");
|
||||||
|
|||||||
Reference in New Issue
Block a user