9 static const char *
const TAG =
"nextion";
33 ESP_LOGN(TAG,
"send_command %s", command.c_str());
36 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
64 if (response.empty() || response.find(
"comok") == std::string::npos) {
65 #ifdef NEXTION_PROTOCOL_LOG 66 ESP_LOGN(TAG,
"Bad connect request %s", response.c_str());
67 for (
size_t i = 0; i < response.length(); i++) {
68 ESP_LOGN(TAG,
"response %s %d %d %c", response.c_str(), i, response[i], response[i]);
72 ESP_LOGW(TAG,
"Nextion is not connected! ");
78 ESP_LOGI(TAG,
"Nextion is connected");
81 ESP_LOGN(TAG,
"connect request %s", response.c_str());
85 std::vector<std::string> connect_info;
86 while ((start = response.find_first_not_of(
',', end)) != std::string::npos) {
87 end = response.find(
',', start);
88 connect_info.push_back(response.substr(start, end - start));
91 if (connect_info.size() == 7) {
92 ESP_LOGN(TAG,
"Received connect_info %zu", connect_info.size());
99 ESP_LOGE(TAG,
"Nextion returned bad connect value \"%s\"", response.c_str());
117 ESP_LOGCONFIG(TAG,
"Nextion:");
118 ESP_LOGCONFIG(TAG,
" Device Model: %s", this->
device_model_.c_str());
120 ESP_LOGCONFIG(TAG,
" Serial Number: %s", this->
serial_number_.c_str());
121 ESP_LOGCONFIG(TAG,
" Flash Size: %s", this->
flash_size_.c_str());
129 ESP_LOGCONFIG(TAG,
" Wake Up Page : %d", this->
wake_up_page_);
164 binarysensortype->update_component();
167 sensortype->update_component();
170 switchtype->update_component();
173 textsensortype->update_component();
183 va_start(arg, format);
184 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
187 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
198 #ifdef NEXTION_PROTOCOL_LOG 200 ESP_LOGN(TAG,
"print_queue_members_ (top 10) size %zu", this->
nextion_queue_.size());
201 ESP_LOGN(TAG,
"*******************************************");
208 ESP_LOGN(TAG,
"Nextion queue is null");
210 ESP_LOGN(TAG,
"Nextion queue type: %d:%s , name: %s", i->component->get_queue_type(),
211 i->component->get_queue_type_string().c_str(), i->component->get_variable_name().c_str());
214 ESP_LOGN(TAG,
"*******************************************");
251 ESP_LOGD(TAG,
"Manually set nextion report ready");
260 ESP_LOGE(TAG,
"Nextion queue is empty!");
268 ESP_LOGN(TAG,
"Removing %s from the queue", component->
get_variable_name().c_str());
295 size_t to_process_length = 0;
296 std::string to_process;
299 #ifdef NEXTION_PROTOCOL_LOG 302 while ((to_process_length = this->
command_data_.find(COMMAND_DELIMITER)) != std::string::npos) {
303 ESP_LOGN(TAG,
"print_queue_members_ size %zu", this->
nextion_queue_.size());
304 while (to_process_length + COMMAND_DELIMITER.length() < this->
command_data_.length() &&
305 static_cast<uint8_t
>(this->
command_data_[to_process_length + COMMAND_DELIMITER.length()]) == 0xFF) {
307 ESP_LOGN(TAG,
"Add extra 0xFF to process");
312 to_process_length -= 1;
313 to_process = this->
command_data_.substr(1, to_process_length);
317 ESP_LOGW(TAG,
"Nextion reported invalid instruction!");
323 ESP_LOGVV(TAG,
"instruction sent by user was successful");
324 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", this->
nextion_queue_.empty() ?
"True" :
"False");
329 ESP_LOGD(TAG,
"Nextion is setup");
337 ESP_LOGW(TAG,
"Nextion reported component ID or name invalid!");
341 ESP_LOGW(TAG,
"Nextion reported page ID invalid!");
345 ESP_LOGW(TAG,
"Nextion reported picture ID invalid!");
349 ESP_LOGW(TAG,
"Nextion reported font ID invalid!");
353 ESP_LOGW(TAG,
"Nextion File operation fail!");
356 ESP_LOGW(TAG,
"Nextion Instructions with CRC validation fails their CRC check!");
359 ESP_LOGW(TAG,
"Nextion reported baud rate invalid!");
370 ESP_LOGW(TAG,
"Nextion reported invalid Waveform ID %d or Channel # %d was used!",
373 ESP_LOGN(TAG,
"Removing waveform from queue with component id %d and waveform id %d",
387 this->nextion_queue_.erase(this->nextion_queue_.begin() + found);
391 "Nextion reported invalid Waveform ID or Channel # was used but no waveform sensor in queue found!");
396 ESP_LOGW(TAG,
"Nextion reported variable name invalid!");
400 ESP_LOGW(TAG,
"Nextion reported variable operation invalid!");
404 ESP_LOGW(TAG,
"Nextion reported failed to assign variable!");
408 ESP_LOGW(TAG,
"Nextion reported operating EEPROM failed!");
411 ESP_LOGW(TAG,
"Nextion reported parameter quantity invalid!");
415 ESP_LOGW(TAG,
"Nextion reported component I/O operation invalid!");
418 ESP_LOGW(TAG,
"Nextion reported undefined escape characters!");
422 ESP_LOGW(TAG,
"Nextion reported too long variable name!");
426 ESP_LOGW(TAG,
"Nextion reported Serial Buffer overflow!");
429 if (to_process_length != 3) {
430 ESP_LOGW(TAG,
"Touch event data is expecting 3, received %zu", to_process_length);
434 uint8_t page_id = to_process[0];
435 uint8_t component_id = to_process[1];
436 uint8_t touch_event = to_process[2];
437 ESP_LOGD(TAG,
"Got touch page=%u component=%u type=%s", page_id, component_id,
438 touch_event ?
"PRESS" :
"RELEASE");
439 for (
auto *touch : this->
touch_) {
440 touch->process_touch(page_id, component_id, touch_event != 0);
446 if (to_process_length != 1) {
447 ESP_LOGW(TAG,
"New page event data is expecting 1, received %zu", to_process_length);
451 uint8_t page_id = to_process[0];
452 ESP_LOGD(TAG,
"Got new page=%u", page_id);
461 if (to_process_length != 5) {
462 ESP_LOGW(TAG,
"Touch coordinate data is expecting 5, received %zu", to_process_length);
463 ESP_LOGW(TAG,
"%s", to_process.c_str());
467 uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
468 uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
469 uint8_t touch_event = to_process[4];
470 ESP_LOGD(TAG,
"Got touch at x=%u y=%u type=%s", x, y, touch_event ?
"PRESS" :
"RELEASE");
481 ESP_LOGW(TAG,
"ERROR: Received string return but the queue is empty");
489 ESP_LOGE(TAG,
"ERROR: Received string return but next in queue \"%s\" is not a text sensor",
492 ESP_LOGN(TAG,
"Received get_string response: \"%s\" for component id: %s, type: %s", to_process.c_str(),
510 ESP_LOGE(TAG,
"ERROR: Received numeric return but the queue is empty");
514 if (to_process_length == 0) {
515 ESP_LOGE(TAG,
"ERROR: Received numeric return but no data!");
523 for (
int i = 0; i < 4; ++i) {
524 value += to_process[i] << (8 * i);
534 ESP_LOGE(TAG,
"ERROR: Received numeric return but next in queue \"%s\" is not a valid sensor type %d",
537 ESP_LOGN(TAG,
"Received numeric return for variable %s, queue type %d:%s, value %d",
550 ESP_LOGVV(TAG,
"Received Nextion entering sleep automatically");
557 ESP_LOGVV(TAG,
"Received Nextion leaves sleep automatically");
565 ESP_LOGD(TAG,
"system successful start up %zu", to_process_length);
579 std::string variable_name;
582 auto index = to_process.find(
'\0');
583 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
584 ESP_LOGE(TAG,
"Bad switch component data received for 0x90 event!");
585 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
589 variable_name = to_process.substr(0, index);
592 ESP_LOGN(TAG,
"Got Switch variable_name=%s value=%d", variable_name.c_str(), to_process[0] != 0);
595 switchtype->process_bool(variable_name, to_process[index] != 0);
606 std::string variable_name;
608 auto index = to_process.find(
'\0');
609 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
610 ESP_LOGE(TAG,
"Bad sensor component data received for 0x91 event!");
611 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
615 index = to_process.find(
'\0');
616 variable_name = to_process.substr(0, index);
619 for (
int i = 0; i < 4; ++i) {
620 value += to_process[i + index + 1] << (8 * i);
623 ESP_LOGN(TAG,
"Got sensor variable_name=%s value=%d", variable_name.c_str(), value);
626 sensor->process_sensor(variable_name, value);
639 std::string variable_name;
640 std::string text_value;
643 auto index = to_process.find(
'\0');
644 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
645 ESP_LOGE(TAG,
"Bad text sensor component data received for 0x92 event!");
646 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
650 variable_name = to_process.substr(0, index);
653 text_value = to_process.substr(index);
655 ESP_LOGN(TAG,
"Got Text Sensor variable_name=%s value=%s", variable_name.c_str(), text_value.c_str());
662 textsensortype->process_text(variable_name, text_value);
673 std::string variable_name;
676 auto index = to_process.find(
'\0');
677 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
678 ESP_LOGE(TAG,
"Bad binary sensor component data received for 0x92 event!");
679 ESP_LOGN(TAG,
"to_process %s %zu %d", to_process.c_str(), to_process_length, index);
683 variable_name = to_process.substr(0, index);
686 ESP_LOGN(TAG,
"Got Binary Sensor variable_name=%s value=%d", variable_name.c_str(), to_process[index] != 0);
689 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
694 ESP_LOGVV(TAG,
"Nextion reported data transmit finished!");
698 ESP_LOGVV(TAG,
"Nextion reported ready for transmit!");
703 auto *component = nb->component;
705 size_t buffer_to_send = component->get_wave_buffer().size() < 255 ? component->get_wave_buffer().size()
708 this->
write_array(component->get_wave_buffer().data(),
static_cast<int>(buffer_to_send));
710 ESP_LOGN(TAG,
"Nextion sending waveform data for component id %d and waveform id %d, size %zu",
711 component->get_component_id(), component->get_wave_channel_id(), buffer_to_send);
713 if (component->get_wave_buffer().size() <= 255) {
714 component->get_wave_buffer().clear();
716 component->get_wave_buffer().erase(component->get_wave_buffer().begin(),
717 component->get_wave_buffer().begin() + buffer_to_send);
728 ESP_LOGE(TAG,
"No waveforms in queue to send data!");
731 this->nextion_queue_.erase(this->nextion_queue_.begin() + found);
736 ESP_LOGW(TAG,
"Received unknown event from nextion: 0x%02X", this->
nextion_event_);
741 this->
command_data_.erase(0, to_process_length + COMMAND_DELIMITER.length() + 1);
753 ESP_LOGD(TAG,
"Removing old queue type \"%s\" name \"%s\" queue_time 0",
761 ESP_LOGD(TAG,
"Removing old queue type \"%s\" name \"%s\"", component->
get_queue_type_string().c_str(),
781 ESP_LOGN(TAG,
"Loop End");
791 ESP_LOGN(TAG,
"Received state for variable %s, state %lf for queue type %d", name.c_str(),
state, queue_type);
793 switch (queue_type) {
796 if (name == sensor->get_variable_name()) {
797 sensor->set_state(state,
true,
true);
805 if (name == sensor->get_variable_name()) {
806 sensor->set_state(state != 0,
true,
true);
814 if (name == sensor->get_variable_name()) {
815 sensor->set_state(state != 0,
true,
true);
822 ESP_LOGW(TAG,
"set_nextion_sensor_state does not support a queue type %d", queue_type);
828 ESP_LOGD(TAG,
"Received state for variable %s, state %s", name.c_str(), state.c_str());
831 if (name == sensor->get_variable_name()) {
832 sensor->set_state(state,
true,
true);
839 ESP_LOGD(TAG,
"all_components_send_state_ ");
841 if (force_update || binarysensortype->get_needs_to_send_update())
842 binarysensortype->send_state_to_nextion();
845 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_chan_id() == 0)
846 sensortype->send_state_to_nextion();
849 if (force_update || switchtype->get_needs_to_send_update())
850 switchtype->send_state_to_nextion();
853 if (force_update || textsensortype->get_needs_to_send_update())
854 textsensortype->send_state_to_nextion();
860 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
861 binarysensortype->update_component_settings(
true);
864 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
865 sensortype->update_component_settings(
true);
868 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
869 switchtype->update_component_settings(
true);
872 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
873 textsensortype->update_component_settings(
true);
880 uint8_t nr_of_ff_bytes = 0;
882 bool exit_flag =
false;
883 bool ff_flag =
false;
887 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
896 if (nr_of_ff_bytes >= 3)
899 response += (char) c;
901 if (response.find(0x05) != std::string::npos) {
908 if (exit_flag || ff_flag) {
914 response = response.substr(0, response.length() - 3);
916 ret = response.length();
962 va_start(arg, format);
963 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
966 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
987 va_start(arg, format);
988 int ret = vsnprintf(buffer,
sizeof(buffer), format, arg);
991 ESP_LOGW(TAG,
"Building command for format '%s' failed!", format);
1014 const std::string &variable_name_to_send,
int state_value) {
1019 const std::string &variable_name_to_send,
int state_value,
1020 bool is_sleep_safe) {
1041 const std::string &variable_name_to_send,
1042 const std::string &state_value) {
1047 const std::string &variable_name_to_send,
1048 const std::string &state_value,
bool is_sleep_safe) {
1053 state_value.c_str());
1107 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.
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_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
uint32_t startup_override_ms_
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_
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_
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int state_value, bool is_sleep_safe=false)
uint32_t IRAM_ATTR HOT millis()
virtual std::string get_queue_type_string()
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)
size_t get_wave_buffer_size()
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::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 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 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
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_
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
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)