3#ifdef USE_NEXTION_TFT_UPLOAD
15static const char *
const TAG =
"nextion.upload.arduino";
21 uint32_t range_size = this->
tft_size_ - range_start;
22 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
24 ESP_LOGD(TAG,
"Range start: %" PRIu32, range_start);
25 if (range_size <= 0 or range_end <= range_start) {
26 ESP_LOGE(TAG,
"Invalid range");
28 "Range end: %" PRIu32
"\n"
29 "Range size: %" PRIu32,
30 range_end, range_size);
34 char range_header[32];
35 sprintf(range_header,
"bytes=%" PRIu32
"-%" PRIu32, range_start, range_end);
36 ESP_LOGV(TAG,
"Range: %s", range_header);
37 http_client.addHeader(
"Range", range_header);
38 int code = http_client.GET();
39 if (code != HTTP_CODE_OK and code != HTTP_CODE_PARTIAL_CONTENT) {
40 ESP_LOGW(TAG,
"HTTP failed: %s", HTTPClient::errorToString(code).c_str());
46 uint8_t *buffer = allocator.
allocate(4096);
48 ESP_LOGE(TAG,
"Buffer alloc failed");
52 std::string recv_string;
55 const uint16_t buffer_size =
57 ESP_LOGV(TAG,
"Fetch %" PRIu16
" bytes", buffer_size);
58 uint16_t read_len = 0;
59 int partial_read_len = 0;
62 if (http_client.getStreamPtr()->available() > 0) {
64 http_client.getStreamPtr()->readBytes(
reinterpret_cast<char *
>(buffer) + read_len, buffer_size - read_len);
65 read_len += partial_read_len;
66 if (partial_read_len > 0) {
72 if (read_len != buffer_size) {
74 ESP_LOGE(TAG,
"Read failed: %" PRIu16
"/%" PRIu16
" bytes", read_len, buffer_size);
80 ESP_LOGV(TAG,
"Fetched %d bytes", read_len);
88 ESP_LOGD(TAG,
"Upload: %0.2f%% (%" PRIu32
" left, heap: %" PRIu32
")", upload_percentage, this->
content_length_,
89 EspClass::getFreeHeap());
91 if (recv_string[0] == 0x08 && recv_string.size() == 5) {
92 ESP_LOGD(TAG,
"Recv: [%s]",
93 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()).c_str());
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) {
110 ESP_LOGE(TAG,
"Invalid response: [%s]",
111 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(recv_string.data()), recv_string.size()).c_str());
119 }
else if (read_len == 0) {
120 ESP_LOGV(TAG,
"HTTP end");
123 ESP_LOGE(TAG,
"HTTP read failed: %d", read_len);
127 range_start = range_end + 1;
131 return range_end + 1;
136 "TFT upload requested\n"
139 YESNO(exit_reparse), this->
tft_url_.c_str());
142 ESP_LOGW(TAG,
"Upload in progress");
147 ESP_LOGE(TAG,
"No network");
154 ESP_LOGD(TAG,
"Exit reparse mode");
156 ESP_LOGW(TAG,
"Exit reparse failed");
163 if (baud_rate <= 0) {
166 ESP_LOGD(TAG,
"Baud rate: %" PRIu32, baud_rate);
172 EspClass::getFreeHeap());
173 HTTPClient http_client;
174 http_client.setTimeout(15000);
176 bool begin_status =
false;
178#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
179 http_client.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
180#elif USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
181 http_client.setFollowRedirects(
true);
183#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
184 http_client.setRedirectLimit(3);
190 ESP_LOGD(TAG,
"Connection failed");
193 ESP_LOGD(TAG,
"Connected");
195 http_client.addHeader(
"Range",
"bytes=0-255");
196 const char *header_names[] = {
"Content-Range"};
197 http_client.collectHeaders(header_names, 1);
198 ESP_LOGD(TAG,
"URL: %s", this->
tft_url_.c_str());
199 http_client.setReuse(
true);
202 int code = http_client.GET();
206 while (code != 200 && code != 206 && tries <= 5) {
207 ESP_LOGW(TAG,
"HTTP fail: URL: %s; Error: %s, retry %d/5", this->
tft_url_.c_str(),
208 HTTPClient::errorToString(code).c_str(), tries);
212 code = http_client.GET();
216 if (code != 200 and code != 206) {
220 String content_range_string = http_client.header(
"Content-Range");
221 content_range_string.remove(0, 12);
222 this->
tft_size_ = content_range_string.toInt();
224 ESP_LOGD(TAG,
"TFT size: %zu bytes", this->
tft_size_);
226 ESP_LOGE(TAG,
"Size check failed");
227 ESP_LOGD(TAG,
"Close HTTP");
229 ESP_LOGV(TAG,
"Connection closed");
232 ESP_LOGV(TAG,
"Size check OK");
236 ESP_LOGD(TAG,
"Uploading");
239 ESP_LOGV(TAG,
"Wake-up");
244 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
251 snprintf(command,
sizeof(command),
"whmi-wris %" PRIu32
",%" PRIu32
",1", this->
content_length_, baud_rate);
254 ESP_LOGV(TAG,
"Clear RX buffer");
259 "Heap: %" PRIu32
"\n"
261 EspClass::getFreeHeap(), command);
272 std::string response;
273 ESP_LOGV(TAG,
"Wait upload resp");
277 ESP_LOGD(TAG,
"Upload resp: [%s] %zu B",
278 format_hex_pretty(
reinterpret_cast<const uint8_t *
>(response.data()), response.size()).c_str(),
280 ESP_LOGV(TAG,
"Heap: %" PRIu32, EspClass::getFreeHeap());
282 if (response.find(0x05) != std::string::npos) {
283 ESP_LOGV(TAG,
"Upload prep done");
285 ESP_LOGE(TAG,
"Prep failed %d '%s'", response[0], response.c_str());
286 ESP_LOGD(TAG,
"Close HTTP");
288 ESP_LOGV(TAG,
"Connection closed");
297 this->
tft_url_.c_str(), this->content_length_, EspClass::getFreeHeap());
301 ESP_LOGV(TAG,
"Start chunk transfer");
306 if (upload_result < 0) {
307 ESP_LOGE(TAG,
"Upload error");
308 ESP_LOGD(TAG,
"Close HTTP");
310 ESP_LOGV(TAG,
"Connection closed");
314 ESP_LOGV(TAG,
"Heap: %" PRIu32
" left: %" PRIu32, EspClass::getFreeHeap(), this->
content_length_);
317 ESP_LOGD(TAG,
"Upload complete");
319 ESP_LOGV(TAG,
"Close HTTP");
321 ESP_LOGV(TAG,
"Connection closed");
327 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.
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.
void IRAM_ATTR HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.