diff --git a/platformio.ini b/platformio.ini index b8d08c0..8b70908 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,7 @@ lib_deps = lostincompilation/PrettyOTA@^1.1.3 esp32async/AsyncTCP@^3.4.2 mlesniew/PicoMQTT@^1.3.0 + miguelbalboa/MFRC522@^1.4.12 [env:wemos_d1_mini32_OTA] board = wemos_d1_mini32 @@ -41,6 +42,7 @@ lib_deps = lostincompilation/PrettyOTA@^1.1.3 esp32async/AsyncTCP@^3.4.2 mlesniew/PicoMQTT@^1.3.0 + miguelbalboa/MFRC522@^1.4.12 upload_protocol = espota upload_port = 192.168.1.94 @@ -58,3 +60,4 @@ lib_deps = lostincompilation/PrettyOTA@^1.1.3 esp32async/AsyncTCP@^3.4.2 mlesniew/PicoMQTT@^1.3.0 + miguelbalboa/MFRC522@^1.4.12 diff --git a/src/communication.h b/src/communication.h index 141d917..207824d 100644 --- a/src/communication.h +++ b/src/communication.h @@ -9,6 +9,18 @@ #include "timesync.h" #include "buttonassigh.h" #include "helper.h" +#include +#include + +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 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()); -} \ No newline at end of file +} + diff --git a/src/debug.h b/src/debug.h index 508aff5..0607629 100644 --- a/src/debug.h +++ b/src/debug.h @@ -7,11 +7,13 @@ #include #include +#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 \ No newline at end of file diff --git a/src/rfid.h b/src/rfid.h index bdbc877..ae355b2 100644 --- a/src/rfid.h +++ b/src/rfid.h @@ -31,13 +31,6 @@ void setupRFID() { SPI.begin(); mfrc522.PCD_Init(); - - // Gespeicherte Daten laden - loadUsersFromFile(); - - // Route Definitionen - setupRoutes(AsyncWebServer& server); - } diff --git a/src/wificlass.h b/src/wificlass.h index 1e95c6b..a06eb80 100644 --- a/src/wificlass.h +++ b/src/wificlass.h @@ -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");