Merge pull request #41 from janecker/memory_leak_fixes
Memory leak fixes
This commit is contained in:
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
.pio
|
||||||
|
.vscode/.browse.c_cpp.db*
|
||||||
|
.vscode/c_cpp_properties.json
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/ipch
|
||||||
|
.vscode/extensions.json
|
||||||
|
.vscode/settings.json
|
||||||
|
data
|
@@ -52,6 +52,7 @@ build_flags =
|
|||||||
-mtext-section-literals
|
-mtext-section-literals
|
||||||
-DVERSION=\"${common.version}\"
|
-DVERSION=\"${common.version}\"
|
||||||
-DTOOLDVERSION=\"${common.to_old_version}\"
|
-DTOOLDVERSION=\"${common.to_old_version}\"
|
||||||
|
#-DENABLE_HEAP_DEBUGGING
|
||||||
-DASYNCWEBSERVER_REGEX
|
-DASYNCWEBSERVER_REGEX
|
||||||
#-DCORE_DEBUG_LEVEL=3
|
#-DCORE_DEBUG_LEVEL=3
|
||||||
-DCONFIG_ARDUHAL_LOG_COLORS=1
|
-DCONFIG_ARDUHAL_LOG_COLORS=1
|
||||||
|
26
src/api.cpp
26
src/api.cpp
@@ -2,6 +2,7 @@
|
|||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "commonFS.h"
|
#include "commonFS.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
volatile spoolmanApiStateType spoolmanApiState = API_INIT;
|
volatile spoolmanApiStateType spoolmanApiState = API_INIT;
|
||||||
//bool spoolman_connected = false;
|
//bool spoolman_connected = false;
|
||||||
@@ -87,6 +88,8 @@ JsonDocument fetchSingleSpoolInfo(int spoolId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sendToApi(void *parameter) {
|
void sendToApi(void *parameter) {
|
||||||
|
HEAP_DEBUG_MESSAGE("sendToApi begin");
|
||||||
|
|
||||||
spoolmanApiState = API_TRANSMITTING;
|
spoolmanApiState = API_TRANSMITTING;
|
||||||
SendToApiParams* params = (SendToApiParams*)parameter;
|
SendToApiParams* params = (SendToApiParams*)parameter;
|
||||||
|
|
||||||
@@ -131,8 +134,8 @@ void sendToApi(void *parameter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||||
doc.clear();
|
|
||||||
}
|
}
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
||||||
@@ -145,8 +148,9 @@ void sendToApi(void *parameter) {
|
|||||||
|
|
||||||
// Speicher freigeben
|
// Speicher freigeben
|
||||||
delete params;
|
delete params;
|
||||||
vTaskDelete(NULL);
|
HEAP_DEBUG_MESSAGE("sendToApi end");
|
||||||
spoolmanApiState = API_IDLE;
|
spoolmanApiState = API_IDLE;
|
||||||
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateSpoolTagId(String uidString, const char* payload) {
|
bool updateSpoolTagId(String uidString, const char* payload) {
|
||||||
@@ -169,6 +173,8 @@ bool updateSpoolTagId(String uidString, const char* payload) {
|
|||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
// Update Payload erstellen
|
// Update Payload erstellen
|
||||||
JsonDocument updateDoc;
|
JsonDocument updateDoc;
|
||||||
updateDoc["extra"]["nfc_id"] = "\""+uidString+"\"";
|
updateDoc["extra"]["nfc_id"] = "\""+uidString+"\"";
|
||||||
@@ -207,6 +213,7 @@ bool updateSpoolTagId(String uidString, const char* payload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolWeight begin");
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId + "/measure";
|
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId + "/measure";
|
||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
@@ -241,11 +248,14 @@ uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
updateDoc.clear();
|
updateDoc.clear();
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolWeight end");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t updateSpoolLocation(String spoolId, String location){
|
uint8_t updateSpoolLocation(String spoolId, String location){
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolLocation begin");
|
||||||
|
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId;
|
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId;
|
||||||
Serial.print("Update Spule mit URL: ");
|
Serial.print("Update Spule mit URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
@@ -281,6 +291,7 @@ uint8_t updateSpoolLocation(String spoolId, String location){
|
|||||||
|
|
||||||
updateDoc.clear();
|
updateDoc.clear();
|
||||||
|
|
||||||
|
HEAP_DEBUG_MESSAGE("updateSpoolLocation end");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,6 +356,10 @@ bool updateSpoolBambuData(String payload) {
|
|||||||
|
|
||||||
String updatePayload;
|
String updatePayload;
|
||||||
serializeJson(updateDoc, updatePayload);
|
serializeJson(updateDoc, updatePayload);
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
updateDoc.clear();
|
||||||
|
|
||||||
Serial.print("Update Payload: ");
|
Serial.print("Update Payload: ");
|
||||||
Serial.println(updatePayload);
|
Serial.println(updatePayload);
|
||||||
|
|
||||||
@@ -511,6 +526,7 @@ bool checkSpoolmanExtraFields() {
|
|||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
doc.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,6 +573,8 @@ bool checkSpoolmanInstance(const String& url) {
|
|||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
return strcmp(status, "healthy") == 0;
|
return strcmp(status, "healthy") == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
|
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
|
||||||
@@ -584,6 +602,8 @@ bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octoWh, const
|
|||||||
octoUrl = octoWh;
|
octoUrl = octoWh;
|
||||||
octoToken = octoTk;
|
octoToken = octoTk;
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,6 +620,8 @@ String loadSpoolmanUrl() {
|
|||||||
return doc["url"].as<String>();
|
return doc["url"].as<String>();
|
||||||
}
|
}
|
||||||
Serial.println("Keine gültige Spoolman-URL gefunden.");
|
Serial.println("Keine gültige Spoolman-URL gefunden.");
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
src/debug.h
Normal file
12
src/debug.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef ENABLE_HEAP_DEBUGGING
|
||||||
|
#define HEAP_DEBUG_MESSAGE(location) printHeapDebugData(location);
|
||||||
|
#else
|
||||||
|
#define HEAP_DEBUG_MESSAGE(location)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline void printHeapDebugData(String location){
|
||||||
|
Serial.println("Heap: " + String(ESP.getMinFreeHeap()/1024) + "\t" + String(ESP.getFreeHeap()/1024) + "\t" + String(ESP.getMaxAllocHeap()/1024) + "\t" + location);
|
||||||
|
}
|
@@ -212,14 +212,14 @@ void loop() {
|
|||||||
lastWeight = weight;
|
lastWeight = weight;
|
||||||
|
|
||||||
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
||||||
if (spoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS) {
|
if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS) {
|
||||||
oledShowIcon("loading");
|
oledShowIcon("loading");
|
||||||
if (updateSpoolWeight(spoolId, weight))
|
if (updateSpoolWeight(activeSpoolId, weight))
|
||||||
{
|
{
|
||||||
oledShowIcon("success");
|
oledShowIcon("success");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
weightSend = 1;
|
weightSend = 1;
|
||||||
autoSetToBambuSpoolId = spoolId.toInt();
|
autoSetToBambuSpoolId = activeSpoolId.toInt();
|
||||||
|
|
||||||
if (octoEnabled)
|
if (octoEnabled)
|
||||||
{
|
{
|
||||||
|
21
src/nfc.cpp
21
src/nfc.cpp
@@ -14,7 +14,8 @@ Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
|
|||||||
TaskHandle_t RfidReaderTask;
|
TaskHandle_t RfidReaderTask;
|
||||||
|
|
||||||
JsonDocument rfidData;
|
JsonDocument rfidData;
|
||||||
String spoolId = "";
|
String activeSpoolId = "";
|
||||||
|
String lastSpoolId = "";
|
||||||
String nfcJsonData = "";
|
String nfcJsonData = "";
|
||||||
volatile bool pauseBambuMqttTask = false;
|
volatile bool pauseBambuMqttTask = false;
|
||||||
|
|
||||||
@@ -64,6 +65,8 @@ void payloadToJson(uint8_t *data) {
|
|||||||
Serial.print("deserializeJson() failed: ");
|
Serial.print("deserializeJson() failed: ");
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Kein gültiger JSON-Inhalt gefunden oder fehlerhafte Formatierung.");
|
Serial.println("Kein gültiger JSON-Inhalt gefunden oder fehlerhafte Formatierung.");
|
||||||
//writeJsonToTag("{\"version\":\"1.0\",\"protocol\":\"NFC\",\"color_hex\":\"#FFFFFF\",\"type\":\"Example\",\"min_temp\":10,\"max_temp\":30,\"brand\":\"BrandName\"}");
|
//writeJsonToTag("{\"version\":\"1.0\",\"protocol\":\"NFC\",\"color_hex\":\"#FFFFFF\",\"type\":\"Example\",\"min_temp\":10,\"max_temp\":30,\"brand\":\"BrandName\"}");
|
||||||
@@ -218,17 +221,18 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
|||||||
// Sende die aktualisierten AMS-Daten an alle WebSocket-Clients
|
// Sende die aktualisierten AMS-Daten an alle WebSocket-Clients
|
||||||
Serial.println("JSON-Dokument erfolgreich verarbeitet");
|
Serial.println("JSON-Dokument erfolgreich verarbeitet");
|
||||||
Serial.println(doc.as<String>());
|
Serial.println(doc.as<String>());
|
||||||
if (doc.containsKey("sm_id") && doc["sm_id"] != "")
|
if (doc["sm_id"].is<String>() && doc["sm_id"] != "")
|
||||||
{
|
{
|
||||||
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
|
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
|
||||||
spoolId = doc["sm_id"].as<String>();
|
activeSpoolId = doc["sm_id"].as<String>();
|
||||||
|
lastSpoolId = activeSpoolId;
|
||||||
}
|
}
|
||||||
else if(doc.containsKey("location") && doc["location"] != "")
|
else if(doc["location"].is<String>() && doc["location"] != "")
|
||||||
{
|
{
|
||||||
Serial.println("Location Tag found!");
|
Serial.println("Location Tag found!");
|
||||||
String location = doc["location"].as<String>();
|
String location = doc["location"].as<String>();
|
||||||
if(spoolId != ""){
|
if(lastSpoolId != ""){
|
||||||
updateSpoolLocation(spoolId, location);
|
updateSpoolLocation(lastSpoolId, location);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -240,12 +244,14 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Keine SPOOL-ID gefunden.");
|
Serial.println("Keine SPOOL-ID gefunden.");
|
||||||
spoolId = "";
|
activeSpoolId = "";
|
||||||
oledShowMessage("Unknown Spool");
|
oledShowMessage("Unknown Spool");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,6 +452,7 @@ void scanRfidTask(void * parameter) {
|
|||||||
nfcReaderState = NFC_IDLE;
|
nfcReaderState = NFC_IDLE;
|
||||||
//uidString = "";
|
//uidString = "";
|
||||||
nfcJsonData = "";
|
nfcJsonData = "";
|
||||||
|
activeSpoolId = "";
|
||||||
Serial.println("Tag entfernt");
|
Serial.println("Tag entfernt");
|
||||||
if (!autoSendToBambu) oledShowWeight(weight);
|
if (!autoSendToBambu) oledShowWeight(weight);
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,8 @@ void startWriteJsonToTag(const char* payload);
|
|||||||
|
|
||||||
extern TaskHandle_t RfidReaderTask;
|
extern TaskHandle_t RfidReaderTask;
|
||||||
extern String nfcJsonData;
|
extern String nfcJsonData;
|
||||||
extern String spoolId;
|
extern String activeSpoolId;
|
||||||
|
extern String lastSpoolId;
|
||||||
extern volatile nfcReaderStateType nfcReaderState;
|
extern volatile nfcReaderStateType nfcReaderState;
|
||||||
extern volatile bool pauseBambuMqttTask;
|
extern volatile bool pauseBambuMqttTask;
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
#include <Update.h>
|
#include <Update.h>
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
#ifndef VERSION
|
#ifndef VERSION
|
||||||
#define VERSION "1.1.0"
|
#define VERSION "1.1.0"
|
||||||
@@ -26,6 +27,7 @@ nfcReaderStateType lastnfcReaderState = NFC_IDLE;
|
|||||||
|
|
||||||
|
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||||
|
HEAP_DEBUG_MESSAGE("onWsEvent begin");
|
||||||
if (type == WS_EVT_CONNECT) {
|
if (type == WS_EVT_CONNECT) {
|
||||||
Serial.println("Neuer Client verbunden!");
|
Serial.println("Neuer Client verbunden!");
|
||||||
// Sende die AMS-Daten an den neuen Client
|
// Sende die AMS-Daten an den neuen Client
|
||||||
@@ -33,6 +35,10 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
sendNfcData(client);
|
sendNfcData(client);
|
||||||
foundNfcTag(client, 0);
|
foundNfcTag(client, 0);
|
||||||
sendWriteResult(client, 3);
|
sendWriteResult(client, 3);
|
||||||
|
|
||||||
|
// Clean up dead connections
|
||||||
|
(*server).cleanupClients();
|
||||||
|
Serial.println("Currently connected number of clients: " + String((*server).getClients().size()));
|
||||||
} else if (type == WS_EVT_DISCONNECT) {
|
} else if (type == WS_EVT_DISCONNECT) {
|
||||||
Serial.println("Client getrennt.");
|
Serial.println("Client getrennt.");
|
||||||
} else if (type == WS_EVT_ERROR) {
|
} else if (type == WS_EVT_ERROR) {
|
||||||
@@ -113,7 +119,9 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
else {
|
else {
|
||||||
Serial.println("Unbekannter WebSocket-Typ: " + doc["type"].as<String>());
|
Serial.println("Unbekannter WebSocket-Typ: " + doc["type"].as<String>());
|
||||||
}
|
}
|
||||||
|
doc.clear();
|
||||||
}
|
}
|
||||||
|
HEAP_DEBUG_MESSAGE("onWsEvent end");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Funktion zum Laden und Ersetzen des Headers in einer HTML-Datei
|
// Funktion zum Laden und Ersetzen des Headers in einer HTML-Datei
|
||||||
@@ -280,6 +288,8 @@ void setupWebserver(AsyncWebServer &server) {
|
|||||||
html.replace("{{autoSendTime}}", String(autoSetBambuAmsCounter));
|
html.replace("{{autoSendTime}}", String(autoSetBambuAmsCounter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc.clear();
|
||||||
|
|
||||||
request->send(200, "text/html", html);
|
request->send(200, "text/html", html);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user