From 91920a679ad7d04541f7b9e1ac12cb3327a1cb09 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:59:13 +0100 Subject: [PATCH] Fix #10 --- CYD-Klipper/platformio.ini | 3 ++- CYD-Klipper/src/core/data_setup.cpp | 6 +++--- CYD-Klipper/src/core/files_query.cpp | 15 ++++++++++----- CYD-Klipper/src/core/macros_query.cpp | 6 +++--- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CYD-Klipper/platformio.ini b/CYD-Klipper/platformio.ini index e756d90..c04f71d 100644 --- a/CYD-Klipper/platformio.ini +++ b/CYD-Klipper/platformio.ini @@ -18,7 +18,8 @@ lib_deps = lvgl/lvgl@^8.3.9 https://github.com/Bodmer/TFT_eSPI.git https://github.com/PaulStoffregen/XPT2046_Touchscreen.git - bblanchon/ArduinoJson@^6.21.3 + bblanchon/ArduinoJson@^7.0.0 +monitor_filters = esp32_exception_decoder build_flags = -DLV_CONF_PATH="../../../../src/conf/lv_conf.h" -DUSER_SETUP_LOADED=1 diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index 82d32f8..28ba10c 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -70,14 +70,14 @@ void fetch_printer_data() char buff[256] = {}; sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks", global_config.klipperHost, global_config.klipperPort); HTTPClient client; + client.useHTTP10(true); client.begin(buff); int httpCode = client.GET(); if (httpCode == 200) { klipper_request_consecutive_fail_count = 0; - String payload = client.getString(); - DynamicJsonDocument doc(4096); - deserializeJson(doc, payload); + JsonDocument doc; + deserializeJson(doc, client.getStream()); auto status = doc["result"]["status"]; bool emit_state_update = false; int printer_state = printer.state; diff --git a/CYD-Klipper/src/core/files_query.cpp b/CYD-Klipper/src/core/files_query.cpp index d6ebb42..fec496c 100644 --- a/CYD-Klipper/src/core/files_query.cpp +++ b/CYD-Klipper/src/core/files_query.cpp @@ -22,24 +22,28 @@ FILESYSTEM_FILE* get_files(){ free(last_query); } - + Serial.printf("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()); std::list files; char buff[256] = {}; sprintf(buff, "http://%s:%d/server/files/list", global_config.klipperHost, global_config.klipperPort); HTTPClient client; + client.useHTTP10(true); client.begin(buff); int httpCode = client.GET(); int count = 0; if (httpCode == 200){ - String payload = client.getString(); - DynamicJsonDocument doc(60000); - auto a = deserializeJson(doc, payload); - Serial.printf("JSON PARSE: %s\n", a.c_str()); + JsonDocument doc; + auto parseResult = deserializeJson(doc, client.getStream()); + Serial.printf("Json parse: %s\n", parseResult.c_str()); auto result = doc["result"].as(); for (auto file : result){ FILESYSTEM_FILE f = {0}; const char* path = file["path"]; f.name = (char*)malloc(strlen(path) + 1); + if (f.name == NULL){ + Serial.println("Failed to allocate memory"); + continue; + } strcpy(f.name, path); f.modified = file["modified"]; files.push_back(f); @@ -62,6 +66,7 @@ FILESYSTEM_FILE* get_files(){ result += 1; } + Serial.printf("Heap space post-file-parse: %d bytes\n", esp_get_free_heap_size()); unfreeze_request_thread(); return last_query; } \ No newline at end of file diff --git a/CYD-Klipper/src/core/macros_query.cpp b/CYD-Klipper/src/core/macros_query.cpp index 11ddef3..909f4fc 100644 --- a/CYD-Klipper/src/core/macros_query.cpp +++ b/CYD-Klipper/src/core/macros_query.cpp @@ -15,12 +15,12 @@ static void on_state_change(void * s, lv_msg_t * m) { String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/gcode/help"; HTTPClient client; + client.useHTTP10(true); client.begin(url.c_str()); int httpCode = client.GET(); if (httpCode == 200){ - String payload = client.getString(); - DynamicJsonDocument doc(16384); - deserializeJson(doc, payload); + JsonDocument doc; + deserializeJson(doc, client.getStream()); auto result = doc["result"].as(); for (int i = 0; i < macros_count; i++){