3#ifdef USE_NEXTION_TFT_UPLOAD
16static const char *
const TAG =
"nextion.upload.arduino";
17static constexpr size_t NEXTION_MAX_RESPONSE_LOG_BYTES = 16;
24static constexpr uint32_t NEXTION_UPLOAD_ACK_TIMEOUT_MS = 5000;
31 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
33 ESP_LOGD(TAG,
"Range start: %" PRIu32, range_start);
34 if (range_size <= 0 || range_end <= range_start) {
35 ESP_LOGE(TAG,
"Invalid range end: %" PRIu32
", size: %" PRIu32, range_end, range_size);
39 char range_header[32];
40 buf_append_printf(range_header,
sizeof(range_header), 0,
"bytes=%" PRIu32
"-%" PRIu32, range_start, range_end);
41 ESP_LOGV(TAG,
"Range: %s", range_header);
42 http_client.addHeader(
"Range", range_header);
43 int code = http_client.GET();
44 if (code != HTTP_CODE_OK && code != HTTP_CODE_PARTIAL_CONTENT) {
45 ESP_LOGW(TAG,
"HTTP failed: %s", HTTPClient::errorToString(code).c_str());
51 uint8_t *buffer = allocator.
allocate(4096);
53 ESP_LOGE(TAG,
"Buffer alloc failed");
57 std::string recv_string;
60 const uint16_t buffer_size =
62 ESP_LOGV(TAG,
"Fetch %" PRIu16
" bytes", buffer_size);
63 uint16_t read_len = 0;
64 int partial_read_len = 0;
67 if (http_client.getStreamPtr()->available() > 0) {
69 http_client.getStreamPtr()->readBytes(
reinterpret_cast<char *
>(buffer) + read_len, buffer_size - read_len);
70 read_len += partial_read_len;
71 if (partial_read_len > 0) {
77 if (read_len != buffer_size) {
79 ESP_LOGE(TAG,
"Read failed: %" PRIu16
"/%" PRIu16
" bytes", read_len, buffer_size);
85 ESP_LOGV(TAG,
"Fetched %d bytes", read_len);
93 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
")", upload_percentage, this->
content_length_,
94 EspClass::getFreeHeap());
96 if (recv_string.empty()) {
97 ESP_LOGW(TAG,
"No response from display after %" PRIu32
"ms", NEXTION_UPLOAD_ACK_TIMEOUT_MS);
102 if (recv_string[0] == 0x08 && recv_string.size() == 5) {
106 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
108 for (
int j = 0; j < 4; ++j) {
109 result +=
static_cast<uint8_t
>(recv_string[j + 1]) << (8 * j);
112 ESP_LOGI(TAG,
"New range: %" PRIu32, result);
114 range_start = result;
116 range_start = range_end + 1;
121 return range_end + 1;
122 }
else if (recv_string[0] != 0x05 && recv_string[0] != 0x08) {
125 TAG,
"Invalid response: [%s]",
126 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
134 }
else if (read_len == 0) {
135 ESP_LOGV(TAG,
"HTTP end");
138 ESP_LOGE(TAG,
"HTTP read failed: %d", read_len);
142 range_start = range_end + 1;
146 return range_end + 1;
150 ESP_LOGD(TAG,
"TFT upload requested, exit reparse: %s, URL: %s", YESNO(exit_reparse), this->
tft_url_.c_str());
153 ESP_LOGW(TAG,
"Upload in progress");
158 ESP_LOGE(TAG,
"No network");
165 ESP_LOGD(TAG,
"Exit reparse mode");
167 ESP_LOGW(TAG,
"Exit reparse failed");
174 if (baud_rate <= 0) {
177 ESP_LOGD(TAG,
"Baud rate: %" PRIu32, baud_rate);
180 ESP_LOGV(TAG,
"Init HTTP client, heap: %" PRIu32, EspClass::getFreeHeap());
181 HTTPClient http_client;
184 bool begin_status =
false;
186#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
187 http_client.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
188#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
189 http_client.setFollowRedirects(
true);
191#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
192 http_client.setRedirectLimit(3);
198 ESP_LOGD(TAG,
"Connection failed");
201 ESP_LOGD(TAG,
"Connected");
203 http_client.addHeader(
"Range",
"bytes=0-255");
204 const char *header_names[] = {
"Content-Range"};
205 http_client.collectHeaders(header_names, 1);
206 ESP_LOGD(TAG,
"URL: %s", this->
tft_url_.c_str());
207 http_client.setReuse(
true);
210 int code = http_client.GET();
215 ESP_LOGW(TAG,
"HTTP fail: URL: %s; Error: %s, retry %d/%u", this->
tft_url_.c_str(),
216 HTTPClient::errorToString(code).c_str(), tries, this->tft_upload_http_retries_);
220 code = http_client.GET();
224 if (code != 200 && code != 206) {
225 ESP_LOGE(TAG,
"HTTP request failed with status %d", code);
229 String content_range_string = http_client.header(
"Content-Range");
230 content_range_string.remove(0, 12);
231 this->
tft_size_ = content_range_string.toInt();
233 ESP_LOGD(TAG,
"TFT size: %zu bytes", this->
tft_size_);
235 ESP_LOGE(TAG,
"Size check failed");
236 ESP_LOGD(TAG,
"Close HTTP");
238 ESP_LOGV(TAG,
"Connection closed");
241 ESP_LOGV(TAG,
"Size check OK");
245 ESP_LOGD(TAG,
"Uploading");
248 ESP_LOGV(TAG,
"Wake-up");
253 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
260 snprintf(command,
sizeof(command),
"whmi-wris %" PRIu32
",%" PRIu32
",1", this->
content_length_, baud_rate);
263 ESP_LOGV(TAG,
"Clear RX buffer");
267 ESP_LOGV(TAG,
"Heap: %" PRIu32
", upload cmd: %s", EspClass::getFreeHeap(), command);
278 std::string response;
279 ESP_LOGV(TAG,
"Wait upload resp");
284 ESP_LOGD(TAG,
"Upload resp: [%s] %zu B",
285 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(response.data()), response.size()),
287 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
289 if (response.find(0x05) != std::string::npos) {
290 ESP_LOGV(TAG,
"Upload prep done");
292 ESP_LOGE(TAG,
"Prep failed %d '%s'", response[0], response.c_str());
293 ESP_LOGD(TAG,
"Close HTTP");
295 ESP_LOGV(TAG,
"Connection closed");
304 this->
tft_url_.c_str(), this->content_length_, EspClass::getFreeHeap());
308 ESP_LOGV(TAG,
"Start chunk transfer");
313 if (upload_result < 0) {
314 ESP_LOGE(TAG,
"Upload error");
315 ESP_LOGD(TAG,
"Close HTTP");
317 ESP_LOGV(TAG,
"Connection closed");
321 ESP_LOGV(TAG,
"Heap: %" PRIu32
" left: %" PRIu32, EspClass::getFreeHeap(), this->
content_length_);
324 ESP_LOGD(TAG,
"Upload complete");
326 ESP_LOGV(TAG,
"Close HTTP");
328 ESP_LOGV(TAG,
"Connection closed");
334 if (this->
tft_url_.compare(0, 6,
"https:") == 0) {
void feed_wdt()
Feed the task watchdog.
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.
uint8_t tft_upload_http_retries_
HTTP retry count (default: 5)
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.
uint16_t tft_upload_http_timeout_
HTTP timeout in ms (default: 4.5s)
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)
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
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.