3#ifdef USE_NEXTION_TFT_UPLOAD
6#include <esp_heap_caps.h>
7#include <esp_http_client.h>
17static const char *
const TAG =
"nextion.upload.esp32";
23 uint32_t range_size = this->
tft_size_ - range_start;
24 ESP_LOGV(TAG,
"Heap: %" PRIu32, esp_get_free_heap_size());
26 ESP_LOGD(TAG,
"Range start: %" PRIu32, range_start);
27 if (range_size <= 0 or range_end <= range_start) {
29 "Range end: %" PRIu32
"\n"
30 "Range size: %" PRIu32,
31 range_end, range_size);
32 ESP_LOGE(TAG,
"Invalid range");
36 char range_header[32];
37 sprintf(range_header,
"bytes=%" PRIu32
"-%" PRIu32, range_start, range_end);
38 ESP_LOGV(TAG,
"Range: %s", range_header);
39 esp_http_client_set_header(http_client,
"Range", range_header);
40 ESP_LOGV(TAG,
"Open HTTP");
41 esp_err_t err = esp_http_client_open(http_client, 0);
43 ESP_LOGE(TAG,
"HTTP open failed: %s", esp_err_to_name(err));
47 ESP_LOGV(TAG,
"Fetch length");
48 const int chunk_size = esp_http_client_fetch_headers(http_client);
49 ESP_LOGV(TAG,
"Length: %d", chunk_size);
50 if (chunk_size <= 0) {
51 ESP_LOGE(TAG,
"Get length failed: %d", chunk_size);
57 uint8_t *buffer = allocator.
allocate(4096);
59 ESP_LOGE(TAG,
"Buffer alloc failed");
63 std::string recv_string;
66 const uint16_t buffer_size =
68 ESP_LOGV(TAG,
"Fetch %" PRIu16
" bytes", buffer_size);
69 uint16_t read_len = 0;
70 int partial_read_len = 0;
73 while (retries < 5 && read_len < buffer_size) {
75 esp_http_client_read(http_client,
reinterpret_cast<char *
>(buffer) + read_len, buffer_size - read_len);
76 if (partial_read_len > 0) {
77 read_len += partial_read_len;
83 vTaskDelay(pdMS_TO_TICKS(2));
87 if (read_len != buffer_size) {
89 ESP_LOGE(TAG,
"Read failed: %" PRIu16
"/%" PRIu16
" bytes", read_len, buffer_size);
95 ESP_LOGV(TAG,
"Fetched %d bytes", read_len);
104 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
"+%" PRIu32
")", upload_percentage,
105 this->
content_length_,
static_cast<uint32_t
>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL)),
106 static_cast<uint32_t
>(heap_caps_get_free_size(MALLOC_CAP_SPIRAM)));
108 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
")", upload_percentage, this->
content_length_,
109 static_cast<uint32_t
>(esp_get_free_heap_size()));
112 if (recv_string[0] == 0x08 && recv_string.size() == 5) {
113 ESP_LOGD(TAG,
"Recv: [%s]",
114 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()).c_str());
116 for (
int j = 0; j < 4; ++j) {
117 result +=
static_cast<uint8_t
>(recv_string[j + 1]) << (8 * j);
120 ESP_LOGI(TAG,
"New range: %" PRIu32, result);
122 range_start = result;
124 range_start = range_end + 1;
129 return range_end + 1;
130 }
else if (recv_string[0] != 0x05 and recv_string[0] != 0x08) {
131 ESP_LOGE(TAG,
"Invalid response: [%s]",
132 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()).c_str());
140 }
else if (read_len == 0) {
141 ESP_LOGV(TAG,
"HTTP end");
144 ESP_LOGE(TAG,
"HTTP read failed: %" PRIu16, read_len);
148 range_start = range_end + 1;
152 return range_end + 1;
157 "TFT upload requested\n"
160 YESNO(exit_reparse), this->
tft_url_.c_str());
163 ESP_LOGW(TAG,
"Upload in progress");
168 ESP_LOGE(TAG,
"No network");
175 ESP_LOGD(TAG,
"Exit reparse mode");
177 ESP_LOGW(TAG,
"Exit reparse failed");
184 if (baud_rate <= 0) {
187 ESP_LOGD(TAG,
"Baud rate: %" PRIu32, baud_rate);
193 esp_get_free_heap_size());
194 esp_http_client_config_t config = {
197 .method = HTTP_METHOD_HEAD,
199 .disable_auto_redirect =
false,
200 .max_redirection_count = 10,
203 esp_http_client_handle_t http_client = esp_http_client_init(&config);
205 ESP_LOGE(TAG,
"HTTP init failed");
209 esp_err_t err = esp_http_client_set_header(http_client,
"Connection",
"keep-alive");
211 ESP_LOGE(TAG,
"Set header failed: %s", esp_err_to_name(err));
212 esp_http_client_cleanup(http_client);
220 esp_get_free_heap_size());
221 err = esp_http_client_perform(http_client);
223 ESP_LOGE(TAG,
"HTTP failed: %s", esp_err_to_name(err));
224 esp_http_client_cleanup(http_client);
232 esp_get_free_heap_size());
233 int status_code = esp_http_client_get_status_code(http_client);
234 if (status_code != 200 && status_code != 206) {
238 this->
tft_size_ = esp_http_client_get_content_length(http_client);
240 ESP_LOGD(TAG,
"TFT size: %zu bytes", this->
tft_size_);
242 ESP_LOGE(TAG,
"Size check failed");
243 ESP_LOGD(TAG,
"Close HTTP");
244 esp_http_client_close(http_client);
245 esp_http_client_cleanup(http_client);
246 ESP_LOGV(TAG,
"Connection closed");
249 ESP_LOGV(TAG,
"Size check OK");
253 ESP_LOGD(TAG,
"Uploading");
256 ESP_LOGV(TAG,
"Wake-up");
260 vTaskDelay(pdMS_TO_TICKS(250));
261 ESP_LOGV(TAG,
"Heap: %" PRIu32, esp_get_free_heap_size());
268 snprintf(command,
sizeof(command),
"whmi-wris %" PRIu32
",%" PRIu32
",1", this->
content_length_, baud_rate);
271 ESP_LOGV(TAG,
"Clear RX buffer");
273 vTaskDelay(pdMS_TO_TICKS(250));
274 ESP_LOGV(TAG,
"Heap: %" PRIu32, esp_get_free_heap_size());
276 ESP_LOGV(TAG,
"Upload cmd: %s", command);
285 std::string response;
286 ESP_LOGV(TAG,
"Wait upload resp");
290 ESP_LOGD(TAG,
"Upload resp: [%s] %zu B",
291 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(response.data()), response.size()).c_str(),
293 ESP_LOGV(TAG,
"Heap: %" PRIu32, esp_get_free_heap_size());
295 if (response.find(0x05) != std::string::npos) {
296 ESP_LOGV(TAG,
"Upload prep done");
298 ESP_LOGE(TAG,
"Upload prep failed %d '%s'", response[0], response.c_str());
299 ESP_LOGD(TAG,
"Close HTTP");
300 esp_http_client_close(http_client);
301 esp_http_client_cleanup(http_client);
302 ESP_LOGV(TAG,
"Connection closed");
306 ESP_LOGV(TAG,
"Set method to GET");
307 esp_err_t set_method_result = esp_http_client_set_method(http_client, HTTP_METHOD_GET);
308 if (set_method_result != ESP_OK) {
309 ESP_LOGE(TAG,
"Set GET failed: %s", esp_err_to_name(set_method_result));
316 " Size: %" PRIu32
" bytes\n"
318 this->
tft_url_.c_str(), this->content_length_, esp_get_free_heap_size());
322 ESP_LOGV(TAG,
"Start chunk transfer");
327 if (upload_result < 0) {
328 ESP_LOGE(TAG,
"TFT upload error");
329 ESP_LOGD(TAG,
"Close HTTP");
330 esp_http_client_close(http_client);
331 esp_http_client_cleanup(http_client);
332 ESP_LOGV(TAG,
"Connection closed");
336 ESP_LOGV(TAG,
"Heap: %" PRIu32
" left: %" PRIu32, esp_get_free_heap_size(), this->
content_length_);
339 ESP_LOGD(TAG,
"TFT upload complete\n"
341 esp_http_client_close(http_client);
342 esp_http_client_cleanup(http_client);
343 ESP_LOGV(TAG,
"Connection closed");
void feed_wdt(uint32_t time=0)
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
int upload_by_chunks_(esp_http_client_handle_t http_client, uint32_t &range_start)
will request 4096 bytes chunks from the web server and send each to Nextion
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
struct esphome::nextion::Nextion::@144 connection_state_
Status flags for Nextion display state management.
bool upload_tft(uint32_t baud_rate=0, bool exit_reparse=true)
Uploads the TFT file to the Nextion display.
bool set_protocol_reparse_mode(bool active_mode)
Sets the Nextion display's protocol reparse mode.
bool upload_end_(bool successful)
Ends the upload process, restart Nextion and, if successful, restarts ESP.
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
void reset_(bool reset_nextion=true)
uint32_t original_baud_rate_
bool upload_first_chunk_sent_
virtual void load_settings(bool dump_config)
Load the UART settings.
void set_baud_rate(uint32_t baud_rate)
uint32_t get_baud_rate() const
void write_array(const uint8_t *data, size_t len)
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Providing packet encoding functions for exchanging data with a remote host.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Application App
Global storage of Application pointer - only one Application can exist.