Upload TFT UI on panel
This commit is contained in:
@@ -140,10 +140,45 @@ script:
|
|||||||
static const char *const TAG = "script.upload_tft";
|
static const char *const TAG = "script.upload_tft";
|
||||||
ESP_LOGD(TAG, "Starting...");
|
ESP_LOGD(TAG, "Starting...");
|
||||||
|
|
||||||
|
auto delay_seconds_ = [](int seconds) {
|
||||||
|
ESP_LOGD(TAG, "Wait %i seconds", seconds);
|
||||||
|
for (int i = 0; i < (seconds*4); i++) {
|
||||||
|
#ifdef ARDUINO
|
||||||
|
delay(250);
|
||||||
|
#elif defined(USE_ESP_IDF)
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(250));
|
||||||
|
#endif
|
||||||
|
App.feed_wdt();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string framework = "Unknown";
|
||||||
|
#ifdef ARDUINO
|
||||||
|
framework = "Arduino";
|
||||||
|
#elif defined(ESP_PLATFORM)
|
||||||
|
framework = "ESP-IDF";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (disp1->is_setup()) {
|
||||||
|
disp1->set_backlight_brightness(1);
|
||||||
|
disp1->set_component_text_printf("confirm.title", "Upload TFT\\r%s", framework.c_str());
|
||||||
|
disp1->set_component_text_printf("confirm.body", "Preparing...");
|
||||||
|
disp1->goto_page("confirm");
|
||||||
|
disp1->hide_component("bt_close");
|
||||||
|
disp1->hide_component("bt_accept");
|
||||||
|
disp1->hide_component("bt_clear");
|
||||||
|
disp1->hide_component("bt_close");
|
||||||
|
delay_seconds_(1);
|
||||||
|
display_wrapped_text->execute("confirm.body", "Informing the blueprint that panel is unavailable...", 18);
|
||||||
|
disp1->hide_component("bt_close");
|
||||||
|
}
|
||||||
nextion_init->publish_state(false);
|
nextion_init->publish_state(false);
|
||||||
|
if (disp1->is_setup()) {
|
||||||
|
delay_seconds_(1);
|
||||||
|
display_wrapped_text->execute("confirm.body", "Stopping scripts...", 18);
|
||||||
|
}
|
||||||
stop_all->execute();
|
stop_all->execute();
|
||||||
disp1->set_backlight_brightness(1);
|
if (disp1->is_setup()) delay_seconds_(1);
|
||||||
disp1->hide_component("255");
|
|
||||||
|
|
||||||
std::vector<uint8_t> buffer_;
|
std::vector<uint8_t> buffer_;
|
||||||
|
|
||||||
@@ -165,18 +200,6 @@ script:
|
|||||||
uart->setup();
|
uart->setup();
|
||||||
};
|
};
|
||||||
|
|
||||||
auto delay_seconds_ = [](int seconds) {
|
|
||||||
ESP_LOGD(TAG, "Wait %i seconds", seconds);
|
|
||||||
for (int i = 0; i < (seconds*4); i++) {
|
|
||||||
#ifdef ARDUINO
|
|
||||||
delay(250);
|
|
||||||
#elif defined(USE_ESP_IDF)
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(250));
|
|
||||||
#endif
|
|
||||||
App.feed_wdt();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
auto send_nextion_command = [](const std::string &command) -> bool
|
auto send_nextion_command = [](const std::string &command) -> bool
|
||||||
{
|
{
|
||||||
static const char *const TAG = "script.upload_tft.send_nextion_command";
|
static const char *const TAG = "script.upload_tft.send_nextion_command";
|
||||||
@@ -774,16 +797,26 @@ script:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Try #1 at 921600 bps");
|
ESP_LOGD(TAG, "Try #1 at 921600 bps");
|
||||||
|
if (disp1->is_setup()) {
|
||||||
|
display_wrapped_text->execute("confirm.body", "Try #1 at 921600 bps", 18);
|
||||||
|
delay_seconds_(1);
|
||||||
|
}
|
||||||
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
||||||
ESP_LOGW(TAG, "Try #1 failed");
|
ESP_LOGW(TAG, "Try #1 failed");
|
||||||
|
if (disp1->is_setup()) display_wrapped_text->execute("confirm.body", "Try #1 failed", 18);
|
||||||
delay_seconds_(5);
|
delay_seconds_(5);
|
||||||
ESP_LOGD(TAG, "Try #2 at 921600 bps");
|
ESP_LOGD(TAG, "Try #2 at 921600 bps");
|
||||||
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
||||||
ESP_LOGW(TAG, "Try #2 failed");
|
ESP_LOGW(TAG, "Try #2 failed");
|
||||||
|
if (disp1->is_setup()) display_wrapped_text->execute("confirm.body", "Try #2 failed", 18);
|
||||||
delay_seconds_(5);
|
delay_seconds_(5);
|
||||||
ESP_LOGD(TAG, "Try #3 at 115200 bps");
|
ESP_LOGD(TAG, "Try #3 at 115200 bps");
|
||||||
if (upload_tft_(url, 115200)) id(restart_nspanel).press();
|
if (upload_tft_(url, 115200)) id(restart_nspanel).press();
|
||||||
ESP_LOGW(TAG, "Try #3 failed");
|
ESP_LOGW(TAG, "Try #3 failed");
|
||||||
|
if (disp1->is_setup()) {
|
||||||
|
display_wrapped_text->execute("confirm.body", "Try #3 failed. Restarting display.", 18);
|
||||||
|
delay_seconds_(3);
|
||||||
|
}
|
||||||
ESP_LOGD(TAG, "Turn off Nextion");
|
ESP_LOGD(TAG, "Turn off Nextion");
|
||||||
id(screen_power).turn_off();
|
id(screen_power).turn_off();
|
||||||
delay_seconds_(2);
|
delay_seconds_(2);
|
||||||
|
|||||||
Binary file not shown.
@@ -109,7 +109,7 @@ Text body
|
|||||||
Send Component ID : disabled
|
Send Component ID : disabled
|
||||||
Associated Keyboard: none
|
Associated Keyboard: none
|
||||||
Text : Please confirm
|
Text : Please confirm
|
||||||
Max. Text Size : 22
|
Max. Text Size : 254
|
||||||
|
|
||||||
Events
|
Events
|
||||||
Touch Press Event
|
Touch Press Event
|
||||||
|
|||||||
Binary file not shown.
@@ -109,7 +109,7 @@ Text body
|
|||||||
Send Component ID : disabled
|
Send Component ID : disabled
|
||||||
Associated Keyboard: none
|
Associated Keyboard: none
|
||||||
Text : Please confirm
|
Text : Please confirm
|
||||||
Max. Text Size : 22
|
Max. Text Size : 254
|
||||||
|
|
||||||
Events
|
Events
|
||||||
Touch Press Event
|
Touch Press Event
|
||||||
|
|||||||
Binary file not shown.
@@ -109,7 +109,7 @@ Text body
|
|||||||
Send Component ID : disabled
|
Send Component ID : disabled
|
||||||
Associated Keyboard: none
|
Associated Keyboard: none
|
||||||
Text : Please confirm
|
Text : Please confirm
|
||||||
Max. Text Size : 22
|
Max. Text Size : 254
|
||||||
|
|
||||||
Events
|
Events
|
||||||
Touch Press Event
|
Touch Press Event
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
#####################################################################################################
|
|
||||||
##### NSPANEL ESPHOME created by Blackymas - https://github.com/Blackymas/NSPanel_HA_Blueprint #####
|
|
||||||
##### ESPHome Add-on for Climate control - Shared - This will be called by heat/cool #####
|
|
||||||
##### PLEASE only make changes if it is necessary and also the required knowledge is available. #####
|
|
||||||
##### For normal use with the Blueprint, no changes are necessary. #####
|
|
||||||
#####################################################################################################
|
|
||||||
##### ATTENTION: This will add climate elements to the core system and requires the core part. #####
|
|
||||||
#####################################################################################################
|
|
||||||
|
|
||||||
substitutions:
|
|
||||||
### Local thermostat defaults ###
|
|
||||||
# https://esphome.io/components/climate/thermostat.html
|
|
||||||
temp_units: "°C"
|
|
||||||
heater_relay: "0" # Select 1 for "Relay 1", 2 for "Relay 2" or "0" to a dummy switch/disabled
|
|
||||||
cooler_relay: "0" # Select 1 for "Relay 1", 2 for "Relay 2" or "0" to a dummy switch/disabled
|
|
||||||
min_off_time: "300"
|
|
||||||
min_run_time: "300"
|
|
||||||
min_idle_time: "30"
|
|
||||||
# https://esphome.io/components/climate/index.html#base-climate-configuration
|
|
||||||
temp_min: "5"
|
|
||||||
temp_max: "45"
|
|
||||||
temp_step: "0.5"
|
|
||||||
|
|
||||||
##### DO NOT CHANGE THIS #####
|
|
||||||
addon_climate_cool: "false"
|
|
||||||
addon_climate_heat: "false"
|
|
||||||
##############################
|
|
||||||
|
|
||||||
climate:
|
|
||||||
- platform: thermostat
|
|
||||||
name: ${device_name} Thermostat
|
|
||||||
id: thermostat_embedded
|
|
||||||
sensor: temp_nspanel
|
|
||||||
min_idle_time: ${min_idle_time}s
|
|
||||||
visual:
|
|
||||||
min_temperature: ${temp_min} ${temp_units}
|
|
||||||
max_temperature: ${temp_max} ${temp_units}
|
|
||||||
temperature_step: ${temp_step} ${temp_units}
|
|
||||||
idle_action:
|
|
||||||
- switch.turn_off: relay_${heater_relay}
|
|
||||||
default_preset: "Off"
|
|
||||||
on_boot_restore_from: memory
|
|
||||||
internal: false
|
|
||||||
on_state:
|
|
||||||
- logger.log: Climate state changed - Start
|
|
||||||
- script.execute: addon_climate_update_page_climate
|
|
||||||
- script.execute: addon_climate_update_page_home
|
|
||||||
- logger.log: Climate state changed - End
|
|
||||||
|
|
||||||
globals:
|
|
||||||
##### Is embedded thermostat visible on climate page? #####
|
|
||||||
- id: is_addon_climate_visible
|
|
||||||
type: bool
|
|
||||||
restore_value: false
|
|
||||||
initial_value: 'false'
|
|
||||||
##### Embeded climate friendly name #####
|
|
||||||
- id: addon_climate_friendly_name
|
|
||||||
type: std::string
|
|
||||||
restore_value: false
|
|
||||||
initial_value: '"${device_name} Thermostat"'
|
|
||||||
|
|
||||||
switch:
|
|
||||||
##### PHYSICAL SWITCH 0 (Dummy) - Used when relay is not set #####
|
|
||||||
- name: ${device_name} Relay 0 (dummy)
|
|
||||||
platform: template
|
|
||||||
id: relay_0
|
|
||||||
lambda: !lambda return false;
|
|
||||||
internal: true
|
|
||||||
optimistic: true
|
|
||||||
|
|
||||||
script:
|
|
||||||
- id: !extend addon_climate_update_page_home
|
|
||||||
mode: restart
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
// Update home.climate_entity variable
|
|
||||||
detailed_entity->publish_state((id(is_embedded_thermostat)) ? "embedded_climate" : "");
|
|
||||||
disp1->set_component_value("climate.embedded", (id(is_embedded_thermostat)) ? 1 : 0);
|
|
||||||
// Update chips
|
|
||||||
if (id(is_embedded_thermostat))
|
|
||||||
id(update_climate_icon).execute("home.icon_top_03", int(thermostat_embedded->action), int(thermostat_embedded->mode));
|
|
||||||
|
|
||||||
- id: !extend addon_climate_service_call
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
id(is_addon_climate_visible) = true;
|
|
||||||
disp1->set_component_value("climate.embedded", 1);
|
|
||||||
auto call = thermostat_embedded->make_call();
|
|
||||||
if (key == "set_temperature")
|
|
||||||
call.set_target_temperature(stof(value) / 10);
|
|
||||||
else if (key == "hvac_mode")
|
|
||||||
call.set_mode(value);
|
|
||||||
call.perform();
|
|
||||||
|
|
||||||
- id: !extend addon_climate_set_climate
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
id(is_addon_climate_visible) = embedded_climate;
|
|
||||||
|
|
||||||
- id: !extend addon_climate_update_page_climate
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
if (current_page->state == "climate" and id(is_addon_climate_visible))
|
|
||||||
{
|
|
||||||
disp1->set_component_text_printf("page_label", id(addon_climate_friendly_name).c_str());
|
|
||||||
float temp_step = ${temp_step};
|
|
||||||
float temp_offset = ${temp_min};
|
|
||||||
float temp_max = ${temp_max};
|
|
||||||
float total_steps = (temp_max-temp_offset)/temp_step;
|
|
||||||
set_climate->execute
|
|
||||||
(
|
|
||||||
thermostat_embedded->current_temperature, // current_temp
|
|
||||||
thermostat_embedded->target_temperature, // target_temp
|
|
||||||
int(round(${temp_step}*10)), // temp_step
|
|
||||||
int(round(total_steps)), // total_steps //int(round((10*thermostat_embedded->target_temperature-temp_offset)/temp_step)), // slider_val
|
|
||||||
int(round(${temp_min}*10)), // temp_offset
|
|
||||||
"", // climate_icon
|
|
||||||
true // embedded_climate
|
|
||||||
);
|
|
||||||
|
|
||||||
// Update target temp icon
|
|
||||||
update_climate_icon->execute("climate.target_icon", int(thermostat_embedded->action), int(thermostat_embedded->mode));
|
|
||||||
|
|
||||||
// Update buttons bar
|
|
||||||
// Hide not supported hotspots
|
|
||||||
disp1->hide_component("button01");
|
|
||||||
disp1->hide_component("button02");
|
|
||||||
if (${addon_climate_heat}) disp1->show_component("button03"); else disp1->hide_component("button03"); //Heat
|
|
||||||
if (${addon_climate_cool}) disp1->show_component("button04"); else disp1->hide_component("button04"); //Cool
|
|
||||||
disp1->hide_component("button05");
|
|
||||||
disp1->hide_component("button06");
|
|
||||||
disp1->show_component("button07"); //Off
|
|
||||||
// Set buttons colors
|
|
||||||
disp1->set_component_font_color("climate.button01_icon", 6339);
|
|
||||||
disp1->set_component_font_color("climate.button02_icon", 6339);
|
|
||||||
disp1->set_component_font_color("climate.button03_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_HEAT) ? 64164 : ((${addon_climate_heat}) ? 48631 : 6339));
|
|
||||||
disp1->set_component_font_color("climate.button04_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_COOL) ? 1055 : ((${addon_climate_cool}) ? 48631 : 6339));
|
|
||||||
disp1->set_component_font_color("climate.button05_icon", 6339);
|
|
||||||
disp1->set_component_font_color("climate.button06_icon", 6339);
|
|
||||||
disp1->set_component_font_color("climate.button07_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_OFF) ? 10597 : 35921);
|
|
||||||
}
|
|
||||||
|
|
||||||
- id: !extend addon_climate_set_climate_friendly_name
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
id(addon_climate_friendly_name) = friendly_name;
|
|
||||||
@@ -34,4 +34,4 @@ climate:
|
|||||||
default_target_temperature_high: 21 ${temp_units}
|
default_target_temperature_high: 21 ${temp_units}
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
core_package: !include nspanel_esphome_addon_climate_base.yaml
|
base_climate_package: !include advanced/esphome/nspanel_esphome_addon_climate_base.yaml
|
||||||
|
|||||||
@@ -34,4 +34,4 @@ climate:
|
|||||||
default_target_temperature_low: 21 ${temp_units}
|
default_target_temperature_low: 21 ${temp_units}
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
core_package: !include nspanel_esphome_addon_climate_base.yaml
|
base_climate_package: !include advanced/esphome/nspanel_esphome_addon_climate_base.yaml
|
||||||
|
|||||||
@@ -1,805 +0,0 @@
|
|||||||
#####################################################################################################
|
|
||||||
##### NSPANEL ESPHOME created by Blackymas - https://github.com/Blackymas/NSPanel_HA_Blueprint #####
|
|
||||||
##### TFT Upload engine #####
|
|
||||||
##### PLEASE only make changes if it is necessary and also the required knowledge is available. #####
|
|
||||||
##### For normal use with the Blueprint, no changes are necessary. #####
|
|
||||||
#####################################################################################################
|
|
||||||
##### ATTENTION: This will add advanced elements to the core system and requires the core part. #####
|
|
||||||
#####################################################################################################
|
|
||||||
|
|
||||||
substitutions:
|
|
||||||
|
|
||||||
################## Defaults ##################
|
|
||||||
# Just in case user forgets to set something #
|
|
||||||
nextion_update_url: "http://github.com/Blackymas/NSPanel_HA_Blueprint/raw/main/custom_configuration/nspanel_blank.tft"
|
|
||||||
##############################################
|
|
||||||
|
|
||||||
##### DON'T CHANGE THIS #####
|
|
||||||
upload_tft_chunk_size_max: "32768"
|
|
||||||
#############################
|
|
||||||
|
|
||||||
external_components:
|
|
||||||
- source: github://pr#3256 # adds esp-idf support to http_request
|
|
||||||
components:
|
|
||||||
- http_request
|
|
||||||
# - source: github://pr#5484 # adds exit reparse to Nextion library
|
|
||||||
# components:
|
|
||||||
# - nextion
|
|
||||||
|
|
||||||
##### HTTP REQUEST #####
|
|
||||||
# Enables http client #
|
|
||||||
# for upload_tft. #
|
|
||||||
########################
|
|
||||||
http_request:
|
|
||||||
id: httpclient
|
|
||||||
|
|
||||||
button:
|
|
||||||
##### UPDATE TFT DISPLAY #####
|
|
||||||
- name: ${device_name} Update TFT display
|
|
||||||
platform: template
|
|
||||||
icon: mdi:file-sync
|
|
||||||
id: tft_update
|
|
||||||
entity_category: config
|
|
||||||
on_press:
|
|
||||||
- logger.log: "Button pressed: Update TFT display"
|
|
||||||
- lambda: |-
|
|
||||||
upload_tft->execute("${nextion_update_url}");
|
|
||||||
|
|
||||||
api:
|
|
||||||
services:
|
|
||||||
##### SERVICE TO UPDATE THE TFT FILE from URL #####
|
|
||||||
##### It will use the default url if url is empty or "default"
|
|
||||||
- service: upload_tft_url
|
|
||||||
variables:
|
|
||||||
url: string
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
static const char *const TAG = "service.upload_tft_url";
|
|
||||||
ESP_LOGVV(TAG, "Starting...");
|
|
||||||
|
|
||||||
std::string clean_url = url;
|
|
||||||
// Convert to lowercase
|
|
||||||
std::transform(clean_url.begin(), clean_url.end(), clean_url.begin(),
|
|
||||||
[](unsigned char c){ return std::tolower(c); });
|
|
||||||
// Trim trailing spaces
|
|
||||||
auto endPos = clean_url.find_last_not_of(" \t");
|
|
||||||
if (std::string::npos != endPos) {
|
|
||||||
clean_url = clean_url.substr(0, endPos + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( clean_url.empty() or clean_url == "default") url = "${nextion_update_url}";
|
|
||||||
upload_tft->execute(url.c_str());
|
|
||||||
|
|
||||||
display:
|
|
||||||
- id: !extend disp1
|
|
||||||
#tft_url: ${nextion_update_url}
|
|
||||||
|
|
||||||
script:
|
|
||||||
- id: upload_tft_new #NOT IN USE FOR NOW
|
|
||||||
mode: single
|
|
||||||
parameters:
|
|
||||||
url: string
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
static const char *const TAG = "script.upload_tft";
|
|
||||||
ESP_LOGVV(TAG, "Starting...");
|
|
||||||
|
|
||||||
nextion_init->state = false;
|
|
||||||
|
|
||||||
auto delay_seconds_ = [](int seconds) {
|
|
||||||
ESP_LOGD(TAG, "Wait %i seconds", seconds);
|
|
||||||
for (int i = 0; i < (seconds*4); i++) {
|
|
||||||
#ifdef ARDUINO
|
|
||||||
delay(250);
|
|
||||||
#elif defined(USE_ESP_IDF)
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(250));
|
|
||||||
#endif
|
|
||||||
App.feed_wdt();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Setting TFT url: %s", url.c_str());
|
|
||||||
//disp1->set_tft_url(url.c_str());
|
|
||||||
unsigned int upload_tries = 0;
|
|
||||||
while (upload_tries < 3) {
|
|
||||||
upload_tries++;
|
|
||||||
ESP_LOGD(TAG, "Try #%i", upload_tries);
|
|
||||||
nextion_status->execute();
|
|
||||||
if (!disp1->is_setup()) {
|
|
||||||
ESP_LOGD(TAG, "Setting Nextion protocol reparse mode to passive");
|
|
||||||
exit_reparse->execute();
|
|
||||||
delay_seconds_(5);
|
|
||||||
}
|
|
||||||
ESP_LOGV(TAG, "Calling upload from Nextion component");
|
|
||||||
//if (disp1->upload_tft()) id(restart_nspanel).press();
|
|
||||||
ESP_LOGD(TAG, "Turn off Nextion");
|
|
||||||
screen_power->turn_off();
|
|
||||||
delay_seconds_(3);
|
|
||||||
ESP_LOGD(TAG, "Turn on Nextion");
|
|
||||||
screen_power->turn_on();
|
|
||||||
delay_seconds_(10);
|
|
||||||
}
|
|
||||||
ESP_LOGE(TAG, "TFT upload failed.");
|
|
||||||
ESP_LOGD(TAG, "Turn off Nextion");
|
|
||||||
screen_power->turn_off();
|
|
||||||
delay_seconds_(2);
|
|
||||||
ESP_LOGD(TAG, "Turn on Nextion");
|
|
||||||
screen_power->turn_on();
|
|
||||||
ESP_LOGD(TAG, "Restarting esphome");
|
|
||||||
delay_seconds_(1);
|
|
||||||
restart_nspanel->press();
|
|
||||||
nextion_init->state = true;
|
|
||||||
ESP_LOGV(TAG, "Finished!");
|
|
||||||
|
|
||||||
- id: upload_tft
|
|
||||||
mode: single
|
|
||||||
parameters:
|
|
||||||
url: string
|
|
||||||
then:
|
|
||||||
- lambda: |-
|
|
||||||
static const char *const TAG = "script.upload_tft";
|
|
||||||
ESP_LOGD(TAG, "Starting...");
|
|
||||||
|
|
||||||
nextion_init->publish_state(false);
|
|
||||||
stop_all->execute();
|
|
||||||
disp1->set_backlight_brightness(1);
|
|
||||||
disp1->hide_component("255");
|
|
||||||
|
|
||||||
std::vector<uint8_t> buffer_;
|
|
||||||
|
|
||||||
bool is_updating_ = false;
|
|
||||||
|
|
||||||
bool upload_first_chunk_sent_ = false;
|
|
||||||
|
|
||||||
int content_length_ = 0;
|
|
||||||
int tft_size_ = 0;
|
|
||||||
|
|
||||||
auto set_baud_rate_ = [](int baud_rate) {
|
|
||||||
#ifdef USE_ARDUINO
|
|
||||||
auto *uart = reinterpret_cast<uart::ESP32ArduinoUARTComponent *>(tf_uart);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_ESP_IDF
|
|
||||||
auto *uart = reinterpret_cast<uart::IDFUARTComponent *>(tf_uart);
|
|
||||||
#endif
|
|
||||||
uart->set_baud_rate(baud_rate);
|
|
||||||
uart->setup();
|
|
||||||
};
|
|
||||||
|
|
||||||
auto delay_seconds_ = [](int seconds) {
|
|
||||||
ESP_LOGD(TAG, "Wait %i seconds", seconds);
|
|
||||||
for (int i = 0; i < (seconds*4); i++) {
|
|
||||||
#ifdef ARDUINO
|
|
||||||
delay(250);
|
|
||||||
#elif defined(USE_ESP_IDF)
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(250));
|
|
||||||
#endif
|
|
||||||
App.feed_wdt();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
auto send_nextion_command = [](const std::string &command) -> bool
|
|
||||||
{
|
|
||||||
static const char *const TAG = "script.upload_tft.send_nextion_command";
|
|
||||||
ESP_LOGD(TAG, "Sending: %s", command.c_str());
|
|
||||||
id(tf_uart).write_str(command.c_str());
|
|
||||||
const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
|
|
||||||
id(tf_uart).write_array(to_send, sizeof(to_send));
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto recv_ret_string_ = [](std::string &response, uint32_t timeout, bool recv_flag) -> uint16_t
|
|
||||||
{
|
|
||||||
static const char *const TAG = "script.upload_tft.recv_ret_string_";
|
|
||||||
uint16_t ret;
|
|
||||||
uint8_t c = 0;
|
|
||||||
uint8_t nr_of_ff_bytes = 0;
|
|
||||||
uint64_t start;
|
|
||||||
bool exit_flag = false;
|
|
||||||
bool ff_flag = false;
|
|
||||||
|
|
||||||
start = millis();
|
|
||||||
|
|
||||||
while ((timeout == 0 && id(tf_uart).available()) || millis() - start <= timeout)
|
|
||||||
{
|
|
||||||
if (!id(tf_uart).available())
|
|
||||||
{
|
|
||||||
App.feed_wdt();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
id(tf_uart).read_byte(&c);
|
|
||||||
if (c == 0xFF)
|
|
||||||
{
|
|
||||||
nr_of_ff_bytes++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nr_of_ff_bytes = 0;
|
|
||||||
ff_flag = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nr_of_ff_bytes >= 3)
|
|
||||||
ff_flag = true;
|
|
||||||
|
|
||||||
response += (char) c;
|
|
||||||
if (recv_flag)
|
|
||||||
{
|
|
||||||
if (response.find(0x05) != std::string::npos)
|
|
||||||
{
|
|
||||||
exit_flag = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
App.feed_wdt();
|
|
||||||
delay(2);
|
|
||||||
|
|
||||||
if (exit_flag || ff_flag)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ff_flag)
|
|
||||||
response = response.substr(0, response.length() - 3); // Remove last 3 0xFF
|
|
||||||
|
|
||||||
ret = response.length();
|
|
||||||
return ret;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto upload_end_ = [&](bool completed) -> bool
|
|
||||||
{
|
|
||||||
static const char *const TAG = "script.upload_tft.upload_end_";
|
|
||||||
ESP_LOGD(TAG, "Completed: %i", completed ? 1 : 0);
|
|
||||||
//ESP_LOGD(TAG, "Restarting Nextion");
|
|
||||||
//send_nextion_command("rest");
|
|
||||||
#ifdef ARDUINO
|
|
||||||
delay(1500);
|
|
||||||
#elif defined(ESP_PLATFORM)
|
|
||||||
//vTaskDelay(pdMS_TO_TICKS(1500));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
is_updating_ = false;
|
|
||||||
if (!completed) ESP_LOGD(TAG, "Nextion TFT upload will try again");
|
|
||||||
return completed;
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef ARDUINO // arduino # To do: Move to Nextion component on ESPHome
|
|
||||||
size_t transfer_buffer_size_ = 0;
|
|
||||||
uint8_t *transfer_buffer_{nullptr};
|
|
||||||
auto upload_by_chunks_arduino = [&](HTTPClient *http, const std::string &url, int range_start) -> int
|
|
||||||
{
|
|
||||||
static const char *const TAG = "script.upload_tft.upload_by_chunks_arduino";
|
|
||||||
int range_end;
|
|
||||||
|
|
||||||
if (range_start == 0 && transfer_buffer_size_ > 16384) { // Start small at the first run in case of a big skip
|
|
||||||
range_end = 16384 - 1;
|
|
||||||
} else {
|
|
||||||
range_end = range_start + transfer_buffer_size_ - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (range_end > tft_size_)
|
|
||||||
range_end = tft_size_;
|
|
||||||
|
|
||||||
char range_header[64];
|
|
||||||
sprintf(range_header, "bytes=%d-%d", range_start, range_end);
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Requesting range: %s", range_header);
|
|
||||||
|
|
||||||
int tries = 1;
|
|
||||||
int code;
|
|
||||||
bool begin_status;
|
|
||||||
while (tries <= 10) {
|
|
||||||
begin_status = http->begin(url.c_str());
|
|
||||||
|
|
||||||
++tries;
|
|
||||||
if (!begin_status) {
|
|
||||||
ESP_LOGD(TAG, "Connection failed");
|
|
||||||
delay(1000);
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
http->addHeader("Range", range_header);
|
|
||||||
|
|
||||||
code = http->GET();
|
|
||||||
if (code == 200 || code == 206) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s, retries(%d/10)", url.c_str(),
|
|
||||||
HTTPClient::errorToString(code).c_str(), tries);
|
|
||||||
http->end();
|
|
||||||
delay(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tries > 10) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string recv_string;
|
|
||||||
size_t size;
|
|
||||||
int fetched = 0;
|
|
||||||
int range = range_end - range_start;
|
|
||||||
int write_len;
|
|
||||||
|
|
||||||
// fetch next segment from HTTP stream
|
|
||||||
while (fetched < range) {
|
|
||||||
size = http->getStreamPtr()->available();
|
|
||||||
if (!size) {
|
|
||||||
App.feed_wdt();
|
|
||||||
delay(2);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
int c = http->getStreamPtr()->readBytes(
|
|
||||||
&transfer_buffer_[fetched], ((size > transfer_buffer_size_) ? transfer_buffer_size_ : size));
|
|
||||||
fetched += c;
|
|
||||||
}
|
|
||||||
http->end();
|
|
||||||
ESP_LOGD(TAG, "Fetched %d bytes", fetched);
|
|
||||||
|
|
||||||
// upload fetched segments to the display in 4KB chunks
|
|
||||||
for (int i = 0; i < range; i += 4096) {
|
|
||||||
App.feed_wdt();
|
|
||||||
write_len = content_length_ < 4096 ? content_length_ : 4096;
|
|
||||||
id(tf_uart).write_array(&transfer_buffer_[i], write_len);
|
|
||||||
content_length_ -= write_len;
|
|
||||||
ESP_LOGD(TAG, "Uploaded %0.1f %%, remaining %d bytes",
|
|
||||||
100.0 * (tft_size_ - content_length_) / tft_size_,
|
|
||||||
content_length_);
|
|
||||||
|
|
||||||
if (!upload_first_chunk_sent_) {
|
|
||||||
upload_first_chunk_sent_ = true;
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
recv_ret_string_(recv_string, 5000, true);
|
|
||||||
if (recv_string[0] != 0x05) { // 0x05 == "ok"
|
|
||||||
ESP_LOGD(TAG, "recv_string [%s]",
|
|
||||||
format_hex_pretty(reinterpret_cast<const uint8_t *>(recv_string.data()), recv_string.size()).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle partial upload request
|
|
||||||
if (recv_string[0] == 0x08 && recv_string.size() == 5) {
|
|
||||||
uint32_t result = 0;
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
result += static_cast<uint8_t>(recv_string[j + 1]) << (8 * j);
|
|
||||||
}
|
|
||||||
if (result > 0) {
|
|
||||||
ESP_LOGD(TAG, "Nextion reported new range %d", result);
|
|
||||||
content_length_ = tft_size_ - result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
recv_string.clear();
|
|
||||||
}
|
|
||||||
return range_end + 1;
|
|
||||||
};
|
|
||||||
auto upload_tft_ = [&](const std::string &url, unsigned int update_baud_rate_) -> bool
|
|
||||||
{
|
|
||||||
static const char *const TAG = "script.upload_tft.upload_tft_arduino";
|
|
||||||
ESP_LOGD(TAG, "Nextion TFT upload requested");
|
|
||||||
ESP_LOGD(TAG, " url: %s", url.c_str());
|
|
||||||
ESP_LOGD(TAG, " baud_rate: %i", update_baud_rate_);
|
|
||||||
|
|
||||||
if (is_updating_) {
|
|
||||||
ESP_LOGD(TAG, "Currently updating");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!network::is_connected()) {
|
|
||||||
ESP_LOGD(TAG, "Network is not connected");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!disp1->is_setup()) {
|
|
||||||
ESP_LOGD(TAG, "Setting Nextion protocol reparse mode to passive");
|
|
||||||
exit_reparse->execute();
|
|
||||||
delay_seconds_(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
is_updating_ = true;
|
|
||||||
|
|
||||||
HTTPClient http;
|
|
||||||
http.setTimeout(15000); // Yes 15 seconds.... Helps 8266s along
|
|
||||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
|
||||||
bool begin_status = http.begin(url.c_str());
|
|
||||||
if (!begin_status) {
|
|
||||||
is_updating_ = false;
|
|
||||||
ESP_LOGD(TAG, "Connection failed");
|
|
||||||
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
|
||||||
allocator.deallocate(transfer_buffer_, transfer_buffer_size_);
|
|
||||||
return upload_end_(false);
|
|
||||||
} else {
|
|
||||||
ESP_LOGD(TAG, "Connected");
|
|
||||||
}
|
|
||||||
|
|
||||||
http.addHeader("Range", "bytes=0-255");
|
|
||||||
const char *header_names[] = {"Content-Range"};
|
|
||||||
http.collectHeaders(header_names, 1);
|
|
||||||
ESP_LOGD(TAG, "Requesting URL: %s", url.c_str());
|
|
||||||
|
|
||||||
http.setReuse(true);
|
|
||||||
// try up to 5 times. DNS sometimes needs a second try or so
|
|
||||||
int tries = 1;
|
|
||||||
int code = http.GET();
|
|
||||||
delay(100);
|
|
||||||
|
|
||||||
while (code != 200 && code != 206 && tries <= 5) {
|
|
||||||
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s, retrying (%d/5)", url.c_str(),
|
|
||||||
HTTPClient::errorToString(code).c_str(), tries);
|
|
||||||
|
|
||||||
delay(250);
|
|
||||||
code = http.GET();
|
|
||||||
++tries;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((code != 200 && code != 206) || tries > 5) {
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
String content_range_string = http.header("Content-Range");
|
|
||||||
content_range_string.remove(0, 12);
|
|
||||||
content_length_ = content_range_string.toInt();
|
|
||||||
tft_size_ = content_length_;
|
|
||||||
http.end();
|
|
||||||
|
|
||||||
if (content_length_ < 4096) {
|
|
||||||
ESP_LOGE(TAG, "Failed to get file size");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Updating Nextion");
|
|
||||||
// The Nextion will ignore the update command if it is sleeping
|
|
||||||
|
|
||||||
char command[128];
|
|
||||||
// Tells the Nextion the content length of the tft file and baud rate it will be sent at
|
|
||||||
// Once the Nextion accepts the command it will wait until the file is successfully uploaded
|
|
||||||
// If it fails for any reason a power cycle of the display will be needed
|
|
||||||
sprintf(command, "whmi-wris %d,%d,1", content_length_, update_baud_rate_);
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Clear serial receive buffer: %d", id(tf_uart).available());
|
|
||||||
// Clear serial receive buffer
|
|
||||||
uint8_t d;
|
|
||||||
while (id(tf_uart).available()) {
|
|
||||||
id(tf_uart).read_byte(&d);
|
|
||||||
};
|
|
||||||
|
|
||||||
send_nextion_command(command);
|
|
||||||
|
|
||||||
if (update_baud_rate_ != id(tf_uart).get_baud_rate()) {
|
|
||||||
set_baud_rate_(update_baud_rate_);
|
|
||||||
//id(tf_uart).set_baud_rate(update_baud_rate_);
|
|
||||||
//id(tf_uart).setup();
|
|
||||||
//delay_seconds_(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Waiting for upgrade response");
|
|
||||||
std::string response;
|
|
||||||
recv_ret_string_(response, 5000, true); // This can take some time to return
|
|
||||||
// The Nextion display will, if it's ready to accept data, send a 0x05 byte.
|
|
||||||
ESP_LOGD(TAG, "Upgrade response is [%s]",
|
|
||||||
format_hex_pretty(reinterpret_cast<const uint8_t *>(response.data()), response.size()).c_str());
|
|
||||||
|
|
||||||
if (response.find(0x05) != std::string::npos) {
|
|
||||||
ESP_LOGD(TAG, "Preparation for tft update done");
|
|
||||||
} else {
|
|
||||||
ESP_LOGD(TAG, "Preparation for tft update failed %d \"%s\"", response[0], response.c_str());
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nextion wants 4096 bytes at a time. Make chunk_size a multiple of 4096
|
|
||||||
uint32_t chunk_size = 8192;
|
|
||||||
if (ESP.getFreeHeap() > 81920) { // Ensure some FreeHeap to other things and limit chunk size
|
|
||||||
chunk_size = ESP.getFreeHeap() - 65536;
|
|
||||||
chunk_size = int(chunk_size / 4096) * 4096;
|
|
||||||
chunk_size = chunk_size > ${upload_tft_chunk_size_max} ? ${upload_tft_chunk_size_max} : chunk_size;
|
|
||||||
} else if (ESP.getFreeHeap() < 32768) {
|
|
||||||
chunk_size = 4096;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (transfer_buffer_ == nullptr) {
|
|
||||||
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
|
||||||
ESP_LOGD(TAG, "Allocating buffer size %d, Heap size is %" PRIu32, chunk_size, ESP.getFreeHeap());
|
|
||||||
transfer_buffer_ = allocator.allocate(chunk_size);
|
|
||||||
if (transfer_buffer_ == nullptr) { // Try a smaller size
|
|
||||||
ESP_LOGD(TAG, "Could not allocate buffer size: %d trying 4096 instead", chunk_size);
|
|
||||||
chunk_size = 4096;
|
|
||||||
ESP_LOGD(TAG, "Allocating %d buffer", chunk_size);
|
|
||||||
transfer_buffer_ = allocator.allocate(chunk_size);
|
|
||||||
|
|
||||||
if (!transfer_buffer_)
|
|
||||||
{
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
transfer_buffer_size_ = chunk_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Updating tft from \"%s\" with a file size of %d using %zu chunksize, Heap Size %" PRIu32,
|
|
||||||
url.c_str(), content_length_, transfer_buffer_size_, ESP.getFreeHeap());
|
|
||||||
|
|
||||||
int result = 0;
|
|
||||||
while (content_length_ > 0) {
|
|
||||||
result = upload_by_chunks_arduino(&http, url, result);
|
|
||||||
if (result < 0) {
|
|
||||||
ESP_LOGD(TAG, "Error updating Nextion!");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
App.feed_wdt();
|
|
||||||
ESP_LOGD(TAG, "Heap Size %" PRIu32 ", Bytes left %d", ESP.getFreeHeap(), content_length_);
|
|
||||||
}
|
|
||||||
is_updating_ = false;
|
|
||||||
ESP_LOGD(TAG, "Successfully updated Nextion!");
|
|
||||||
|
|
||||||
return upload_end_(true);
|
|
||||||
};
|
|
||||||
#elif defined(ESP_PLATFORM) // esp-idf # To do: Move to Nextion component on ESPHome
|
|
||||||
auto upload_range_esp_idf_ = [&](const std::string &url, int range_start) -> int {
|
|
||||||
static const char *const TAG = "script.upload_tft.upload_range_esp_idf_";
|
|
||||||
ESP_LOGVV(TAG, "url: %s", url.c_str());
|
|
||||||
uint range_size_ = tft_size_ - range_start;
|
|
||||||
ESP_LOGVV(TAG, "tft_size_: %i", tft_size_);
|
|
||||||
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
int range_end = (range_start == 0) ? std::min(tft_size_, 16383) : tft_size_;
|
|
||||||
if (range_size_ <= 0 or range_end <= range_start) {
|
|
||||||
ESP_LOGE(TAG, "Invalid range");
|
|
||||||
ESP_LOGD(TAG, "Range start: %i", range_start);
|
|
||||||
ESP_LOGD(TAG, "Range end: %i", range_end);
|
|
||||||
ESP_LOGD(TAG, "Range size: %i", range_size_);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_http_client_config_t config = {
|
|
||||||
.url = url.c_str(),
|
|
||||||
.cert_pem = nullptr,
|
|
||||||
};
|
|
||||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
|
||||||
|
|
||||||
char range_header[64];
|
|
||||||
sprintf(range_header, "bytes=%d-%d", range_start, range_end);
|
|
||||||
ESP_LOGV(TAG, "Requesting range: %s", range_header);
|
|
||||||
esp_http_client_set_header(client, "Range", range_header);
|
|
||||||
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
ESP_LOGV(TAG, "Opening http connetion");
|
|
||||||
esp_err_t err;
|
|
||||||
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
|
||||||
esp_http_client_cleanup(client);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Fetch content length");
|
|
||||||
int content_length = esp_http_client_fetch_headers(client);
|
|
||||||
ESP_LOGV(TAG, "content_length = %d", content_length);
|
|
||||||
if (content_length <= 0) {
|
|
||||||
ESP_LOGE(TAG, "Failed to get content length: %d", content_length);
|
|
||||||
esp_http_client_cleanup(client);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int total_read_len = 0, read_len;
|
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Allocate buffer");
|
|
||||||
uint8_t* buffer = new uint8_t[4096];
|
|
||||||
std::string recv_string;
|
|
||||||
if (buffer == nullptr) {
|
|
||||||
ESP_LOGE(TAG, "Failed to allocate memory for buffer");
|
|
||||||
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
} else {
|
|
||||||
ESP_LOGV(TAG, "Memory for buffer allocated successfully");
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
App.feed_wdt();
|
|
||||||
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
int read_len = esp_http_client_read(client, reinterpret_cast<char*>(buffer), 4096);
|
|
||||||
ESP_LOGVV(TAG, "Read %d bytes from HTTP client, writing to UART", read_len);
|
|
||||||
if (read_len > 0) {
|
|
||||||
tf_uart->write_array(buffer, read_len);
|
|
||||||
ESP_LOGVV(TAG, "Write to UART successful");
|
|
||||||
recv_ret_string_(recv_string, 5000, true);
|
|
||||||
content_length_ -= read_len;
|
|
||||||
ESP_LOGD(TAG, "Uploaded %0.2f %%, remaining %d bytes",
|
|
||||||
100.0 * (tft_size_ - content_length_) / tft_size_,
|
|
||||||
content_length_);
|
|
||||||
if (recv_string[0] != 0x05) { // 0x05 == "ok"
|
|
||||||
ESP_LOGD(TAG, "recv_string [%s]",
|
|
||||||
format_hex_pretty(reinterpret_cast<const uint8_t *>(recv_string.data()), recv_string.size()).c_str());
|
|
||||||
}
|
|
||||||
// handle partial upload request
|
|
||||||
if (recv_string[0] == 0x08 && recv_string.size() == 5) {
|
|
||||||
uint32_t result = 0;
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
result += static_cast<uint8_t>(recv_string[j + 1]) << (8 * j);
|
|
||||||
}
|
|
||||||
if (result > 0) {
|
|
||||||
ESP_LOGI(TAG, "Nextion reported new range %" PRIu32, result);
|
|
||||||
content_length_ = tft_size_ - result;
|
|
||||||
// Deallocate the buffer when done
|
|
||||||
delete[] buffer;
|
|
||||||
ESP_LOGVV(TAG, "Memory for buffer deallocated");
|
|
||||||
esp_http_client_cleanup(client);
|
|
||||||
esp_http_client_close(client);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
recv_string.clear();
|
|
||||||
} else if (read_len == 0) {
|
|
||||||
ESP_LOGV(TAG, "End of HTTP response reached");
|
|
||||||
break; // Exit the loop if there is no more data to read
|
|
||||||
} else {
|
|
||||||
ESP_LOGE(TAG, "Failed to read from HTTP client, error code: %d", read_len);
|
|
||||||
break; // Exit the loop on error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deallocate the buffer when done
|
|
||||||
delete[] buffer;
|
|
||||||
ESP_LOGVV(TAG, "Memory for buffer deallocated");
|
|
||||||
}
|
|
||||||
esp_http_client_cleanup(client);
|
|
||||||
esp_http_client_close(client);
|
|
||||||
return range_end + 1;
|
|
||||||
};
|
|
||||||
auto upload_tft_ = [&](const std::string &url, unsigned int update_baud_rate_) -> bool {
|
|
||||||
static const char *const TAG = "script.upload_tft.upload_tft_esp_idf";
|
|
||||||
ESP_LOGD(TAG, "Nextion TFT upload requested");
|
|
||||||
ESP_LOGD(TAG, " url: %s", url.c_str());
|
|
||||||
ESP_LOGD(TAG, " baud_rate: %i", update_baud_rate_);
|
|
||||||
|
|
||||||
if (is_updating_) {
|
|
||||||
ESP_LOGW(TAG, "Currently updating");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!network::is_connected()) {
|
|
||||||
ESP_LOGE(TAG, "Network is not connected");
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!disp1->is_setup()) {
|
|
||||||
ESP_LOGD(TAG, "Setting Nextion protocol reparse mode to passive");
|
|
||||||
exit_reparse->execute();
|
|
||||||
delay_seconds_(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
is_updating_ = true;
|
|
||||||
|
|
||||||
// Define the configuration for the HTTP client
|
|
||||||
ESP_LOGV(TAG, "Establishing connection to HTTP server");
|
|
||||||
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
esp_http_client_config_t config = {
|
|
||||||
.url = url.c_str(),
|
|
||||||
.cert_pem = nullptr,
|
|
||||||
.method = HTTP_METHOD_HEAD,
|
|
||||||
.timeout_ms = 15000,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Initialize the HTTP client with the configuration
|
|
||||||
ESP_LOGV(TAG, "Initializing HTTP client");
|
|
||||||
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
esp_http_client_handle_t http = esp_http_client_init(&config);
|
|
||||||
if (!http) {
|
|
||||||
ESP_LOGE(TAG, "Failed to initialize HTTP client.");
|
|
||||||
return upload_end_(false); // return -1 to indicate an error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform the HTTP request
|
|
||||||
ESP_LOGV(TAG, "Check if the client could connect");
|
|
||||||
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
|
|
||||||
esp_err_t err = esp_http_client_perform(http);
|
|
||||||
if (err != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
|
|
||||||
esp_http_client_cleanup(http);
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the HTTP Status Code
|
|
||||||
int status_code = esp_http_client_get_status_code(http);
|
|
||||||
ESP_LOGV(TAG, "HTTP Status Code: %d", status_code);
|
|
||||||
size_t tft_file_size = esp_http_client_get_content_length(http);
|
|
||||||
ESP_LOGD(TAG, "TFT file size: %zu", tft_file_size);
|
|
||||||
|
|
||||||
if (tft_file_size < 4096) {
|
|
||||||
ESP_LOGE(TAG, "File size check failed. Size: %zu", tft_file_size);
|
|
||||||
esp_http_client_cleanup(http);
|
|
||||||
return upload_end_(false);
|
|
||||||
} else {
|
|
||||||
ESP_LOGD(TAG, "File size check passed. Proceeding...");
|
|
||||||
}
|
|
||||||
content_length_ = tft_file_size;
|
|
||||||
tft_size_ = tft_file_size;
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Updating Nextion");
|
|
||||||
// The Nextion will ignore the update command if it is sleeping
|
|
||||||
|
|
||||||
char command[128];
|
|
||||||
// Tells the Nextion the content length of the tft file and baud rate it will be sent at
|
|
||||||
// Once the Nextion accepts the command it will wait until the file is successfully uploaded
|
|
||||||
// If it fails for any reason a power cycle of the display will be needed
|
|
||||||
sprintf(command, "whmi-wris %d,%d,1", content_length_, update_baud_rate_);
|
|
||||||
|
|
||||||
// Clear serial receive buffer
|
|
||||||
uint8_t d;
|
|
||||||
while (id(tf_uart).available()) {
|
|
||||||
id(tf_uart).read_byte(&d);
|
|
||||||
};
|
|
||||||
|
|
||||||
send_nextion_command(command);
|
|
||||||
|
|
||||||
if (update_baud_rate_ != id(tf_uart).get_baud_rate()) {
|
|
||||||
set_baud_rate_(update_baud_rate_);
|
|
||||||
//id(tf_uart).set_baud_rate(update_baud_rate_);
|
|
||||||
//id(tf_uart).setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string response;
|
|
||||||
ESP_LOGD(TAG, "Waiting for upgrade response");
|
|
||||||
recv_ret_string_(response, 2000, true); // This can take some time to return
|
|
||||||
|
|
||||||
// The Nextion display will, if it's ready to accept data, send a 0x05 byte.
|
|
||||||
ESP_LOGD(TAG, "Upgrade response is [%s]",
|
|
||||||
format_hex_pretty(reinterpret_cast<const uint8_t *>(response.data()), response.size()).c_str());
|
|
||||||
|
|
||||||
if (response.find(0x05) != std::string::npos) {
|
|
||||||
ESP_LOGV(TAG, "Preparation for tft update done");
|
|
||||||
} else {
|
|
||||||
ESP_LOGE(TAG, "Preparation for tft update failed %d \"%s\"", response[0], response.c_str());
|
|
||||||
esp_http_client_cleanup(http);
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Updating tft from \"%s\" with a file size of %d, Heap Size %" PRIu32,
|
|
||||||
url.c_str(), content_length_, esp_get_free_heap_size());
|
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Starting transfer by chunks loop");
|
|
||||||
int result = 0;
|
|
||||||
while (content_length_ > 0) {
|
|
||||||
result = upload_range_esp_idf_(url.c_str(), result);
|
|
||||||
if (result < 0) {
|
|
||||||
ESP_LOGE(TAG, "Error updating Nextion!");
|
|
||||||
esp_http_client_cleanup(http);
|
|
||||||
return upload_end_(false);
|
|
||||||
}
|
|
||||||
App.feed_wdt();
|
|
||||||
ESP_LOGV(TAG, "Heap Size %" PRIu32 ", Bytes left %d", esp_get_free_heap_size(), content_length_);
|
|
||||||
}
|
|
||||||
|
|
||||||
is_updating_ = false;
|
|
||||||
ESP_LOGD(TAG, "Successfully updated Nextion!");
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Close HTTP connection");
|
|
||||||
esp_http_client_close(http);
|
|
||||||
esp_http_client_cleanup(http);
|
|
||||||
return upload_end_(true);
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Try #1 at 921600 bps");
|
|
||||||
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
|
||||||
ESP_LOGW(TAG, "Try #1 failed");
|
|
||||||
delay_seconds_(5);
|
|
||||||
ESP_LOGD(TAG, "Try #2 at 921600 bps");
|
|
||||||
if (upload_tft_(url, 921600)) id(restart_nspanel).press();
|
|
||||||
ESP_LOGW(TAG, "Try #2 failed");
|
|
||||||
delay_seconds_(5);
|
|
||||||
ESP_LOGD(TAG, "Try #3 at 115200 bps");
|
|
||||||
if (upload_tft_(url, 115200)) id(restart_nspanel).press();
|
|
||||||
ESP_LOGW(TAG, "Try #3 failed");
|
|
||||||
ESP_LOGD(TAG, "Turn off Nextion");
|
|
||||||
id(screen_power).turn_off();
|
|
||||||
delay_seconds_(2);
|
|
||||||
ESP_LOGD(TAG, "Turn on Nextion");
|
|
||||||
id(screen_power).turn_on();
|
|
||||||
delay_seconds_(10);
|
|
||||||
ESP_LOGD(TAG, "Try #4 at 115200 bps");
|
|
||||||
if (upload_tft_(url, 115200)) id(restart_nspanel).press();
|
|
||||||
ESP_LOGE(TAG, "TFT upload failed");
|
|
||||||
ESP_LOGD(TAG, "Turn off Nextion");
|
|
||||||
id(screen_power).turn_off();
|
|
||||||
delay_seconds_(2);
|
|
||||||
ESP_LOGD(TAG, "Turn on Nextion");
|
|
||||||
id(screen_power).turn_on();
|
|
||||||
ESP_LOGD(TAG, "Restarting ESPHome");
|
|
||||||
delay_seconds_(2);
|
|
||||||
id(restart_nspanel).press();
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Finished!");
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
#####################################################################################################
|
|
||||||
##### NSPANEL ESPHOME created by Blackymas - https://github.com/Blackymas/NSPanel_HA_Blueprint #####
|
|
||||||
##### ESPHOME ADVANCED #####
|
|
||||||
##### PLEASE only make changes if it is necessary and also the required knowledge is available. #####
|
|
||||||
##### For normal use with the Blueprint, no changes are necessary. #####
|
|
||||||
#####################################################################################################
|
|
||||||
##### ATTENTION: This will add advanced elements to the core system and requires the core part. #####
|
|
||||||
#####################################################################################################
|
|
||||||
|
|
||||||
button:
|
|
||||||
##### EXIT REPARSE TFT DISPLAY #####
|
|
||||||
- name: ${device_name} Exit reparse
|
|
||||||
platform: template
|
|
||||||
icon: mdi:file-sync
|
|
||||||
id: tft_reparse_off
|
|
||||||
entity_category: config
|
|
||||||
on_press:
|
|
||||||
- logger.log: "Button pressed: Exit reparse"
|
|
||||||
- script.execute: exit_reparse
|
|
||||||
|
|
||||||
captive_portal:
|
|
||||||
|
|
||||||
esp32:
|
|
||||||
framework:
|
|
||||||
type: esp-idf
|
|
||||||
|
|
||||||
sensor:
|
|
||||||
##### Uptime Sensors #####
|
|
||||||
- name: ${device_name} Uptime seconds
|
|
||||||
id: uptime_sec
|
|
||||||
platform: uptime
|
|
||||||
internal: true
|
|
||||||
|
|
||||||
- name: ${device_name} API uptime
|
|
||||||
id: api_timestamp
|
|
||||||
platform: template
|
|
||||||
lambda: 'return id(time_provider).now().timestamp;'
|
|
||||||
internal: false
|
|
||||||
device_class: timestamp
|
|
||||||
entity_category: diagnostic
|
|
||||||
accuracy_decimals: 0
|
|
||||||
update_interval: never
|
|
||||||
|
|
||||||
- name: ${device_name} Device uptime
|
|
||||||
id: device_timestamp
|
|
||||||
platform: template
|
|
||||||
lambda: 'return (id(time_provider).now().timestamp - id(uptime_sec).state);'
|
|
||||||
internal: false
|
|
||||||
device_class: timestamp
|
|
||||||
entity_category: diagnostic
|
|
||||||
accuracy_decimals: 0
|
|
||||||
update_interval: never
|
|
||||||
|
|
||||||
##### WIFI Signal stregth
|
|
||||||
- name: ${device_name} RSSI
|
|
||||||
platform: wifi_signal
|
|
||||||
update_interval: 60s
|
|
||||||
on_value:
|
|
||||||
- script.execute:
|
|
||||||
id: refresh_wifi_icon
|
|
||||||
|
|
||||||
text_sensor:
|
|
||||||
##### ESPhome version used to compile the app #####
|
|
||||||
- name: ${device_name} ESPhome Version
|
|
||||||
platform: version
|
|
||||||
disabled_by_default: true
|
|
||||||
|
|
||||||
- platform: wifi_info
|
|
||||||
ip_address:
|
|
||||||
name: ${device_name} IP
|
|
||||||
disabled_by_default: true
|
|
||||||
id: ip_address
|
|
||||||
ssid:
|
|
||||||
name: ${device_name} SSID
|
|
||||||
disabled_by_default: true
|
|
||||||
bssid:
|
|
||||||
name: ${device_name} BSSID
|
|
||||||
disabled_by_default: true
|
|
||||||
|
|
||||||
time:
|
|
||||||
- id: !extend time_provider
|
|
||||||
on_time_sync:
|
|
||||||
then:
|
|
||||||
- component.update: api_timestamp
|
|
||||||
- component.update: device_timestamp
|
|
||||||
|
|
||||||
web_server:
|
|
||||||
id: web_server_std
|
|
||||||
port: 80
|
|
||||||
auth:
|
|
||||||
username: admin
|
|
||||||
password: ${wifi_password}
|
|
||||||
File diff suppressed because it is too large
Load Diff
BIN
nspanel_eu.tft
BIN
nspanel_eu.tft
Binary file not shown.
BIN
nspanel_us.tft
BIN
nspanel_us.tft
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user