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_LOGD(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
337 ESP_LOGD(TAG,
"Manual ready set");
342#ifdef USE_NEXTION_COMMAND_SPACING
348#ifdef USE_NEXTION_COMMAND_SPACING
350 if (this->
nextion_queue_.empty() || !this->command_pacer_.can_send()) {
356 if (front_item && !front_item->pending_command.empty()) {
359 front_item->pending_command.clear();
360 ESP_LOGVV(TAG,
"Pending command sent: %s", front_item->component->get_variable_name().c_str());
369 ESP_LOGE(TAG,
"Queue empty");
376 ESP_LOGE(TAG,
"Invalid queue");
409#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
410 size_t commands_processed = 0;
413 size_t to_process_length = 0;
414 std::string to_process;
416 ESP_LOGN(TAG,
"command_data_ %s len %d", this->
command_data_.c_str(), this->command_data_.length());
417#ifdef NEXTION_PROTOCOL_LOG
420 while ((to_process_length = this->
command_data_.find(COMMAND_DELIMITER)) != std::string::npos) {
421#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
423 ESP_LOGW(TAG,
"Command processing limit exceeded");
428 while (to_process_length + COMMAND_DELIMITER.length() < this->command_data_.length() &&
429 static_cast<uint8_t
>(this->command_data_[to_process_length + COMMAND_DELIMITER.length()]) == 0xFF) {
431 ESP_LOGN(TAG,
"Add 0xFF");
436 to_process_length -= 1;
437 to_process = this->
command_data_.substr(1, to_process_length);
439 switch (nextion_event) {
441 ESP_LOGW(TAG,
"Invalid instruction");
447 ESP_LOGVV(TAG,
"Cmd OK");
448 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", YESNO(this->
nextion_queue_.empty()));
457#ifdef USE_NEXTION_COMMAND_SPACING
459 ESP_LOGN(TAG,
"Command spacing: marked command sent");
463 ESP_LOGW(TAG,
"Invalid component ID/name");
467 ESP_LOGW(TAG,
"Invalid page ID");
471 ESP_LOGW(TAG,
"Invalid picture ID");
475 ESP_LOGW(TAG,
"Invalid font ID");
479 ESP_LOGW(TAG,
"File operation failed");
482 ESP_LOGW(TAG,
"CRC validation failed");
485 ESP_LOGW(TAG,
"Invalid baud rate");
489 ESP_LOGW(TAG,
"Waveform ID/ch used but no sensor queued");
494 ESP_LOGW(TAG,
"Invalid waveform ID %d/ch %d", component->
get_component_id(),
504 ESP_LOGW(TAG,
"Invalid variable name");
508 ESP_LOGW(TAG,
"Invalid variable operation");
512 ESP_LOGW(TAG,
"Variable assign failed");
516 ESP_LOGW(TAG,
"EEPROM operation failed");
519 ESP_LOGW(TAG,
"Invalid parameter count");
523 ESP_LOGW(TAG,
"Invalid component I/O");
526 ESP_LOGW(TAG,
"Undefined escape chars");
530 ESP_LOGW(TAG,
"Variable name too long");
535 ESP_LOGE(TAG,
"Serial buffer overflow");
539 if (to_process_length != 3) {
540 ESP_LOGW(TAG,
"Incorrect touch len: %zu (need 3)", to_process_length);
544 uint8_t page_id = to_process[0];
545 uint8_t component_id = to_process[1];
546 uint8_t touch_event = to_process[2];
547 ESP_LOGD(TAG,
"Touch %s: page %u comp %u", touch_event ?
"PRESS" :
"RELEASE", page_id, component_id);
548 for (
auto *touch : this->
touch_) {
549 touch->process_touch(page_id, component_id, touch_event != 0);
556 if (to_process_length != 1) {
557 ESP_LOGW(TAG,
"Page event: expect 1, got %zu", to_process_length);
561 uint8_t page_id = to_process[0];
562 ESP_LOGD(TAG,
"New page: %u", page_id);
571 if (to_process_length != 5) {
572 ESP_LOGW(TAG,
"Touch coordinate: expect 5, got %zu", to_process_length);
573 ESP_LOGW(TAG,
"%s", to_process.c_str());
577 const uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
578 const uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
579 const uint8_t touch_event = to_process[4];
580 ESP_LOGD(TAG,
"Touch %s at %u,%u", touch_event ?
"PRESS" :
"RELEASE",
x,
y);
591 ESP_LOGW(TAG,
"String return but queue is empty");
597 ESP_LOGE(TAG,
"Invalid queue entry");
604 ESP_LOGE(TAG,
"String return but '%s' not text sensor", component->
get_variable_name().c_str());
606 ESP_LOGN(TAG,
"String resp: '%s' id: %s type: %s", to_process.c_str(), component->
get_variable_name().c_str(),
623 ESP_LOGE(TAG,
"Numeric return but queue empty");
627 if (to_process_length == 0) {
628 ESP_LOGE(TAG,
"Numeric return but no data");
634 for (
int i = 0; i < 4; ++i) {
635 value += to_process[i] << (8 * i);
640 ESP_LOGE(TAG,
"Invalid queue");
649 ESP_LOGE(TAG,
"Numeric return but '%s' invalid type %d", component->
get_variable_name().c_str(),
652 ESP_LOGN(TAG,
"Numeric: %s type %d:%s val %d", component->
get_variable_name().c_str(),
664 ESP_LOGVV(TAG,
"Auto sleep");
671 ESP_LOGVV(TAG,
"Auto wake");
679 ESP_LOGD(TAG,
"System start: %zu", to_process_length);
693 std::string variable_name;
696 auto index = to_process.find(
'\0');
697 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
698 ESP_LOGE(TAG,
"Bad switch data (0x90)");
699 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
703 variable_name = to_process.substr(0, index);
706 ESP_LOGN(TAG,
"Switch %s: %s", ONOFF(to_process[index] != 0), variable_name.c_str());
709 switchtype->process_bool(variable_name, to_process[index] != 0);
720 std::string variable_name;
722 auto index = to_process.find(
'\0');
723 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
724 ESP_LOGE(TAG,
"Bad sensor data (0x91)");
725 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
729 index = to_process.find(
'\0');
730 variable_name = to_process.substr(0, index);
733 for (
int i = 0; i < 4; ++i) {
734 value += to_process[i + index + 1] << (8 * i);
737 ESP_LOGN(TAG,
"Sensor: %s=%d", variable_name.c_str(), value);
740 sensor->process_sensor(variable_name, value);
753 std::string variable_name;
754 std::string text_value;
757 auto index = to_process.find(
'\0');
758 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
759 ESP_LOGE(TAG,
"Bad text data (0x92)");
760 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
764 variable_name = to_process.substr(0, index);
767 text_value = to_process.substr(index);
769 ESP_LOGN(TAG,
"Text sensor: %s='%s'", variable_name.c_str(), text_value.c_str());
776 textsensortype->process_text(variable_name, text_value);
787 std::string variable_name;
790 auto index = to_process.find(
'\0');
791 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
792 ESP_LOGE(TAG,
"Bad binary data (0x92)");
793 ESP_LOGN(TAG,
"proc: %s %zu %d", to_process.c_str(), to_process_length, index);
797 variable_name = to_process.substr(0, index);
800 ESP_LOGN(TAG,
"Binary sensor: %s=%s", variable_name.c_str(), ONOFF(to_process[index] != 0));
803 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
808 ESP_LOGVV(TAG,
"Data transmit done");
813 ESP_LOGVV(TAG,
"Ready for transmit");
815 ESP_LOGE(TAG,
"No waveforms queued");
820 auto *component = nb->component;
821 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size()
824 this->
write_array(component->get_wave_buffer().data(),
static_cast<int>(buffer_to_send));
826 ESP_LOGN(TAG,
"Send waveform: component id %d, waveform id %d, size %zu", component->get_component_id(),
827 component->get_wave_channel_id(), buffer_to_send);
829 component->clear_wave_buffer(buffer_to_send);
835 ESP_LOGW(TAG,
"Unknown event: 0x%02X", nextion_event);
839 this->
command_data_.erase(0, to_process_length + COMMAND_DELIMITER.length() + 1);
844 if (!this->
nextion_queue_.empty() && this->nextion_queue_.front()->queue_time + this->max_q_age_ms_ < ms) {
877 ESP_LOGN(TAG,
"Loop end");
887 ESP_LOGN(TAG,
"State: %s=%lf (type %d)", name.c_str(),
state, queue_type);
889 switch (queue_type) {
892 if (name == sensor->get_variable_name()) {
893 sensor->set_state(
state,
true,
true);
901 if (name == sensor->get_variable_name()) {
902 sensor->set_state(
state != 0,
true,
true);
910 if (name == sensor->get_variable_name()) {
911 sensor->set_state(
state != 0,
true,
true);
918 ESP_LOGW(TAG,
"set_sensor_state: bad type %d", queue_type);
924 ESP_LOGD(TAG,
"State: %s='%s'", name.c_str(),
state.c_str());
927 if (name == sensor->get_variable_name()) {
928 sensor->set_state(
state,
true,
true);
935 ESP_LOGD(TAG,
"Send states");
937 if (force_update || binarysensortype->get_needs_to_send_update())
938 binarysensortype->send_state_to_nextion();
941 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_chan_id() == 0)
942 sensortype->send_state_to_nextion();
945 if (force_update || switchtype->get_needs_to_send_update())
946 switchtype->send_state_to_nextion();
949 if (force_update || textsensortype->get_needs_to_send_update())
950 textsensortype->send_state_to_nextion();
956 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
957 binarysensortype->update_component_settings(
true);
960 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
961 sensortype->update_component_settings(
true);
964 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
965 switchtype->update_component_settings(
true);
968 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
969 textsensortype->update_component_settings(
true);
975 uint8_t nr_of_ff_bytes = 0;
976 bool exit_flag =
false;
977 bool ff_flag =
false;
979 const uint32_t start =
millis();
981 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
996 if (nr_of_ff_bytes >= 3)
999 response += (char) c;
1001 if (response.find(0x05) != std::string::npos) {
1008 if (exit_flag || ff_flag) {
1014 response = response.substr(0, response.length() - 3);
1016 return response.length();
1030#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1032 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->
nextion_queue_.size(), variable_name.c_str());
1039 if (nextion_queue ==
nullptr) {
1040 ESP_LOGW(TAG,
"Queue alloc failed");
1068#ifdef USE_NEXTION_COMMAND_SPACING
1076#ifdef USE_NEXTION_COMMAND_SPACING
1078 const std::string &command) {
1079#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1081 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->
nextion_queue_.size(), variable_name.c_str());
1088 if (nextion_queue ==
nullptr) {
1089 ESP_LOGW(TAG,
"Queue alloc failed");
1100 ESP_LOGVV(TAG,
"Queue with pending command: %s", variable_name.c_str());
1111 va_start(arg, format);
1112 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1115 ESP_LOGW(TAG,
"Bad cmd format: '%s'", format);
1136 va_start(arg, format);
1137 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
1140 ESP_LOGW(TAG,
"Bad cmd format: '%s'", format);
1163 const std::string &variable_name_to_send, int32_t state_value) {
1168 const std::string &variable_name_to_send, int32_t state_value,
1169 bool is_sleep_safe) {
1190 const std::string &variable_name_to_send,
1191 const std::string &state_value) {
1196 const std::string &variable_name_to_send,
1197 const std::string &state_value,
bool is_sleep_safe) {
1202 state_value.c_str());
1218#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1220 ESP_LOGW(TAG,
"Queue full (%zu), drop GET: %s", this->
nextion_queue_.size(),
1228 if (nextion_queue ==
nullptr) {
1229 ESP_LOGW(TAG,
"Queue alloc failed");
1260 if (nextion_queue ==
nullptr) {
1261 ESP_LOGW(TAG,
"Queue alloc failed");
1279 auto *component = nb->component;
1280 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size()
1283 std::string command =
"addt " + to_string(component->get_component_id()) +
"," +
1284 to_string(component->get_wave_channel_id()) +
"," + to_string(buffer_to_send);
1294void
Nextion::set_wait_for_ack(
bool wait_for_ack) { ESP_LOGE(TAG,
"Deprecated"); }
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.
uint8_t get_wave_channel_id()
virtual std::string get_queue_type_string()
virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion)
std::string get_variable_name()
uint8_t get_component_id()
virtual NextionQueueType get_queue_type()
std::string get_variable_name_to_send()
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_()
void set_wake_up_page(uint8_t wake_up_page=255)
Sets which page Nextion loads when exiting sleep mode.
optional< nextion_writer_t > writer_
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_
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.
struct esphome::nextion::Nextion::@141 connection_state_
Status flags for Nextion display state management.
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)
std::function< void(Nextion &)> nextion_writer_t
ESPDEPRECATED("set_wait_for_ack(bool) deprecated, no effect", "v1.20") void Nextion
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.