diff --git a/nspanel_esphome.yaml b/nspanel_esphome.yaml index 21669dd..2678639 100644 --- a/nspanel_esphome.yaml +++ b/nspanel_esphome.yaml @@ -53,7 +53,7 @@ esphome: - lambda: !lambda return id(disp1).is_setup(); timeout: 20s - lambda: |- - if (not id(tf_uart).available() and not id(disp1).is_setup()) + if (not id(tf_uart).available() or not id(disp1).is_setup()) { ESP_LOGE("on_boot", "No response from Nextion display"); ESP_LOGD("on_boot", "Turn off Nextion"); @@ -2256,9 +2256,9 @@ script: url: string then: - lambda: |- - ESP_LOGD("script.upload_tft", "Starting..."); + static const char *const TAG = "script.upload_tft"; + ESP_LOGD(TAG, "Starting..."); - #ifdef ARDUINO std::vector buffer_; bool is_updating_ = false; @@ -2272,7 +2272,8 @@ script: auto send_nextion_command = [](const std::string &command) -> bool { - ESP_LOGD("script.upload_tft.send_nextion_command", "Sending: %s", command.c_str()); + static const char *const TAG = "script.upload_tft.send_nextion_command"; + ESP_LOGD(TAG, "Sending: %s", command.c_str()); id(tf_uart).write_str(command.c_str()); const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF}; id(tf_uart).write_array(to_send, sizeof(to_send)); @@ -2281,6 +2282,7 @@ script: auto recv_ret_string_ = [](std::string &response, uint32_t timeout, bool recv_flag) -> uint16_t { + static const char *const TAG = "script.upload_tft.recv_ret_string_"; uint16_t ret; uint8_t c = 0; uint8_t nr_of_ff_bytes = 0; @@ -2338,15 +2340,20 @@ script: auto upload_end_ = [&](bool retry) -> bool { - ESP_LOGD("script.upload_tft.upload_end_", "Restarting Nextion"); + static const char *const TAG = "script.upload_tft.upload_end_"; + ESP_LOGD(TAG, "Restarting Nextion"); send_nextion_command("rest"); if (is_updating_) is_updating_ = not retry; - if (retry) ESP_LOGD("script.upload_tft.upload_end_", "Nextion TFT upload will try again"); + if (retry) ESP_LOGD(TAG, "Nextion TFT upload will try again"); return not retry; }; - auto upload_by_chunks_ = [&](HTTPClient *http, const std::string &url, int range_start) -> int + + #ifdef ARDUINO + + auto upload_by_chunks_arduino = [&](HTTPClient *http, const std::string &url, int range_start) -> int { + static const char *const TAG = "script.upload_tft.upload_by_chunks_arduino"; int range_end; if (range_start == 0 && transfer_buffer_size_ > 16384) { // Start small at the first run in case of a big skip @@ -2361,7 +2368,7 @@ script: char range_header[64]; sprintf(range_header, "bytes=%d-%d", range_start, range_end); - ESP_LOGD("script.upload_tft.upload_by_chunks", "Requesting range: %s", range_header); + ESP_LOGD(TAG, "Requesting range: %s", range_header); int tries = 1; int code; @@ -2371,7 +2378,7 @@ script: ++tries; if (!begin_status) { - ESP_LOGD("script.upload_tft.upload_by_chunks", "Connection failed"); + ESP_LOGD(TAG, "Connection failed"); delay(1000); continue; }; @@ -2382,7 +2389,7 @@ script: if (code == 200 || code == 206) { break; } - ESP_LOGW("script.upload_tft.upload_by_chunks", "HTTP Request failed; URL: %s; Error: %s, retries(%d/10)", url.c_str(), + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s, retries(%d/10)", url.c_str(), HTTPClient::errorToString(code).c_str(), tries); http->end(); delay(1000); @@ -2411,7 +2418,7 @@ script: fetched += c; } http->end(); - ESP_LOGD("script.upload_tft.upload_by_chunks", "Fetched %d bytes", fetched); + ESP_LOGD(TAG, "Fetched %d bytes", fetched); // upload fetched segments to the display in 4KB chunks for (int i = 0; i < range; i += 4096) { @@ -2419,7 +2426,7 @@ script: write_len = content_length_ < 4096 ? content_length_ : 4096; id(tf_uart).write_array(&transfer_buffer_[i], write_len); content_length_ -= write_len; - ESP_LOGD("script.upload_tft.upload_by_chunks", "Uploaded %0.1f %%, remaining %d bytes", + ESP_LOGD(TAG, "Uploaded %0.1f %%, remaining %d bytes", 100.0 * (tft_size_ - content_length_) / tft_size_, content_length_); @@ -2430,7 +2437,7 @@ script: recv_ret_string_(recv_string, 5000, true); if (recv_string[0] != 0x05) { // 0x05 == "ok" - ESP_LOGD("script.upload_tft.upload_by_chunks", "recv_string [%s]", + ESP_LOGD(TAG, "recv_string [%s]", format_hex_pretty(reinterpret_cast(recv_string.data()), recv_string.size()).c_str()); } @@ -2441,7 +2448,7 @@ script: result += static_cast(recv_string[j + 1]) << (8 * j); } if (result > 0) { - ESP_LOGD("script.upload_tft.upload_by_chunks", "Nextion reported new range %d", result); + ESP_LOGD(TAG, "Nextion reported new range %d", result); content_length_ = tft_size_ - result; return result; } @@ -2452,7 +2459,7 @@ script: return range_end + 1; }; - auto upload_tft = [&](const std::string &url, unsigned int update_baud_rate_) -> bool + auto upload_tft_arduino = [&](const std::string &url, unsigned int update_baud_rate_) -> bool { ESP_LOGD("script.upload_tft.upload_tft", "Nextion TFT upload requested"); ESP_LOGD("script.upload_tft.upload_tft", "url: %s", url.c_str()); @@ -2594,7 +2601,7 @@ script: int result = 0; while (content_length_ > 0) { - result = upload_by_chunks_(&http, url, result); + result = upload_by_chunks_arduino(&http, url, result); if (result < 0) { ESP_LOGD("script.upload_tft.upload_tft", "Error updating Nextion!"); return upload_end_(true); @@ -2608,35 +2615,330 @@ script: return upload_end_(false); }; - if (upload_tft(url, id(tf_uart).get_baud_rate())) id(restart_nspanel).press(); - ESP_LOGD("script.upload_tft", "Turn off Nextion"); - id(screen_power).turn_off(); - delay(1500); - ESP_LOGD("script.upload_tft", "Turn on Nextion"); - id(screen_power).turn_on(); - delay(1500); + unsigned int upload_tries = 0; + while (upload_tries < 2) { + upload_tries++; + if (upload_tft_arduino(url, id(tf_uart).get_baud_rate())) id(restart_nspanel).press(); + ESP_LOGD(TAG, "Turn off Nextion"); + id(screen_power).turn_off(); + delay(1500); + ESP_LOGD(TAG, "Turn on Nextion"); + id(screen_power).turn_on(); + delay(1500); + } unsigned int new_baud_rate; if (id(tf_uart).get_baud_rate() == 115200) new_baud_rate = 921600; else new_baud_rate = 115200; - ESP_LOGD("script.upload_tft", "Trying again at %i bps", new_baud_rate); - if (upload_tft(url, new_baud_rate)) id(restart_nspanel).press(); - #elif defined(ESP_PLATFORM) // esp-idf - // ESP-IDF-specific code - #endif - ESP_LOGE("script.upload_tft", "TFT upload failed."); - ESP_LOGD("script.upload_tft", "Turn off Nextion"); + ESP_LOGD(TAG, "Trying again at %i bps", new_baud_rate); + if (upload_tft_arduino(url, new_baud_rate)) id(restart_nspanel).press(); + ESP_LOGE(TAG, "TFT upload failed."); + ESP_LOGD(TAG, "Turn off Nextion"); id(screen_power).turn_off(); delay(1500); - //ESP_LOGD("script.upload_tft", "Turn on Nextion"); + + #elif defined(ESP_PLATFORM) // esp-idf + // ESP-IDF-specific code + + auto upload_by_chunks_esp_idf = [&](const std::string &url, int range_start) -> int + { + int range_end; + + if (range_start == 0 && transfer_buffer_size_ > 16384) { + range_end = 16384 - 1; + } else { + range_end = range_start + transfer_buffer_size_ - 1; + } + + if (range_end > tft_size_) + range_end = tft_size_; + + char range_header[64]; + sprintf(range_header, "bytes=%d-%d", range_start, range_end); + ESP_LOGD(TAG, "Requesting range: %s", range_header); + + esp_http_client_config_t config = { + .url = url.c_str(), + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + int tries = 1; + int status; + + while (tries <= 10) { + esp_http_client_set_header(client, "Range", range_header); + status = esp_http_client_perform(client); + + if (status == ESP_OK && (esp_http_client_get_status_code(client) == 200 || esp_http_client_get_status_code(client) == 206)) { + break; + } + + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %d, retries(%d/10)", url.c_str(), status, tries); + tries++; + delay(1000); + } + + if (tries > 10) { + esp_http_client_cleanup(client); + return -1; + } + + std::string recv_string; + size_t size; + int fetched = 0; + int range = range_end - range_start; + int write_len; + while (fetched < range) { + int size = esp_http_client_get_content_length(client); + if (!size) { + delay(2); + continue; + } + int c = esp_http_client_read(client, reinterpret_cast(&transfer_buffer_[fetched]), size); + fetched += c; + } + + ESP_LOGD(TAG, "Fetched %d bytes", fetched); + esp_http_client_cleanup(client); + + // upload fetched segments to the display in 4KB chunks + for (int i = 0; i < range; i += 4096) { + App.feed_wdt(); + write_len = content_length_ < 4096 ? content_length_ : 4096; + id(tf_uart).write_array(&transfer_buffer_[i], write_len); + content_length_ -= write_len; + ESP_LOGD(TAG, "Uploaded %0.1f %%, remaining %d bytes", + 100.0 * (tft_size_ - content_length_) / tft_size_, + content_length_); + + if (!upload_first_chunk_sent_) { + upload_first_chunk_sent_ = true; + delay(500); + } + + recv_ret_string_(recv_string, 5000, true); + if (recv_string[0] != 0x05) { // 0x05 == "ok" + ESP_LOGD(TAG, "recv_string [%s]", + format_hex_pretty(reinterpret_cast(recv_string.data()), recv_string.size()).c_str()); + } + + // handle partial upload request + if (recv_string[0] == 0x08 && recv_string.size() == 5) { + uint32_t result = 0; + for (int j = 0; j < 4; ++j) { + result += static_cast(recv_string[j + 1]) << (8 * j); + } + if (result > 0) { + ESP_LOGD(TAG, "Nextion reported new range %d", result); + content_length_ = tft_size_ - result; + return result; + } + } + + recv_string.clear(); + } + + return range_end + 1; + }; + + auto upload_tft_esp_idf = [&](const std::string &url, unsigned int update_baud_rate_) -> bool + { + ESP_LOGD("script.upload_tft.upload_tft", "Nextion TFT upload requested"); + ESP_LOGD("script.upload_tft.upload_tft", "url: %s", url.c_str()); + ESP_LOGD("script.upload_tft.upload_tft", "baud_rate: %i", update_baud_rate_); + + if (is_updating_) { + ESP_LOGD("script.upload_tft.upload_tft", "Currently updating"); + return upload_end_(false); + } + + if (!network::is_connected()) { + ESP_LOGD("script.upload_tft.upload_tft", "Network is not connected"); + return upload_end_(false); + } + + ESP_LOGD("script.upload_tft.upload_tft", "Setting Nextion protocol reparse mode to passive"); + id(disp1).set_protocol_reparse_mode(false); + + is_updating_ = true; + + esp_http_client_config_t config = { + .url = url.c_str() + }; + esp_http_client_handle_t http = esp_http_client_init(&config); + esp_http_client_set_header(http, "Range", "bytes=0-255"); + esp_http_client_set_header(http, "User-Agent", "curl/7.68.0"); + + int tries = 1; + int status = esp_http_client_perform(http); + delay(100); + while ((status != ESP_OK || (esp_http_client_get_status_code(http) != 200 && esp_http_client_get_status_code(http) != 206)) && tries <= 5) { + ESP_LOGW("script.upload_tft.upload_tft", "HTTP Request failed; URL: %s; Error: %d, retrying (%d/5)", url.c_str(), status, tries); + delay(250); + status = esp_http_client_perform(http); + tries++; + } + + if (tries > 5) { + esp_http_client_cleanup(http); + return upload_end_(true); + } + + int http_status = esp_http_client_get_status_code(http); + ESP_LOGD("script.upload_tft.upload_tft", "HTTP Status Code: %d", http_status); + + char *content_range_cstr = nullptr; + char *content_length_cstr = nullptr; + + esp_err_t range_err = esp_http_client_get_header(http, "Content-Range", &content_range_cstr); + if (range_err == ESP_OK && content_range_cstr != nullptr) { + ESP_LOGD("script.upload_tft.upload_tft", "Fetched Content-Range header: %s", content_range_cstr); + std::string content_range_string = content_range_cstr; + content_range_string = content_range_string.substr(content_range_string.find("/") + 1); + content_length_ = atoi(content_range_string.c_str()); + ESP_LOGD("script.upload_tft.upload_tft", "Using Content-Range header: %s", content_range_cstr); + free(content_range_cstr); + } else { + ESP_LOGW("script.upload_tft.upload_tft", "Failed to fetch Content-Range header. Error: %d", range_err); + } + + esp_err_t length_err = esp_http_client_get_header(http, "Content-Length", &content_length_cstr); + if (length_err == ESP_OK && content_length_cstr != nullptr) { + ESP_LOGD("script.upload_tft.upload_tft", "Fetched Content-Length header: %s", content_length_cstr); + content_length_ = atoi(content_length_cstr); // Convert to integer + free(content_length_cstr); + } else { + ESP_LOGW("script.upload_tft.upload_tft", "Failed to fetch Content-Length header. Error: %d", length_err); + } + + ESP_LOGD("script.upload_tft.upload_tft", "Parsed content length: %d", content_length_); + + if (content_length_ < 4096) { + ESP_LOGE("script.upload_tft.upload_tft", "File size check failed. Size: %d", content_length_); + return upload_end_(true); + } else { + ESP_LOGD("script.upload_tft.upload_tft", "File size check passed. Proceeding..."); + } + + + + ESP_LOGD("script.upload_tft.upload_tft", "Updating Nextion"); + // The Nextion will ignore the update command if it is sleeping + + char command[128]; + // Tells the Nextion the content length of the tft file and baud rate it will be sent at + // Once the Nextion accepts the command it will wait until the file is successfully uploaded + // If it fails for any reason a power cycle of the display will be needed + sprintf(command, "whmi-wris %d,%d,1", content_length_, update_baud_rate_); + + // Clear serial receive buffer + uint8_t d; + while (id(tf_uart).available()) { + id(tf_uart).read_byte(&d); + }; + + send_nextion_command(command); + + if (update_baud_rate_ != id(tf_uart).get_baud_rate()) + { + id(tf_uart).set_baud_rate(update_baud_rate_); + id(tf_uart).setup(); + } + + std::string response; + ESP_LOGD("script.upload_tft.upload_tft", "Waiting for upgrade response"); + recv_ret_string_(response, 2000, true); // This can take some time to return + + // The Nextion display will, if it's ready to accept data, send a 0x05 byte. + ESP_LOGD("script.upload_tft.upload_tft", "Upgrade response is [%s]", + format_hex_pretty(reinterpret_cast(response.data()), response.size()).c_str()); + + if (response.find(0x05) != std::string::npos) { + ESP_LOGD("script.upload_tft.upload_tft", "Preparation for tft update done"); + } else { + ESP_LOGD("script.upload_tft.upload_tft", "Preparation for tft update failed %d \"%s\"", response[0], response.c_str()); + return upload_end_(true); + } + + // Nextion wants 4096 bytes at a time. Make chunk_size a multiple of 4096 + uint32_t chunk_size = 8192; + if (esp_get_free_heap_size() > 81920) { // Ensure some FreeHeap to other things and limit chunk size + chunk_size = esp_get_free_heap_size() - 65536; + chunk_size = int(chunk_size / 4096) * 4096; + chunk_size = chunk_size > ${upload_tft_chunk_size_max} ? ${upload_tft_chunk_size_max} : chunk_size; + } else if (esp_get_free_heap_size() < 32768) { + chunk_size = 4096; + } + + if (transfer_buffer_ == nullptr) { + ExternalRAMAllocator allocator(ExternalRAMAllocator::ALLOW_FAILURE); + ESP_LOGD("script.upload_tft.upload_tft", "Allocating buffer size %d, Heap size is %u", chunk_size, esp_get_free_heap_size()); + transfer_buffer_ = allocator.allocate(chunk_size); + if (transfer_buffer_ == nullptr) { // Try a smaller size + ESP_LOGD("script.upload_tft.upload_tft", "Could not allocate buffer size: %d trying 4096 instead", chunk_size); + chunk_size = 4096; + ESP_LOGD("script.upload_tft.upload_tft", "Allocating %d buffer", chunk_size); + transfer_buffer_ = allocator.allocate(chunk_size); + + if (!transfer_buffer_) + { + return upload_end_(true); + } + } + + transfer_buffer_size_ = chunk_size; + } + + ESP_LOGD("script.upload_tft.upload_tft", "Updating tft from \"%s\" with a file size of %d using %zu chunksize, Heap Size %d", + url.c_str(), content_length_, transfer_buffer_size_, esp_get_free_heap_size()); + + int result = 0; + while (content_length_ > 0) { + result = upload_by_chunks_esp_idf(url, result); + if (result < 0) { + ESP_LOGD("script.upload_tft.upload_tft", "Error updating Nextion!"); + return upload_end_(true); + } + App.feed_wdt(); + ESP_LOGD("script.upload_tft.upload_tft", "Heap Size %d, Bytes left %d", esp_get_free_heap_size(), content_length_); + } + is_updating_ = false; + ESP_LOGD("script.upload_tft.upload_tft", "Successfully updated Nextion!"); + + return upload_end_(false); + + }; + + unsigned int upload_tries = 0; + while (upload_tries < 2) { + upload_tries++; + if (upload_tft_esp_idf(url, id(tf_uart).get_baud_rate())) id(restart_nspanel).press(); + ESP_LOGD(TAG, "Turn off Nextion"); + id(screen_power).turn_off(); + delay(1500); + ESP_LOGD(TAG, "Turn on Nextion"); + id(screen_power).turn_on(); + delay(1500); + } + unsigned int new_baud_rate; + if (id(tf_uart).get_baud_rate() == 115200) new_baud_rate = 921600; else new_baud_rate = 115200; + ESP_LOGD(TAG, "Trying again at %i bps", new_baud_rate); + if (upload_tft_esp_idf(url, new_baud_rate)) id(restart_nspanel).press(); + ESP_LOGE(TAG, "TFT upload failed."); + ESP_LOGD(TAG, "Turn off Nextion"); + id(screen_power).turn_off(); + delay(1500); + + #endif + + //ESP_LOGD(TAG, "Turn on Nextion"); //id(screen_power).turn_on(); //delay(1500); - //ESP_LOGW("script.upload_tft", "Trying Nextion standard upload"); + //ESP_LOGW(TAG, "Trying Nextion standard upload"); //id(disp1)->set_tft_url(url.c_str()); //id(disp1)->upload_tft(); - ESP_LOGD("script.upload_tft", "Restarting esphome"); + ESP_LOGD(TAG, "Restarting esphome"); delay(1500); id(restart_nspanel).press(); - ESP_LOGD("script.upload_tft", "Finished!"); + ESP_LOGD(TAG, "Finished!"); ##### ADD-ONS ############################################################ ##### Add-on - Climate #####