3#ifdef USE_NEXTION_TFT_UPLOAD
16static const char *
const TAG =
"nextion.upload.arduino";
17static constexpr size_t NEXTION_MAX_RESPONSE_LOG_BYTES = 16;
23 uint32_t range_size = this->
tft_size_ - range_start;
24 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
26 ESP_LOGD(TAG,
"Range start: %" PRIu32, range_start);
27 if (range_size <= 0 or range_end <= range_start) {
28 ESP_LOGE(TAG,
"Invalid range end: %" PRIu32
", size: %" PRIu32, range_end, range_size);
32 char range_header[32];
33 buf_append_printf(range_header,
sizeof(range_header), 0,
"bytes=%" PRIu32
"-%" PRIu32, range_start, range_end);
34 ESP_LOGV(TAG,
"Range: %s", range_header);
35 http_client.addHeader(
"Range", range_header);
36 int code = http_client.GET();
37 if (code != HTTP_CODE_OK and code != HTTP_CODE_PARTIAL_CONTENT) {
38 ESP_LOGW(TAG,
"HTTP failed: %s", HTTPClient::errorToString(code).c_str());
44 uint8_t *buffer = allocator.
allocate(4096);
46 ESP_LOGE(TAG,
"Buffer alloc failed");
50 std::string recv_string;
53 const uint16_t buffer_size =
55 ESP_LOGV(TAG,
"Fetch %" PRIu16
" bytes", buffer_size);
56 uint16_t read_len = 0;
57 int partial_read_len = 0;
60 if (http_client.getStreamPtr()->available() > 0) {
62 http_client.getStreamPtr()->readBytes(
reinterpret_cast<char *
>(buffer) + read_len, buffer_size - read_len);
63 read_len += partial_read_len;
64 if (partial_read_len > 0) {
70 if (read_len != buffer_size) {
72 ESP_LOGE(TAG,
"Read failed: %" PRIu16
"/%" PRIu16
" bytes", read_len, buffer_size);
78 ESP_LOGV(TAG,
"Fetched %d bytes", read_len);
86 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
")", upload_percentage, this->
content_length_,
87 EspClass::getFreeHeap());
89 if (recv_string[0] == 0x08 && recv_string.size() == 5) {
93 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
95 for (
int j = 0; j < 4; ++j) {
96 result +=
static_cast<uint8_t
>(recv_string[j + 1]) << (8 * j);
99 ESP_LOGI(TAG,
"New range: %" PRIu32, result);
101 range_start = result;
103 range_start = range_end + 1;
108 return range_end + 1;
109 }
else if (recv_string[0] != 0x05 and recv_string[0] != 0x08) {
112 TAG,
"Invalid response: [%s]",
113 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
121 }
else if (read_len == 0) {
122 ESP_LOGV(TAG,
"HTTP end");
125 ESP_LOGE(TAG,
"HTTP read failed: %d", read_len);
129 range_start = range_end + 1;
133 return range_end + 1;
137 ESP_LOGD(TAG,
"TFT upload requested, exit reparse: %s, URL: %s", YESNO(exit_reparse), this->
tft_url_.c_str());
140 ESP_LOGW(TAG,
"Upload in progress");
145 ESP_LOGE(TAG,
"No network");
152 ESP_LOGD(TAG,
"Exit reparse mode");
154 ESP_LOGW(TAG,
"Exit reparse failed");
161 if (baud_rate <= 0) {
164 ESP_LOGD(TAG,
"Baud rate: %" PRIu32, baud_rate);
167 ESP_LOGV(TAG,
"Init HTTP client, heap: %" PRIu32, EspClass::getFreeHeap());
168 HTTPClient http_client;
169 http_client.setTimeout(15000);
171 bool begin_status =
false;
173#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
174 http_client.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
175#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
176 http_client.setFollowRedirects(
true);
178#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
179 http_client.setRedirectLimit(3);
185 ESP_LOGD(TAG,
"Connection failed");
188 ESP_LOGD(TAG,
"Connected");
190 http_client.addHeader(
"Range",
"bytes=0-255");
191 const char *header_names[] = {
"Content-Range"};
192 http_client.collectHeaders(header_names, 1);
193 ESP_LOGD(TAG,
"URL: %s", this->
tft_url_.c_str());
194 http_client.setReuse(
true);
197 int code = http_client.GET();
201 while (code != 200 && code != 206 && tries <= 5) {
202 ESP_LOGW(TAG,
"HTTP fail: URL: %s; Error: %s, retry %d/5", this->
tft_url_.c_str(),
203 HTTPClient::errorToString(code).c_str(), tries);
207 code = http_client.GET();
211 if (code != 200 and code != 206) {
212 ESP_LOGE(TAG,
"HTTP request failed with status %d", code);
216 String content_range_string = http_client.header(
"Content-Range");
217 content_range_string.remove(0, 12);
218 this->
tft_size_ = content_range_string.toInt();
220 ESP_LOGD(TAG,
"TFT size: %zu bytes", this->
tft_size_);
222 ESP_LOGE(TAG,
"Size check failed");
223 ESP_LOGD(TAG,
"Close HTTP");
225 ESP_LOGV(TAG,
"Connection closed");
228 ESP_LOGV(TAG,
"Size check OK");
232 ESP_LOGD(TAG,
"Uploading");
235 ESP_LOGV(TAG,
"Wake-up");
240 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
247 snprintf(command,
sizeof(command),
"whmi-wris %" PRIu32
",%" PRIu32
",1", this->
content_length_, baud_rate);
250 ESP_LOGV(TAG,
"Clear RX buffer");
254 ESP_LOGV(TAG,
"Heap: %" PRIu32
", upload cmd: %s", EspClass::getFreeHeap(), command);
265 std::string response;
266 ESP_LOGV(TAG,
"Wait upload resp");
271 ESP_LOGD(TAG,
"Upload resp: [%s] %zu B",
272 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(response.data()), response.size()),
274 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
276 if (response.find(0x05) != std::string::npos) {
277 ESP_LOGV(TAG,
"Upload prep done");
279 ESP_LOGE(TAG,
"Prep failed %d '%s'", response[0], response.c_str());
280 ESP_LOGD(TAG,
"Close HTTP");
282 ESP_LOGV(TAG,
"Connection closed");
291 this->
tft_url_.c_str(), this->content_length_, EspClass::getFreeHeap());
295 ESP_LOGV(TAG,
"Start chunk transfer");
300 if (upload_result < 0) {
301 ESP_LOGE(TAG,
"Upload error");
302 ESP_LOGD(TAG,
"Close HTTP");
304 ESP_LOGV(TAG,
"Connection closed");
308 ESP_LOGV(TAG,
"Heap: %" PRIu32
" left: %" PRIu32, EspClass::getFreeHeap(), this->
content_length_);
311 ESP_LOGD(TAG,
"Upload complete");
313 ESP_LOGV(TAG,
"Close HTTP");
315 ESP_LOGV(TAG,
"Connection closed");
321 if (this->
tft_url_.compare(0, 6,
"https:") == 0) {
void feed_wdt(uint32_t time=0)
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
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.
WiFiClient * wifi_client_
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.
BearSSL::WiFiClientSecure * wifi_client_secure_
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
WiFiClient * get_wifi_client_()
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.
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.