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");
30 "Range end: %" PRIu32
"\n"
31 "Range size: %" PRIu32,
32 range_end, range_size);
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 http_client.addHeader(
"Range", range_header);
40 int code = http_client.GET();
41 if (code != HTTP_CODE_OK and code != HTTP_CODE_PARTIAL_CONTENT) {
42 ESP_LOGW(TAG,
"HTTP failed: %s", HTTPClient::errorToString(code).c_str());
48 uint8_t *buffer = allocator.
allocate(4096);
50 ESP_LOGE(TAG,
"Buffer alloc failed");
54 std::string recv_string;
57 const uint16_t buffer_size =
59 ESP_LOGV(TAG,
"Fetch %" PRIu16
" bytes", buffer_size);
60 uint16_t read_len = 0;
61 int partial_read_len = 0;
64 if (http_client.getStreamPtr()->available() > 0) {
66 http_client.getStreamPtr()->readBytes(
reinterpret_cast<char *
>(buffer) + read_len, buffer_size - read_len);
67 read_len += partial_read_len;
68 if (partial_read_len > 0) {
74 if (read_len != buffer_size) {
76 ESP_LOGE(TAG,
"Read failed: %" PRIu16
"/%" PRIu16
" bytes", read_len, buffer_size);
82 ESP_LOGV(TAG,
"Fetched %d bytes", read_len);
90 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
")", upload_percentage, this->
content_length_,
91 EspClass::getFreeHeap());
93 if (recv_string[0] == 0x08 && recv_string.size() == 5) {
97 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
99 for (
int j = 0; j < 4; ++j) {
100 result +=
static_cast<uint8_t
>(recv_string[j + 1]) << (8 * j);
103 ESP_LOGI(TAG,
"New range: %" PRIu32, result);
105 range_start = result;
107 range_start = range_end + 1;
112 return range_end + 1;
113 }
else if (recv_string[0] != 0x05 and recv_string[0] != 0x08) {
116 TAG,
"Invalid response: [%s]",
117 format_hex_pretty_to(hex_buf,
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()));
125 }
else if (read_len == 0) {
126 ESP_LOGV(TAG,
"HTTP end");
129 ESP_LOGE(TAG,
"HTTP read failed: %d", read_len);
133 range_start = range_end + 1;
137 return range_end + 1;
142 "TFT upload requested\n"
145 YESNO(exit_reparse), this->
tft_url_.c_str());
148 ESP_LOGW(TAG,
"Upload in progress");
153 ESP_LOGE(TAG,
"No network");
160 ESP_LOGD(TAG,
"Exit reparse mode");
162 ESP_LOGW(TAG,
"Exit reparse failed");
169 if (baud_rate <= 0) {
172 ESP_LOGD(TAG,
"Baud rate: %" PRIu32, baud_rate);
178 EspClass::getFreeHeap());
179 HTTPClient http_client;
180 http_client.setTimeout(15000);
182 bool begin_status =
false;
184#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
185 http_client.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
186#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
187 http_client.setFollowRedirects(
true);
189#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
190 http_client.setRedirectLimit(3);
196 ESP_LOGD(TAG,
"Connection failed");
199 ESP_LOGD(TAG,
"Connected");
201 http_client.addHeader(
"Range",
"bytes=0-255");
202 const char *header_names[] = {
"Content-Range"};
203 http_client.collectHeaders(header_names, 1);
204 ESP_LOGD(TAG,
"URL: %s", this->
tft_url_.c_str());
205 http_client.setReuse(
true);
208 int code = http_client.GET();
212 while (code != 200 && code != 206 && tries <= 5) {
213 ESP_LOGW(TAG,
"HTTP fail: URL: %s; Error: %s, retry %d/5", this->
tft_url_.c_str(),
214 HTTPClient::errorToString(code).c_str(), tries);
218 code = http_client.GET();
222 if (code != 200 and code != 206) {
226 String content_range_string = http_client.header(
"Content-Range");
227 content_range_string.remove(0, 12);
228 this->
tft_size_ = content_range_string.toInt();
230 ESP_LOGD(TAG,
"TFT size: %zu bytes", this->
tft_size_);
232 ESP_LOGE(TAG,
"Size check failed");
233 ESP_LOGD(TAG,
"Close HTTP");
235 ESP_LOGV(TAG,
"Connection closed");
238 ESP_LOGV(TAG,
"Size check OK");
242 ESP_LOGD(TAG,
"Uploading");
245 ESP_LOGV(TAG,
"Wake-up");
250 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
257 snprintf(command,
sizeof(command),
"whmi-wris %" PRIu32
",%" PRIu32
",1", this->
content_length_, baud_rate);
260 ESP_LOGV(TAG,
"Clear RX buffer");
265 "Heap: %" PRIu32
"\n"
267 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(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 IRAM_ATTR HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.