ESPHome  2024.3.2
nextion.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <deque>
4 #include <vector>
5 
6 #include "esphome/core/defines.h"
7 #include "esphome/core/time.h"
8 
10 #include "nextion_base.h"
11 #include "nextion_component.h"
13 
14 #ifdef USE_NEXTION_TFT_UPLOAD
15 #ifdef ARDUINO
16 #ifdef USE_ESP32
17 #include <HTTPClient.h>
18 #endif // USE_ESP32
19 #ifdef USE_ESP8266
20 #include <ESP8266HTTPClient.h>
21 #include <WiFiClientSecure.h>
22 #endif // USE_ESP8266
23 #elif defined(USE_ESP_IDF)
24 #include <esp_http_client.h>
25 #endif // ARDUINO vs ESP-IDF
26 #endif // USE_NEXTION_TFT_UPLOAD
27 
28 namespace esphome {
29 namespace nextion {
30 
31 class Nextion;
32 class NextionComponentBase;
33 
34 using nextion_writer_t = std::function<void(Nextion &)>;
35 
36 static const std::string COMMAND_DELIMITER{static_cast<char>(255), static_cast<char>(255), static_cast<char>(255)};
37 
38 class Nextion : public NextionBase, public PollingComponent, public uart::UARTDevice {
39  public:
52  void set_component_text(const char *component, const char *text);
68  void set_component_text_printf(const char *component, const char *format, ...) __attribute__((format(printf, 3, 4)));
81  void set_component_value(const char *component, int value);
94  void set_component_picture(const char *component, const char *picture);
109  void set_component_background_color(const char *component, uint16_t color);
123  void set_component_background_color(const char *component, const char *color);
137  void set_component_background_color(const char *component, Color color) override;
153  void set_component_pressed_background_color(const char *component, uint16_t color);
168  void set_component_pressed_background_color(const char *component, const char *color);
183  void set_component_pressed_background_color(const char *component, Color color) override;
198  void set_component_foreground_color(const char *component, uint16_t color);
212  void set_component_foreground_color(const char *component, const char *color);
225  void set_component_foreground_color(const char *component, Color color) override;
241  void set_component_pressed_foreground_color(const char *component, uint16_t color);
256  void set_component_pressed_foreground_color(const char *component, const char *color);
271  void set_component_pressed_foreground_color(const char *component, Color color) override;
272 
285  void set_component_pic(const char *component, uint8_t pic_id);
298  void set_component_picc(const char *component, uint8_t pic_id);
299 
314  void set_component_font_color(const char *component, uint16_t color);
328  void set_component_font_color(const char *component, const char *color);
341  void set_component_font_color(const char *component, Color color) override;
356  void set_component_pressed_font_color(const char *component, uint16_t color);
370  void set_component_pressed_font_color(const char *component, const char *color);
383  void set_component_pressed_font_color(const char *component, Color color) override;
397  void set_component_coordinates(const char *component, int x, int y);
410  void set_component_font(const char *component, uint8_t font_id) override;
415  void set_nextion_rtc_time(ESPTime time);
416 
428  void goto_page(const char *page);
440  void goto_page(uint8_t page);
452  void hide_component(const char *component) override;
464  void show_component(const char *component) override;
476  void enable_component_touch(const char *component);
488  void disable_component_touch(const char *component);
495  void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value);
496  void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value);
510  void display_picture(int picture_id, int x_start, int y_start);
529  void fill_area(int x1, int y1, int width, int height, uint16_t color);
547  void fill_area(int x1, int y1, int width, int height, const char *color);
565  void fill_area(int x1, int y1, int width, int height, Color color);
584  void line(int x1, int y1, int x2, int y2, uint16_t color);
602  void line(int x1, int y1, int x2, int y2, const char *color);
620  void line(int x1, int y1, int x2, int y2, Color color);
639  void rectangle(int x1, int y1, int width, int height, uint16_t color);
657  void rectangle(int x1, int y1, int width, int height, const char *color);
675  void rectangle(int x1, int y1, int width, int height, Color color);
685  void circle(int center_x, int center_y, int radius, uint16_t color);
694  void circle(int center_x, int center_y, int radius, const char *color);
702  void circle(int center_x, int center_y, int radius, Color color);
719  void filled_circle(int center_x, int center_y, int radius, uint16_t color);
735  void filled_circle(int center_x, int center_y, int radius, const char *color);
751  void filled_circle(int center_x, int center_y, int radius, Color color);
752 
771  void qrcode(int x1, int y1, const char *content, int size = 200, uint16_t background_color = 65535,
772  uint16_t foreground_color = 0, int logo_pic = -1, uint8_t border_width = 8);
794  void qrcode(int x1, int y1, const char *content, int size, Color background_color = Color(255, 255, 255),
795  Color foreground_color = Color(0, 0, 0), int logo_pic = -1, uint8_t border_width = 8);
796 
808  void set_backlight_brightness(float brightness);
821  void set_touch_sleep_timeout(uint16_t timeout);
834  void set_wake_up_page(uint8_t page_id = 255);
847  void set_start_up_page(uint8_t page_id = 255);
848 
861  void set_auto_wake_on_touch(bool auto_wake);
874  void set_exit_reparse_on_start(bool exit_reparse);
879  void sleep(bool sleep);
886  void set_protocol_reparse_mode(bool active_mode);
887 
888  // ========== INTERNAL METHODS ==========
889  // (In most use cases you won't need these)
890  void register_touch_component(NextionComponentBase *obj) { this->touch_.push_back(obj); }
891  void register_switch_component(NextionComponentBase *obj) { this->switchtype_.push_back(obj); }
893  void register_sensor_component(NextionComponentBase *obj) { this->sensortype_.push_back(obj); }
895 
896  void setup() override;
897  void set_brightness(float brightness) { this->brightness_ = brightness; }
898  float get_setup_priority() const override;
899  void update() override;
900  void loop() override;
901  void set_writer(const nextion_writer_t &writer);
902 
903  // This function has been deprecated
904  void set_wait_for_ack(bool wait_for_ack);
905 
912  bool send_command_printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
913 
914 #ifdef USE_NEXTION_TFT_UPLOAD
915 
918  void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
919 
920 #endif
921 
926  bool upload_tft();
927 
928  void dump_config() override;
929 
933  void soft_reset();
934 
939  void add_sleep_state_callback(std::function<void()> &&callback);
940 
945  void add_wake_state_callback(std::function<void()> &&callback);
946 
951  void add_setup_state_callback(std::function<void()> &&callback);
952 
957  void add_new_page_callback(std::function<void(uint8_t)> &&callback);
958 
963  void add_touch_event_callback(std::function<void(uint8_t, uint8_t, bool)> &&callback);
964 
965  void update_all_components();
966 
979  void set_nextion_sensor_state(int queue_type, const std::string &name, float state);
980  void set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state);
981  void set_nextion_text_state(const std::string &name, const std::string &state);
982 
983  void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override;
984  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
985  int state_value) override;
986 
987  void add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &state_value) override;
988  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
989  const std::string &state_value) override;
990 
991  void add_to_get_queue(NextionComponentBase *component) override;
992 
993  void add_addt_command_to_queue(NextionComponentBase *component) override;
994 
995  void update_components_by_prefix(const std::string &prefix);
996 
997  void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout) {
998  this->touch_sleep_timeout_ = touch_sleep_timeout;
999  }
1000  void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
1001  void set_start_up_page_internal(uint8_t start_up_page) { this->start_up_page_ = start_up_page; }
1002  void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
1003  void set_exit_reparse_on_start_internal(bool exit_reparse_on_start) {
1004  this->exit_reparse_on_start_ = exit_reparse_on_start;
1005  }
1006 
1020  size_t queue_size() { return this->nextion_queue_.size(); }
1021 
1022  protected:
1023  std::deque<NextionQueue *> nextion_queue_;
1024  std::deque<NextionQueue *> waveform_queue_;
1025  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
1026  void all_components_send_state_(bool force_update = false);
1027  uint64_t comok_sent_ = 0;
1028  bool remove_from_q_(bool report_empty = true);
1033  bool ignore_is_setup_ = false;
1036 
1038  void process_serial_();
1039  bool is_updating_ = false;
1040  uint32_t touch_sleep_timeout_ = 0;
1041  int wake_up_page_ = -1;
1042  int start_up_page_ = -1;
1043  bool auto_wake_on_touch_ = true;
1045 
1050  bool send_command_(const std::string &command);
1051  void add_no_result_to_queue_(const std::string &variable_name);
1052  bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...)
1053  __attribute__((format(printf, 3, 4)));
1054  void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command);
1055 
1056  bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...)
1057  __attribute__((format(printf, 3, 4)));
1058 
1059  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
1060  const std::string &variable_name_to_send, int state_value,
1061  bool is_sleep_safe = false);
1062 
1063  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
1064  const std::string &variable_name_to_send,
1065  const std::string &state_value, bool is_sleep_safe = false);
1066 
1067  void check_pending_waveform_();
1068 
1069 #ifdef USE_NEXTION_TFT_UPLOAD
1070 #ifdef USE_ESP8266
1071  WiFiClient *wifi_client_{nullptr};
1072  BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
1073  WiFiClient *get_wifi_client_();
1074 #endif
1076  int tft_size_ = 0;
1077 #ifdef ARDUINO
1078 
1085  int upload_by_chunks_(HTTPClient *http, int range_start);
1086 
1087  bool upload_with_range_(uint32_t range_start, uint32_t range_end);
1088 
1096  bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
1103  bool upload_end_(bool successful);
1104 #elif defined(USE_ESP_IDF)
1105 
1112  int upload_range(const std::string &url, int range_start);
1119  bool upload_end(bool successful);
1120 #endif // ARDUINO vs ESP-IDF
1121 
1122 #endif // USE_NEXTION_TFT_UPLOAD
1123 
1124  bool get_is_connected_() { return this->is_connected_; }
1125 
1126  bool check_connect_();
1127 
1128  std::vector<NextionComponentBase *> touch_;
1129  std::vector<NextionComponentBase *> switchtype_;
1130  std::vector<NextionComponentBase *> sensortype_;
1131  std::vector<NextionComponentBase *> textsensortype_;
1132  std::vector<NextionComponentBase *> binarysensortype_;
1138 
1140  float brightness_{1.0};
1141 
1142  std::string device_model_;
1143  std::string firmware_version_;
1144  std::string serial_number_;
1145  std::string flash_size_;
1146 
1147  void remove_front_no_sensors_();
1148 
1149 #ifdef USE_NEXTION_TFT_UPLOAD
1150  std::string tft_url_;
1151  uint8_t *transfer_buffer_{nullptr};
1154 #endif
1155 
1156 #ifdef NEXTION_PROTOCOL_LOG
1157  void print_queue_members_();
1158 #endif
1159  void reset_(bool reset_nextion = true);
1160 
1161  std::string command_data_;
1162  bool is_connected_ = false;
1163  uint32_t startup_override_ms_ = 8000;
1164  uint32_t max_q_age_ms_ = 8000;
1165  uint32_t started_ms_ = 0;
1166  bool sent_setup_commands_ = false;
1167 };
1168 } // namespace nextion
1169 } // namespace esphome
void goto_page(const char *page)
Show the page with a given name.
void set_nextion_rtc_time(ESPTime time)
Send the current time to the nextion display.
bool ignore_is_setup_
Sends commands ignoring of the Nextion has been setup.
Definition: nextion.h:1033
int upload_range(const std::string &url, int range_start)
will request 4096 bytes chunks from the web server and send each to Nextion
void all_components_send_state_(bool force_update=false)
Definition: nextion.cpp:851
CallbackManager< void(uint8_t)> page_callback_
Definition: nextion.h:1136
CallbackManager< void()> sleep_callback_
Definition: nextion.h:1134
const char * name
Definition: stm32flash.h:78
void set_protocol_reparse_mode(bool active_mode)
Sets Nextion Protocol Reparse mode between active or passive.
void set_component_pic(const char *component, uint8_t pic_id)
Set the picture id of a component.
void add_new_page_callback(std::function< void(uint8_t)> &&callback)
Add a callback to be notified when the nextion changes pages.
Definition: nextion.cpp:171
void set_start_up_page_internal(uint8_t start_up_page)
Definition: nextion.h:1001
void add_wake_state_callback(std::function< void()> &&callback)
Add a callback to be notified of wake state changes.
Definition: nextion.cpp:163
void hide_component(const char *component) override
Hide a component.
bool send_command_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
Definition: nextion.cpp:197
void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override
Definition: nextion.cpp:1027
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
Definition: nextion.cpp:1103
uint32_t startup_override_ms_
Definition: nextion.h:1163
void add_to_get_queue(NextionComponentBase *component) override
Definition: nextion.cpp:1075
std::vector< NextionComponentBase * > touch_
Definition: nextion.h:1128
optional< nextion_writer_t > writer_
Definition: nextion.h:1139
uint16_t x
Definition: tt21100.cpp:17
bool void set_tft_url(const std::string &tft_url)
Set the tft file URL.
Definition: nextion.h:918
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
void add_setup_state_callback(std::function< void()> &&callback)
Add a callback to be notified when the nextion completes its initialize setup.
Definition: nextion.cpp:167
int upload_by_chunks_(HTTPClient *http, int range_start)
will request chunk_size chunks from the web server and send each to the nextion
bool send_command_(const std::string &command)
Manually send a raw command to the display and don&#39;t wait for an acknowledgement packet.
Definition: nextion.cpp:29
STL namespace.
void set_component_coordinates(const char *component, int x, int y)
Set the coordinates of a component on screen.
float get_setup_priority() const override
Definition: nextion.cpp:149
void set_wake_up_page_internal(uint8_t wake_up_page)
Definition: nextion.h:1000
void setup() override
Definition: nextion.cpp:12
std::string serial_number_
Definition: nextion.h:1144
void set_brightness(float brightness)
Definition: nextion.h:897
void register_binarysensor_component(NextionComponentBase *obj)
Definition: nextion.h:892
void register_touch_component(NextionComponentBase *obj)
Definition: nextion.h:890
void set_component_picc(const char *component, uint8_t pic_id)
Set the background picture id of component.
void sleep(bool sleep)
Sets Nextion mode between sleep and awake.
This class simplifies creating components that periodically check a state.
Definition: component.h:283
BearSSL::WiFiClientSecure * wifi_client_secure_
Definition: nextion.h:1072
void disable_component_touch(const char *component)
Disable touch for a component.
CallbackManager< void(uint8_t, uint8_t, bool)> touch_callback_
Definition: nextion.h:1137
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)
Definition: nextion.cpp:1037
bool upload_end_(bool successful)
Ends the upload process, restart Nextion and, if successful, restarts ESP.
void set_component_picture(const char *component, const char *picture)
Set the picture of an image component.
void set_exit_reparse_on_start(bool exit_reparse)
Sets if Nextion should exit the active reparse mode before the "connect" command is sent...
void show_component(const char *component) override
Show a component.
uint16_t y
Definition: tt21100.cpp:18
void void set_component_value(const char *component, int value)
Set the integer value of a component.
void filled_circle(int center_x, int center_y, int radius, uint16_t color)
Draw a filled circled.
bool upload_end(bool successful)
Ends the upload process, restart Nextion and, if successful, restarts ESP.
void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value)
bool upload_with_range_(uint32_t range_start, uint32_t range_end)
void register_sensor_component(NextionComponentBase *obj)
Definition: nextion.h:893
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.
Definition: nextion.cpp:1000
void add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
Definition: nextion.cpp:159
void register_switch_component(NextionComponentBase *obj)
Definition: nextion.h:891
void set_component_pressed_background_color(const char *component, uint16_t color)
Set the pressed background color of a component.
void set_component_pressed_foreground_color(const char *component, uint16_t color)
Set the pressed foreground color of a component.
std::string flash_size_
Definition: nextion.h:1145
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
void fill_area(int x1, int y1, int width, int height, uint16_t color)
Fill a rectangle with a color.
void set_component_background_color(const char *component, uint16_t color)
Set the background color of a component.
void set_component_text_printf(const char *component, const char *format,...) __attribute__((format(printf
Set the text of a component to a formatted string.
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
Definition: nextion.cpp:794
CallbackManager< void()> setup_callback_
Definition: nextion.h:1133
void set_nextion_text_state(const std::string &name, const std::string &state)
Definition: nextion.cpp:838
void set_wait_for_ack(bool wait_for_ack)
void loop() override
Definition: nextion.cpp:238
void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout)
Definition: nextion.h:997
std::deque< NextionQueue * > nextion_queue_
Definition: nextion.h:1023
CallbackManager< void()> wake_callback_
Definition: nextion.h:1135
void display_picture(int picture_id, int x_start, int y_start)
Display a picture at coordinates.
void set_auto_wake_on_touch_internal(bool auto_wake_on_touch)
Definition: nextion.h:1002
void set_component_text(const char *component, const char *text)
Set the text of a component to a static string.
void set_component_font_color(const char *component, uint16_t color)
Set the font color of a component.
void add_no_result_to_queue_(const std::string &variable_name)
Definition: nextion.cpp:944
std::deque< NextionQueue * > waveform_queue_
Definition: nextion.h:1024
void set_component_font(const char *component, uint8_t font_id) override
Set the font id for a component.
std::string command_data_
Definition: nextion.h:1161
enum esphome::EntityCategory __attribute__
void enable_component_touch(const char *component)
Enable touch for a component.
bool remove_from_q_(bool report_empty=true)
Definition: nextion.cpp:282
uint8_t * transfer_buffer_
Definition: nextion.h:1151
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
Definition: nextion.h:1142
void soft_reset()
Softreset the Nextion.
bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
Definition: nextion.cpp:974
void register_textsensor_component(NextionComponentBase *obj)
Definition: nextion.h:894
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
Definition: nextion.h:1131
bool upload_tft()
Upload the tft file and soft reset 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.
Definition: nextion.cpp:175
void set_wake_up_page(uint8_t page_id=255)
Sets which page Nextion loads when exiting sleep mode.
std::vector< NextionComponentBase * > sensortype_
Definition: nextion.h:1130
void dump_config() override
Definition: nextion.cpp:127
void qrcode(int x1, int y1, const char *content, int size=200, uint16_t background_color=65535, uint16_t foreground_color=0, int logo_pic=-1, uint8_t border_width=8)
Draws a QR code in the screen.
std::vector< NextionComponentBase * > switchtype_
Definition: nextion.h:1129
bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size)
start update tft file to nextion.
void rectangle(int x1, int y1, int width, int height, uint16_t color)
Draw a rectangle outline.
WiFiClient * wifi_client_
Definition: nextion.h:1071
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_start_up_page(uint8_t page_id=255)
Sets which page Nextion loads when connecting to ESPHome.
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
Definition: nextion.cpp:965
void reset_(bool reset_nextion=true)
Definition: nextion.cpp:117
void set_exit_reparse_on_start_internal(bool exit_reparse_on_start)
Definition: nextion.h:1003
void update_components_by_prefix(const std::string &prefix)
Definition: nextion.cpp:871
void update() override
Definition: nextion.cpp:150
std::string firmware_version_
Definition: nextion.h:1143
void set_component_foreground_color(const char *component, uint16_t color)
Set the foreground color of a component.
void set_component_pressed_font_color(const char *component, uint16_t color)
Set the pressed font color of a component.
size_t queue_size()
Retrieves the number of commands pending in the Nextion command queue.
Definition: nextion.h:1020
uint32_t touch_sleep_timeout_
Definition: nextion.h:1040
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
Definition: nextion.cpp:890
void circle(int center_x, int center_y, int radius, uint16_t color)
Draw a circle outline.
std::function< void(Nextion &)> nextion_writer_t
Definition: nextion.h:34
std::vector< NextionComponentBase * > binarysensortype_
Definition: nextion.h:1132
bool state
Definition: fan.h:34
void set_writer(const nextion_writer_t &writer)
Definition: nextion.cpp:1135
void line(int x1, int y1, int x2, int y2, uint16_t color)
Draw a line on the screen.