10static const char *
const TAG =
"nextion";
31#ifdef USE_NEXTION_COMMAND_SPACING
32 if (!this->
connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send()) {
33 ESP_LOGN(TAG,
"Command spacing: delaying command '%s'", command.c_str());
38 ESP_LOGN(TAG,
"cmd: %s", command.c_str());
41 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
51#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
52 ESP_LOGW(TAG,
"Connected (no handshake)");
61#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
78 if (!response.empty() && response[0] == 0x1A) {
80 ESP_LOGV(TAG,
"0x1A error ignored (setup)");
83 if (response.empty() || response.find(
"comok") == std::string::npos) {
84#ifdef NEXTION_PROTOCOL_LOG
85 ESP_LOGN(TAG,
"Bad connect: %s", response.c_str());
86 for (
size_t i = 0; i < response.length(); i++) {
87 ESP_LOGN(TAG,
"resp: %s %d %d %c", response.c_str(), i, response[i], response[i]);
91 ESP_LOGW(TAG,
"Not connected");
97 ESP_LOGI(TAG,
"Connected");
100 ESP_LOGN(TAG,
"connect: %s", response.c_str());
104 std::vector<std::string> connect_info;
105 while ((start = response.find_first_not_of(
',',
end)) != std::string::npos) {
106 end = response.find(
',', start);
107 connect_info.push_back(response.substr(start,
end - start));
112 ESP_LOGN(TAG,
"Connect info: %zu", connect_info.size());
113#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
120 " Device Model: %s\n"
122 " Serial Number: %s\n"
124 connect_info[2].c_str(), connect_info[3].c_str(), connect_info[5].c_str(), connect_info[6].c_str());
127 ESP_LOGE(TAG,
"Bad connect value: '%s'", response.c_str());
147 ESP_LOGCONFIG(TAG,
"Nextion:");
149#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
150 ESP_LOGCONFIG(TAG,
" Skip handshake: YES");
153#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
154 " Device Model: %s\n"
156 " Serial Number: %s\n"
159#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
160 " Exit reparse: YES\n"
162 " Wake On Touch: %s\n"
163 " Touch Timeout: %" PRIu16,
164#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
165 this->
device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(),
166 this->flash_size_.c_str(),
171#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
176 ESP_LOGCONFIG(TAG,
" Wake Up Page: %u", this->
wake_up_page_);
179#ifdef USE_NEXTION_CONF_START_UP_PAGE
185#ifdef USE_NEXTION_COMMAND_SPACING
189#ifdef USE_NEXTION_MAX_QUEUE_SIZE
233 binarysensortype->update_component();
236 sensortype->update_component();
239 switchtype->update_component();
242 textsensortype->update_component();
263 va_start(arg, format);
264 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
267 ESP_LOGW(TAG,
"Bad cmd format: '%s'", format);
278#ifdef NEXTION_PROTOCOL_LOG
280 ESP_LOGN(TAG,
"print_queue_members_ (top 10) size %zu", this->
nextion_queue_.size());
281 ESP_LOGN(TAG,
"*******************************************");
288 ESP_LOGN(TAG,
"Queue null");
290 ESP_LOGN(TAG,
"Queue type: %d:%s, name: %s", i->component->get_queue_type(),
291 i->component->get_queue_type_string().c_str(), i->component->get_variable_name().c_str());
294 ESP_LOGN(TAG,
"*******************************************");
302 if (this->
connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) {
311#ifdef USE_NEXTION_CONF_START_UP_PAGE
339 ESP_LOGV(TAG,
"Manual ready set");
344#ifdef USE_NEXTION_COMMAND_SPACING
350#ifdef USE_NEXTION_COMMAND_SPACING
352 if (this->
nextion_queue_.empty() || !this->command_pacer_.can_send()) {
358 if (front_item && !front_item->pending_command.empty()) {
361 front_item->pending_command.clear();
362 ESP_LOGVV(TAG,
"Pending command sent: %s", front_item->component->get_variable_name().c_str());
371 ESP_LOGE(TAG,
"Queue empty");
378 ESP_LOGE(TAG,
"Invalid queue");
384 ESP_LOGN(TAG,
"Removed: %s",
component->get_variable_name().c_str());
387 if (
component->get_variable_name() ==
"sleep_wake") {
411#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
412 size_t commands_processed = 0;
415 size_t to_process_length = 0;
416 std::string to_process;
418 ESP_LOGN(TAG,
"command_data_ %s len %d", this->
command_data_.c_str(), this->command_data_.length());
419#ifdef NEXTION_PROTOCOL_LOG
422 while ((to_process_length = this->
command_data_.find(COMMAND_DELIMITER)) != std::string::npos) {
423#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
425 ESP_LOGW(TAG,
"Command processing limit exceeded");
430 while (to_process_length + COMMAND_DELIMITER.length() < this->command_data_.length() &&
431 static_cast<uint8_t
>(this->command_data_[to_process_length + COMMAND_DELIMITER.length()]) == 0xFF) {
433 ESP_LOGN(TAG,
"Add 0xFF");
438 to_process_length -= 1;
439 to_process = this->
command_data_.substr(1, to_process_length);
441 switch (nextion_event) {
443 ESP_LOGW(TAG,
"Invalid instruction");
449 ESP_LOGVV(TAG,
"Cmd OK");
450 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", YESNO(this->
nextion_queue_.empty()));
459#ifdef USE_NEXTION_COMMAND_SPACING
461 ESP_LOGN(TAG,
"Command spacing: marked command sent");
465 ESP_LOGW(TAG,
"Invalid component ID/name");
469 ESP_LOGW(TAG,
"Invalid page ID");
473 ESP_LOGW(TAG,
"Invalid picture ID");
477 ESP_LOGW(TAG,
"Invalid font ID");
481 ESP_LOGW(TAG,
"File operation failed");
484 ESP_LOGW(TAG,
"CRC validation failed");
487 ESP_LOGW(TAG,
"Invalid baud rate");
491 ESP_LOGW(TAG,
"Waveform ID/ch used but no sensor queued");
496 ESP_LOGW(TAG,
"Invalid waveform ID %d/ch %d",
component->get_component_id(),
499 ESP_LOGN(TAG,
"Remove waveform ID %d/ch %d",
component->get_component_id(),
component->get_wave_channel_id());
506 ESP_LOGW(TAG,
"Invalid variable name");
510 ESP_LOGW(TAG,
"Invalid variable operation");
514 ESP_LOGW(TAG,
"Variable assign failed");
518 ESP_LOGW(TAG,
"EEPROM operation failed");
521 ESP_LOGW(TAG,
"Invalid parameter count");
525 ESP_LOGW(TAG,
"Invalid component I/O");
528 ESP_LOGW(TAG,
"Undefined escape chars");
532 ESP_LOGW(TAG,
"Variable name too long");
537 ESP_LOGE(TAG,
"Serial buffer overflow");
541 if (to_process_length != 3) {
542 ESP_LOGW(TAG,
"Incorrect touch len: %zu (need 3)", to_process_length);
546 uint8_t page_id = to_process[0];
547 uint8_t component_id = to_process[1];
548 uint8_t touch_event = to_process[2];
549 ESP_LOGV(TAG,
"Touch %s: page %u comp %u", touch_event ?
"PRESS" :
"RELEASE", page_id, component_id);
550 for (
auto *touch : this->
touch_) {
551 touch->process_touch(page_id, component_id, touch_event != 0);
558 if (to_process_length != 1) {
559 ESP_LOGW(TAG,
"Page event: expect 1, got %zu", to_process_length);
563 uint8_t page_id = to_process[0];
564 ESP_LOGV(TAG,
"New page: %u", page_id);
573 if (to_process_length != 5) {
574 ESP_LOGW(TAG,
"Touch coordinate: expect 5, got %zu", to_process_length);
575 ESP_LOGW(TAG,
"%s", to_process.c_str());
579 const uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
580 const uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
581 const uint8_t touch_event = to_process[4];
582 ESP_LOGV(TAG,
"Touch %s at %u,%u", touch_event ?
"PRESS" :
"RELEASE",
x,
y);
593 ESP_LOGW(TAG,
"String return but queue is empty");
599 ESP_LOGE(TAG,
"Invalid queue entry");
606 ESP_LOGE(TAG,
"String return but '%s' not text sensor",
component->get_variable_name().c_str());
608 ESP_LOGN(TAG,
"String resp: '%s' id: %s type: %s", to_process.c_str(),
component->get_variable_name().c_str(),
609 component->get_queue_type_string().c_str());
625 ESP_LOGE(TAG,
"Numeric return but queue empty");
629 if (to_process_length == 0) {
630 ESP_LOGE(TAG,
"Numeric return but no data");
636 for (
int i = 0; i < 4; ++i) {
637 value += to_process[i] << (8 * i);
642 ESP_LOGE(TAG,
"Invalid queue");
651 ESP_LOGE(TAG,
"Numeric return but '%s' invalid type %d",
component->get_variable_name().c_str(),
654 ESP_LOGN(TAG,
"Numeric: %s type %d:%s val %d",
component->get_variable_name().c_str(),
656 component->set_state_from_int(value,
true,
false);
666 ESP_LOGVV(TAG,
"Auto sleep");
673 ESP_LOGVV(TAG,
"Auto wake");
681 ESP_LOGV(TAG,
"System start: %zu", to_process_length);
695 std::string variable_name;
698 auto index = to_process.find(
'\0');
699 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
700 ESP_LOGE(TAG,
"Bad switch data (0x90)");
701 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
705 variable_name = to_process.substr(0, index);
708 ESP_LOGN(TAG,
"Switch %s: %s", ONOFF(to_process[index] != 0), variable_name.c_str());
711 switchtype->process_bool(variable_name, to_process[index] != 0);
722 std::string variable_name;
724 auto index = to_process.find(
'\0');
725 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
726 ESP_LOGE(TAG,
"Bad sensor data (0x91)");
727 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
731 index = to_process.find(
'\0');
732 variable_name = to_process.substr(0, index);
735 for (
int i = 0; i < 4; ++i) {
736 value += to_process[i + index + 1] << (8 * i);
739 ESP_LOGN(TAG,
"Sensor: %s=%d", variable_name.c_str(), value);
742 sensor->process_sensor(variable_name, value);
755 std::string variable_name;
756 std::string text_value;
759 auto index = to_process.find(
'\0');
760 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
761 ESP_LOGE(TAG,
"Bad text data (0x92)");
762 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
766 variable_name = to_process.substr(0, index);
770 text_value = to_process.substr(index, to_process_length - index - 1);
772 ESP_LOGN(TAG,
"Text sensor: %s='%s'", variable_name.c_str(), text_value.c_str());
779 textsensortype->process_text(variable_name, text_value);
790 std::string variable_name;
793 auto index = to_process.find(
'\0');
794 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
795 ESP_LOGE(TAG,
"Bad binary data (0x92)");
796 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
800 variable_name = to_process.substr(0, index);
803 ESP_LOGN(TAG,
"Binary sensor: %s=%s", variable_name.c_str(), ONOFF(to_process[index] != 0));
806 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
811 ESP_LOGVV(TAG,
"Data transmit done");
816 ESP_LOGVV(TAG,
"Ready for transmit");
818 ESP_LOGE(TAG,
"No waveforms queued");
824 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size()
829 ESP_LOGN(TAG,
"Send waveform: component id %d, waveform id %d, size %zu",
component->get_component_id(),
830 component->get_wave_channel_id(), buffer_to_send);
832 component->clear_wave_buffer(buffer_to_send);
838 ESP_LOGW(TAG,
"Unknown event: 0x%02X", nextion_event);
842 this->
command_data_.erase(0, to_process_length + COMMAND_DELIMITER.length() + 1);
847 if (!this->
nextion_queue_.empty() && this->nextion_queue_.front()->queue_time + this->max_q_age_ms_ < ms) {
852 ESP_LOGD(TAG,
"Remove old queue '%s':'%s' (t=0)",
component->get_queue_type_string().c_str(),
856 if (
component->get_variable_name() ==
"sleep_wake") {
860 ESP_LOGD(TAG,
"Remove old queue '%s':'%s'",
component->get_queue_type_string().c_str(),
864 if (
component->get_variable_name() ==
"sleep_wake") {
880 ESP_LOGN(TAG,
"Loop end");
890 ESP_LOGN(TAG,
"State: %s=%lf (type %d)", name.c_str(),
state, queue_type);
892 switch (queue_type) {
895 if (name == sensor->get_variable_name()) {
896 sensor->set_state(
state,
true,
true);
904 if (name == sensor->get_variable_name()) {
905 sensor->set_state(
state != 0,
true,
true);
913 if (name == sensor->get_variable_name()) {
914 sensor->set_state(
state != 0,
true,
true);
921 ESP_LOGW(TAG,
"set_sensor_state: bad type %d", queue_type);
927 ESP_LOGV(TAG,
"State: %s='%s'", name.c_str(),
state.c_str());
930 if (name == sensor->get_variable_name()) {
931 sensor->set_state(
state,
true,
true);
938 ESP_LOGV(TAG,
"Send states");
940 if (force_update || binarysensortype->get_needs_to_send_update())
941 binarysensortype->send_state_to_nextion();
944 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_chan_id() == 0)
945 sensortype->send_state_to_nextion();
948 if (force_update || switchtype->get_needs_to_send_update())
949 switchtype->send_state_to_nextion();
952 if (force_update || textsensortype->get_needs_to_send_update())
953 textsensortype->send_state_to_nextion();
959 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
960 binarysensortype->update_component_settings(
true);
963 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
964 sensortype->update_component_settings(
true);
967 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
968 switchtype->update_component_settings(
true);
971 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
972 textsensortype->update_component_settings(
true);
978 uint8_t nr_of_ff_bytes = 0;
979 bool exit_flag =
false;
980 bool ff_flag =
false;
982 const uint32_t start =
millis();
984 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
999 if (nr_of_ff_bytes >= 3)
1002 response += (char) c;
1004 if (response.find(0x05) != std::string::npos) {
1011 if (exit_flag || ff_flag) {
1017 response = response.substr(0, response.length() - 3);
1019 return response.length();
1033#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1035 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->
nextion_queue_.size(), variable_name.c_str());
1042 if (nextion_queue ==
nullptr) {
1043 ESP_LOGW(TAG,
"Queue alloc failed");
1071#ifdef USE_NEXTION_COMMAND_SPACING
1079#ifdef USE_NEXTION_COMMAND_SPACING
1081 const std::string &command) {
1082#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1084 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->
nextion_queue_.size(), variable_name.c_str());
1091 if (nextion_queue ==
nullptr) {
1092 ESP_LOGW(TAG,
"Queue alloc failed");
1103 ESP_LOGVV(TAG,
"Queue with pending command: %s", variable_name.c_str());
1114 va_start(arg, format);
1115 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1118 ESP_LOGW(TAG,
"Bad cmd format: '%s'", format);
1139 va_start(arg, format);
1140 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1143 ESP_LOGW(TAG,
"Bad cmd format: '%s'", format);
1166 const std::string &variable_name_to_send, int32_t state_value) {
1171 const std::string &variable_name_to_send, int32_t state_value,
1172 bool is_sleep_safe) {
1193 const std::string &variable_name_to_send,
1194 const std::string &state_value) {
1199 const std::string &variable_name_to_send,
1200 const std::string &state_value,
bool is_sleep_safe) {
1205 state_value.c_str());
1221#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1223 ESP_LOGW(TAG,
"Queue full (%zu), drop GET: %s", this->
nextion_queue_.size(),
1231 if (nextion_queue ==
nullptr) {
1232 ESP_LOGW(TAG,
"Queue alloc failed");
1240 ESP_LOGN(TAG,
"Queue %s: %s",
component->get_queue_type_string().c_str(),
component->get_variable_name().c_str());
1242 std::string command =
"get " +
component->get_variable_name_to_send();
1263 if (nextion_queue ==
nullptr) {
1264 ESP_LOGW(TAG,
"Queue alloc failed");
1283 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size()
1286 std::string command =
"addt " + to_string(
component->get_component_id()) +
"," +
1287 to_string(
component->get_wave_channel_id()) +
"," + to_string(buffer_to_send);
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.
uint8_t get_spacing() const
Get current command spacing.
void mark_sent()
Mark a command as sent, updating the timing.
std::string get_variable_name()
void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send="")
void add_setup_state_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion completes its initialize setup.
std::vector< NextionComponentBase * > touch_
std::vector< NextionComponentBase * > switchtype_
void add_buffer_overflow_event_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion reports a buffer overflow.
std::vector< NextionComponentBase * > binarysensortype_
const uint16_t max_q_age_ms_
const uint16_t startup_override_ms_
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
std::vector< NextionComponentBase * > textsensortype_
CallbackManager< void(uint8_t)> page_callback_
void check_pending_waveform_()
struct esphome::nextion::Nextion::@144 connection_state_
Status flags for Nextion display state management.
void set_wake_up_page(uint8_t wake_up_page=255)
Sets which page Nextion loads when exiting sleep mode.
std::string device_model_
void all_components_send_state_(bool force_update=false)
std::string firmware_version_
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
void add_new_page_callback(std::function< void(uint8_t)> &&callback)
Add a callback to be notified when the nextion changes pages.
uint16_t max_commands_per_loop_
void process_nextion_commands_()
void add_to_get_queue(NextionComponentBase *component) override
Queue a GET command for a component that expects a response from the Nextion display.
bool send_command_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
std::deque< NextionQueue * > nextion_queue_
void set_auto_wake_on_touch(bool auto_wake_on_touch)
Sets if Nextion should auto-wake from sleep when touch press occurs.
bool remove_from_q_(bool report_empty=true)
std::vector< NextionComponentBase * > sensortype_
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe=false)
uint16_t touch_sleep_timeout_
bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
Sends a formatted command to the nextion.
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout=0)
Set the touch sleep timeout of the display using the thsp command.
CallbackManager< void()> setup_callback_
std::string command_data_
optional< float > brightness_
CallbackManager< void()> wake_callback_
void add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
bool is_updating() override
Check if the TFT update process is currently running.
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
void goto_page(const char *page)
Show the page with a given name.
bool send_command(const char *command)
Manually send a raw command to the display.
std::string serial_number_
CallbackManager< void()> sleep_callback_
float get_setup_priority() const override
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
void reset_(bool reset_nextion=true)
void update_all_components()
void process_pending_in_queue_()
Process any commands in the queue that are pending due to command spacing.
void add_no_result_to_queue_(const std::string &variable_name)
Add a command to the Nextion queue that expects no response.
CallbackManager< void(uint8_t, uint8_t, bool)> touch_callback_
void print_queue_members_()
void dump_config() override
NextionCommandPacer command_pacer_
void set_nextion_text_state(const std::string &name, const std::string &state)
std::deque< NextionQueue * > waveform_queue_
bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
uint8_t is_connected_
Connection established with Nextion display.
void add_wake_state_callback(std::function< void()> &&callback)
Add a callback to be notified of wake state changes.
CallbackManager< void()> buffer_overflow_callback_
void set_writer(const nextion_writer_t &writer)
void add_no_result_to_queue_with_pending_command_(const std::string &variable_name, const std::string &command)
Add a command to the Nextion queue with a pending command for retry.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) override
void add_touch_event_callback(std::function< void(uint8_t, uint8_t, bool)> &&callback)
Add a callback to be notified when Nextion has a touch event.
void update_components_by_prefix(const std::string &prefix)
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
NextionComponentBase * component
std::string pending_command
value_type const & value() const
void write_str(const char *str)
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
const Component * component
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.