10 static const char *
const TAG =
"nextion";
34 ESP_LOGN(TAG,
"send_command %s", command.c_str());
37 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
49 ESP_LOGW(TAG,
"Nextion display set as connected without performing handshake");
78 if (!response.empty() && response[0] == 0x1A) {
80 ESP_LOGD(TAG,
"0x1A error ignored during setup");
83 if (response.empty() || response.find(
"comok") == std::string::npos) {
84 #ifdef NEXTION_PROTOCOL_LOG 85 ESP_LOGN(TAG,
"Bad connect request %s", response.c_str());
86 for (
size_t i = 0; i < response.length(); i++) {
87 ESP_LOGN(TAG,
"response %s %d %d %c", response.c_str(), i, response[i], response[i]);
91 ESP_LOGW(TAG,
"Nextion is not connected! ");
97 ESP_LOGI(TAG,
"Nextion is connected");
100 ESP_LOGN(TAG,
"connect request %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,
"Received connect_info %zu", connect_info.size());
119 ESP_LOGE(TAG,
"Nextion returned bad connect value \"%s\"", response.c_str());
138 ESP_LOGCONFIG(TAG,
"Nextion:");
142 ESP_LOGCONFIG(TAG,
" Device Model: %s", this->
device_model_.c_str());
144 ESP_LOGCONFIG(TAG,
" Serial Number: %s", this->
serial_number_.c_str());
145 ESP_LOGCONFIG(TAG,
" Flash Size: %s", this->
flash_size_.c_str());
155 ESP_LOGCONFIG(TAG,
" Wake Up Page: %" PRId16, this->
wake_up_page_);
159 ESP_LOGCONFIG(TAG,
" Start Up Page: %" PRId16, this->
start_up_page_);
198 binarysensortype->update_component();
201 sensortype->update_component();
204 switchtype->update_component();
207 textsensortype->update_component();
228 va_start(arg, format);
229 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
232 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
243 #ifdef NEXTION_PROTOCOL_LOG 245 ESP_LOGN(TAG,
"print_queue_members_ (top 10) size %zu", this->
nextion_queue_.size());
246 ESP_LOGN(TAG,
"*******************************************");
253 ESP_LOGN(TAG,
"Nextion queue is null");
255 ESP_LOGN(TAG,
"Nextion queue type: %d:%s , name: %s", i->component->get_queue_type(),
256 i->component->get_queue_type_string().c_str(), i->component->get_variable_name().c_str());
259 ESP_LOGN(TAG,
"*******************************************");
302 ESP_LOGD(TAG,
"Manually set nextion report ready");
311 ESP_LOGE(TAG,
"Nextion queue is empty!");
319 ESP_LOGN(TAG,
"Removing %s from the queue", component->
get_variable_name().c_str());
346 size_t to_process_length = 0;
347 std::string to_process;
350 #ifdef NEXTION_PROTOCOL_LOG 353 while ((to_process_length = this->
command_data_.find(COMMAND_DELIMITER)) != std::string::npos) {
354 ESP_LOGN(TAG,
"print_queue_members_ size %zu", this->
nextion_queue_.size());
355 while (to_process_length + COMMAND_DELIMITER.length() < this->
command_data_.length() &&
356 static_cast<uint8_t
>(this->
command_data_[to_process_length + COMMAND_DELIMITER.length()]) == 0xFF) {
358 ESP_LOGN(TAG,
"Add extra 0xFF to process");
363 to_process_length -= 1;
364 to_process = this->
command_data_.substr(1, to_process_length);
368 ESP_LOGW(TAG,
"Nextion reported invalid instruction!");
374 ESP_LOGVV(TAG,
"instruction sent by user was successful");
375 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", this->
nextion_queue_.empty() ?
"True" :
"False");
380 ESP_LOGD(TAG,
"Nextion is setup");
388 ESP_LOGW(TAG,
"Nextion reported component ID or name invalid!");
392 ESP_LOGW(TAG,
"Nextion reported page ID invalid!");
396 ESP_LOGW(TAG,
"Nextion reported picture ID invalid!");
400 ESP_LOGW(TAG,
"Nextion reported font ID invalid!");
404 ESP_LOGW(TAG,
"Nextion File operation fail!");
407 ESP_LOGW(TAG,
"Nextion Instructions with CRC validation fails their CRC check!");
410 ESP_LOGW(TAG,
"Nextion reported baud rate invalid!");
415 "Nextion reported invalid Waveform ID or Channel # was used but no waveform sensor in queue found!");
420 ESP_LOGW(TAG,
"Nextion reported invalid Waveform ID %d or Channel # %d was used!",
423 ESP_LOGN(TAG,
"Removing waveform from queue with component id %d and waveform id %d",
431 ESP_LOGW(TAG,
"Nextion reported variable name invalid!");
435 ESP_LOGW(TAG,
"Nextion reported variable operation invalid!");
439 ESP_LOGW(TAG,
"Nextion reported failed to assign variable!");
443 ESP_LOGW(TAG,
"Nextion reported operating EEPROM failed!");
446 ESP_LOGW(TAG,
"Nextion reported parameter quantity invalid!");
450 ESP_LOGW(TAG,
"Nextion reported component I/O operation invalid!");
453 ESP_LOGW(TAG,
"Nextion reported undefined escape characters!");
457 ESP_LOGW(TAG,
"Nextion reported too long variable name!");
461 ESP_LOGW(TAG,
"Nextion reported Serial Buffer overflow!");
464 if (to_process_length != 3) {
465 ESP_LOGW(TAG,
"Touch event data is expecting 3, received %zu", to_process_length);
469 uint8_t page_id = to_process[0];
470 uint8_t component_id = to_process[1];
471 uint8_t touch_event = to_process[2];
472 ESP_LOGD(TAG,
"Got touch event:");
473 ESP_LOGD(TAG,
" page_id: %u", page_id);
474 ESP_LOGD(TAG,
" component_id: %u", component_id);
475 ESP_LOGD(TAG,
" event type: %s", touch_event ?
"PRESS" :
"RELEASE");
476 for (
auto *touch : this->
touch_) {
477 touch->process_touch(page_id, component_id, touch_event != 0);
484 if (to_process_length != 1) {
485 ESP_LOGW(TAG,
"New page event data is expecting 1, received %zu", to_process_length);
489 uint8_t page_id = to_process[0];
490 ESP_LOGD(TAG,
"Got new page: %u", page_id);
499 if (to_process_length != 5) {
500 ESP_LOGW(TAG,
"Touch coordinate data is expecting 5, received %zu", to_process_length);
501 ESP_LOGW(TAG,
"%s", to_process.c_str());
505 uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
506 uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
507 uint8_t touch_event = to_process[4];
508 ESP_LOGD(TAG,
"Got touch event:");
509 ESP_LOGD(TAG,
" x: %u", x);
510 ESP_LOGD(TAG,
" y: %u", y);
511 ESP_LOGD(TAG,
" type: %s", touch_event ?
"PRESS" :
"RELEASE");
522 ESP_LOGW(TAG,
"ERROR: Received string return but the queue is empty");
530 ESP_LOGE(TAG,
"ERROR: Received string return but next in queue \"%s\" is not a text sensor",
533 ESP_LOGN(TAG,
"Received get_string response: \"%s\" for component id: %s, type: %s", to_process.c_str(),
551 ESP_LOGE(TAG,
"ERROR: Received numeric return but the queue is empty");
555 if (to_process_length == 0) {
556 ESP_LOGE(TAG,
"ERROR: Received numeric return but no data!");
564 for (
int i = 0; i < 4; ++i) {
565 value += to_process[i] << (8 * i);
575 ESP_LOGE(TAG,
"ERROR: Received numeric return but next in queue \"%s\" is not a valid sensor type %d",
578 ESP_LOGN(TAG,
"Received numeric return for variable %s, queue type %d:%s, value %d",
591 ESP_LOGVV(TAG,
"Received Nextion entering sleep automatically");
598 ESP_LOGVV(TAG,
"Received Nextion leaves sleep automatically");
606 ESP_LOGD(TAG,
"system successful start up %zu", to_process_length);
620 std::string variable_name;
623 auto index = to_process.find(
'\0');
624 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
625 ESP_LOGE(TAG,
"Bad switch component data received for 0x90 event!");
626 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
630 variable_name = to_process.substr(0, index);
633 ESP_LOGN(TAG,
"Got Switch:");
634 ESP_LOGN(TAG,
" variable_name: %s", variable_name.c_str());
635 ESP_LOGN(TAG,
" value: %d", to_process[0] != 0);
638 switchtype->process_bool(variable_name, to_process[index] != 0);
649 std::string variable_name;
651 auto index = to_process.find(
'\0');
652 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
653 ESP_LOGE(TAG,
"Bad sensor component data received for 0x91 event!");
654 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
658 index = to_process.find(
'\0');
659 variable_name = to_process.substr(0, index);
662 for (
int i = 0; i < 4; ++i) {
663 value += to_process[i + index + 1] << (8 * i);
666 ESP_LOGN(TAG,
"Got sensor:");
667 ESP_LOGN(TAG,
" variable_name: %s", variable_name.c_str());
668 ESP_LOGN(TAG,
" value: %d", value);
671 sensor->process_sensor(variable_name, value);
684 std::string variable_name;
685 std::string text_value;
688 auto index = to_process.find(
'\0');
689 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
690 ESP_LOGE(TAG,
"Bad text sensor component data received for 0x92 event!");
691 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
695 variable_name = to_process.substr(0, index);
698 text_value = to_process.substr(index);
700 ESP_LOGN(TAG,
"Got Text Sensor:");
701 ESP_LOGN(TAG,
" variable_name: %s", variable_name.c_str());
702 ESP_LOGN(TAG,
" value: %s", text_value.c_str());
709 textsensortype->process_text(variable_name, text_value);
720 std::string variable_name;
723 auto index = to_process.find(
'\0');
724 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
725 ESP_LOGE(TAG,
"Bad binary sensor component data received for 0x92 event!");
726 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
730 variable_name = to_process.substr(0, index);
733 ESP_LOGN(TAG,
"Got Binary Sensor:");
734 ESP_LOGN(TAG,
" variable_name: %s", variable_name.c_str());
735 ESP_LOGN(TAG,
" value: %d", to_process[index] != 0);
738 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
743 ESP_LOGVV(TAG,
"Nextion reported data transmit finished!");
748 ESP_LOGVV(TAG,
"Nextion reported ready for transmit!");
750 ESP_LOGE(TAG,
"No waveforms in queue to send data!");
755 auto *component = nb->component;
756 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size()
759 this->
write_array(component->get_wave_buffer().data(),
static_cast<int>(buffer_to_send));
761 ESP_LOGN(TAG,
"Nextion sending waveform data for component id %d and waveform id %d, size %zu",
762 component->get_component_id(), component->get_wave_channel_id(), buffer_to_send);
764 component->clear_wave_buffer(buffer_to_send);
770 ESP_LOGW(TAG,
"Received unknown event from nextion: 0x%02X", this->
nextion_event_);
775 this->
command_data_.erase(0, to_process_length + COMMAND_DELIMITER.length() + 1);
787 ESP_LOGD(TAG,
"Removing old queue type \"%s\" name \"%s\" queue_time 0",
795 ESP_LOGD(TAG,
"Removing old queue type \"%s\" name \"%s\"", component->
get_queue_type_string().c_str(),
815 ESP_LOGN(TAG,
"Loop End");
825 ESP_LOGN(TAG,
"Received state:");
826 ESP_LOGN(TAG,
" variable: %s", name.c_str());
827 ESP_LOGN(TAG,
" state: %lf", state);
828 ESP_LOGN(TAG,
" queue type: %d", queue_type);
830 switch (queue_type) {
833 if (name ==
sensor->get_variable_name()) {
834 sensor->set_state(state,
true,
true);
842 if (name ==
sensor->get_variable_name()) {
843 sensor->set_state(state != 0,
true,
true);
851 if (name ==
sensor->get_variable_name()) {
852 sensor->set_state(state != 0,
true,
true);
859 ESP_LOGW(TAG,
"set_nextion_sensor_state does not support a queue type %d", queue_type);
865 ESP_LOGD(TAG,
"Received state:");
866 ESP_LOGD(TAG,
" variable: %s", name.c_str());
867 ESP_LOGD(TAG,
" state: %s", state.c_str());
870 if (name ==
sensor->get_variable_name()) {
871 sensor->set_state(state,
true,
true);
878 ESP_LOGD(TAG,
"all_components_send_state_ ");
880 if (force_update || binarysensortype->get_needs_to_send_update())
881 binarysensortype->send_state_to_nextion();
884 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_chan_id() == 0)
885 sensortype->send_state_to_nextion();
888 if (force_update || switchtype->get_needs_to_send_update())
889 switchtype->send_state_to_nextion();
892 if (force_update || textsensortype->get_needs_to_send_update())
893 textsensortype->send_state_to_nextion();
899 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
900 binarysensortype->update_component_settings(
true);
903 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
904 sensortype->update_component_settings(
true);
907 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
908 switchtype->update_component_settings(
true);
911 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
912 textsensortype->update_component_settings(
true);
919 uint8_t nr_of_ff_bytes = 0;
921 bool exit_flag =
false;
922 bool ff_flag =
false;
926 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
941 if (nr_of_ff_bytes >= 3)
944 response += (char) c;
946 if (response.find(0x05) != std::string::npos) {
953 if (exit_flag || ff_flag) {
959 response = response.substr(0, response.length() - 3);
961 ret = response.length();
1007 va_start(arg, format);
1008 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1011 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
1032 va_start(arg, format);
1033 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1036 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
1059 const std::string &variable_name_to_send, int32_t state_value) {
1064 const std::string &variable_name_to_send, int32_t state_value,
1065 bool is_sleep_safe) {
1086 const std::string &variable_name_to_send,
1087 const std::string &state_value) {
1092 const std::string &variable_name_to_send,
1093 const std::string &state_value,
bool is_sleep_safe) {
1098 state_value.c_str());
1149 auto *component = nb->component;
1150 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size()
1153 std::string command =
"addt " +
to_string(component->get_component_id()) +
"," +
1163 ESPDEPRECATED(
"set_wait_for_ack(bool) is deprecated and has no effect",
"v1.20")
void goto_page(const char *page)
Show the page with a given name.
bool ignore_is_setup_
Sends commands ignoring of the Nextion has been setup.
const uint16_t startup_override_ms_
void write_str(const char *str)
void all_components_send_state_(bool force_update=false)
CallbackManager< void(uint8_t)> page_callback_
CallbackManager< void()> sleep_callback_
const float DATA
For components that import data from directly connected sensors like DHT.
void add_new_page_callback(std::function< void(uint8_t)> &&callback)
Add a callback to be notified when the nextion changes pages.
void write_array(const uint8_t *data, size_t len)
void add_wake_state_callback(std::function< void()> &&callback)
Add a callback to be notified of wake state changes.
uint8_t get_wave_channel_id()
bool send_command(const char *command)
Manually send a raw command to the display.
bool send_command_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
bool is_updating() override
Check if the TFT update process is currently running.
bool exit_reparse_on_start_
void add_to_get_queue(NextionComponentBase *component) override
std::vector< NextionComponentBase * > touch_
optional< nextion_writer_t > writer_
void update_all_components()
void add_setup_state_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion completes its initialize setup.
bool sent_setup_commands_
const uint16_t max_q_age_ms_
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
float get_setup_priority() const override
std::string serial_number_
CallbackManager< void(uint8_t, uint8_t, bool)> touch_callback_
uint32_t IRAM_ATTR HOT millis()
virtual std::string get_queue_type_string()
void set_exit_reparse_on_start(bool exit_reparse)
Sets if Nextion should exit the active reparse mode before the "connect" command is sent...
bool nextion_reports_is_setup_
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 add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
std::string get_variable_name()
virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion)
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
CallbackManager< void()> setup_callback_
bool read_byte(uint8_t *data)
void set_nextion_text_state(const std::string &name, const std::string &state)
void set_wait_for_ack(bool wait_for_ack)
std::deque< NextionQueue * > nextion_queue_
CallbackManager< void()> wake_callback_
void print_queue_members_()
std::string get_variable_name_to_send()
Application App
Global storage of Application pointer - only one Application can exist.
void add_no_result_to_queue_(const std::string &variable_name)
std::deque< NextionQueue * > waveform_queue_
std::string command_data_
bool remove_from_q_(bool report_empty=true)
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
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)
bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
ESPDEPRECATED("set_wait_for_ack(bool) is deprecated and has no effect", "v1.20") void Nextion
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 set_wake_up_page(uint8_t page_id=255)
Sets which page Nextion loads when exiting sleep mode.
uint8_t get_component_id()
std::vector< NextionComponentBase * > sensortype_
void dump_config() override
std::string to_string(int value)
void process_nextion_commands_()
std::vector< NextionComponentBase * > switchtype_
virtual NextionQueueType get_queue_type()
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion)
NextionComponentBase * component
Implementation of SPI Controller mode.
bool skip_connection_handshake_
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_components_by_prefix(const std::string &prefix)
std::string firmware_version_
void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send="")
uint32_t touch_sleep_timeout_
esphome::sensor::Sensor * sensor
void check_pending_waveform_()
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) override
std::function< void(Nextion &)> nextion_writer_t
std::vector< NextionComponentBase * > binarysensortype_
void set_writer(const nextion_writer_t &writer)
void IRAM_ATTR HOT delay(uint32_t ms)