ESPHome  2023.11.6
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 USE_ESP32
16 #include <HTTPClient.h>
17 #endif
18 #ifdef USE_ESP8266
19 #include <ESP8266HTTPClient.h>
20 #include <WiFiClientSecure.h>
21 #endif
22 #endif
23 
24 namespace esphome {
25 namespace nextion {
26 
27 class Nextion;
28 class NextionComponentBase;
29 
30 using nextion_writer_t = std::function<void(Nextion &)>;
31 
32 static const std::string COMMAND_DELIMITER{static_cast<char>(255), static_cast<char>(255), static_cast<char>(255)};
33 
34 class Nextion : public NextionBase, public PollingComponent, public uart::UARTDevice {
35  public:
48  void set_component_text(const char *component, const char *text);
64  void set_component_text_printf(const char *component, const char *format, ...) __attribute__((format(printf, 3, 4)));
77  void set_component_value(const char *component, int value);
90  void set_component_picture(const char *component, const char *picture);
103  void set_component_background_color(const char *component, uint32_t color);
118  void set_component_background_color(const char *component, const char *color);
131  void set_component_background_color(const char *component, Color color) override;
145  void set_component_pressed_background_color(const char *component, uint32_t color);
161  void set_component_pressed_background_color(const char *component, const char *color);
177  void set_component_pressed_background_color(const char *component, Color color) override;
178 
191  void set_component_pic(const char *component, uint8_t pic_id);
204  void set_component_picc(const char *component, uint8_t pic_id);
205 
218  void set_component_font_color(const char *component, uint32_t color);
233  void set_component_font_color(const char *component, const char *color);
248  void set_component_font_color(const char *component, Color color) override;
261  void set_component_pressed_font_color(const char *component, uint32_t color);
276  void set_component_pressed_font_color(const char *component, const char *color);
291  void set_component_pressed_font_color(const char *component, Color color) override;
305  void set_component_coordinates(const char *component, int x, int y);
318  void set_component_font(const char *component, uint8_t font_id) override;
323  void set_nextion_rtc_time(ESPTime time);
324 
336  void goto_page(const char *page);
348  void goto_page(uint8_t page);
360  void hide_component(const char *component) override;
372  void show_component(const char *component) override;
384  void enable_component_touch(const char *component);
396  void disable_component_touch(const char *component);
403  void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value);
404  void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value);
418  void display_picture(int picture_id, int x_start, int y_start);
436  void fill_area(int x1, int y1, int width, int height, const char *color);
454  void fill_area(int x1, int y1, int width, int height, Color color);
473  void line(int x1, int y1, int x2, int y2, const char *color);
492  void line(int x1, int y1, int x2, int y2, Color color);
511  void rectangle(int x1, int y1, int width, int height, const char *color);
530  void rectangle(int x1, int y1, int width, int height, Color color);
538  void circle(int center_x, int center_y, int radius, const char *color);
546  void circle(int center_x, int center_y, int radius, Color color);
563  void filled_circle(int center_x, int center_y, int radius, const char *color);
580  void filled_circle(int center_x, int center_y, int radius, Color color);
581 
593  void set_backlight_brightness(float brightness);
606  void set_touch_sleep_timeout(uint16_t timeout);
619  void set_wake_up_page(uint8_t page_id = 255);
632  void set_start_up_page(uint8_t page_id = 255);
633 
646  void set_auto_wake_on_touch(bool auto_wake);
651  void sleep(bool sleep);
658  void set_protocol_reparse_mode(bool active_mode);
659 
660  // ========== INTERNAL METHODS ==========
661  // (In most use cases you won't need these)
662  void register_touch_component(NextionComponentBase *obj) { this->touch_.push_back(obj); }
663  void register_switch_component(NextionComponentBase *obj) { this->switchtype_.push_back(obj); }
665  void register_sensor_component(NextionComponentBase *obj) { this->sensortype_.push_back(obj); }
667 
668  void setup() override;
669  void set_brightness(float brightness) { this->brightness_ = brightness; }
670  float get_setup_priority() const override;
671  void update() override;
672  void loop() override;
673  void set_writer(const nextion_writer_t &writer);
674 
675  // This function has been deprecated
676  void set_wait_for_ack(bool wait_for_ack);
677 
684  bool send_command_printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
685 
686 #ifdef USE_NEXTION_TFT_UPLOAD
687 
690  void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
691 
692 #endif
693 
697  void upload_tft();
698  void dump_config() override;
699 
703  void soft_reset();
704 
709  void add_sleep_state_callback(std::function<void()> &&callback);
710 
715  void add_wake_state_callback(std::function<void()> &&callback);
716 
721  void add_setup_state_callback(std::function<void()> &&callback);
722 
727  void add_new_page_callback(std::function<void(uint8_t)> &&callback);
728 
729  void update_all_components();
730 
743  void set_nextion_sensor_state(int queue_type, const std::string &name, float state);
744  void set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state);
745  void set_nextion_text_state(const std::string &name, const std::string &state);
746 
747  void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override;
748  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
749  int state_value) override;
750 
751  void add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &state_value) override;
752  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
753  const std::string &state_value) override;
754 
755  void add_to_get_queue(NextionComponentBase *component) override;
756 
757  void add_addt_command_to_queue(NextionComponentBase *component) override;
758 
759  void update_components_by_prefix(const std::string &prefix);
760 
761  void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout) {
762  this->touch_sleep_timeout_ = touch_sleep_timeout;
763  }
764  void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
765  void set_start_up_page_internal(uint8_t start_up_page) { this->start_up_page_ = start_up_page; }
766  void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
767 
768  protected:
769  std::deque<NextionQueue *> nextion_queue_;
770  std::deque<NextionQueue *> waveform_queue_;
771  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
772  void all_components_send_state_(bool force_update = false);
773  uint64_t comok_sent_ = 0;
774  bool remove_from_q_(bool report_empty = true);
779  bool ignore_is_setup_ = false;
781  uint8_t nextion_event_;
782 
784  void process_serial_();
785  bool is_updating_ = false;
786  uint32_t touch_sleep_timeout_ = 0;
787  int wake_up_page_ = -1;
788  int start_up_page_ = -1;
789  bool auto_wake_on_touch_ = true;
790 
795  bool send_command_(const std::string &command);
796  void add_no_result_to_queue_(const std::string &variable_name);
797  bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...)
798  __attribute__((format(printf, 3, 4)));
799  void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command);
800 
801  bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...)
802  __attribute__((format(printf, 3, 4)));
803 
804  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
805  const std::string &variable_name_to_send, int state_value,
806  bool is_sleep_safe = false);
807 
808  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
809  const std::string &variable_name_to_send,
810  const std::string &state_value, bool is_sleep_safe = false);
811 
813 
814 #ifdef USE_NEXTION_TFT_UPLOAD
815 #ifdef USE_ESP8266
816  WiFiClient *wifi_client_{nullptr};
817  BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
818  WiFiClient *get_wifi_client_();
819 #endif
820 
829  int tft_size_ = 0;
830  int upload_by_chunks_(HTTPClient *http, int range_start);
831 
832  bool upload_with_range_(uint32_t range_start, uint32_t range_end);
833 
841  bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
842  void upload_end_();
843 
844 #endif // USE_NEXTION_TFT_UPLOAD
845 
846  bool get_is_connected_() { return this->is_connected_; }
847 
848  bool check_connect_();
849 
850  std::vector<NextionComponentBase *> touch_;
851  std::vector<NextionComponentBase *> switchtype_;
852  std::vector<NextionComponentBase *> sensortype_;
853  std::vector<NextionComponentBase *> textsensortype_;
854  std::vector<NextionComponentBase *> binarysensortype_;
859 
861  float brightness_{1.0};
862 
863  std::string device_model_;
864  std::string firmware_version_;
865  std::string serial_number_;
866  std::string flash_size_;
867 
869 
870 #ifdef USE_NEXTION_TFT_UPLOAD
871  std::string tft_url_;
872  uint8_t *transfer_buffer_{nullptr};
875 #endif
876 
877 #ifdef NEXTION_PROTOCOL_LOG
878  void print_queue_members_();
879 #endif
880  void reset_(bool reset_nextion = true);
881 
882  std::string command_data_;
883  bool is_connected_ = false;
884  uint32_t startup_override_ms_ = 8000;
885  uint32_t max_q_age_ms_ = 8000;
886  uint32_t started_ms_ = 0;
887  bool sent_setup_commands_ = false;
888 };
889 } // namespace nextion
890 } // 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:779
void all_components_send_state_(bool force_update=false)
Definition: nextion.cpp:821
CallbackManager< void(uint8_t)> page_callback_
Definition: nextion.h:858
CallbackManager< void()> sleep_callback_
Definition: nextion.h:856
void line(int x1, int y1, int x2, int y2, const char *color)
Draw a line on the screen.
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:165
void set_start_up_page_internal(uint8_t start_up_page)
Definition: nextion.h:765
void upload_tft()
Upload the tft file and softreset the Nextion.
void add_wake_state_callback(std::function< void()> &&callback)
Add a callback to be notified of wake state changes.
Definition: nextion.cpp:157
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:187
void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override
Definition: nextion.cpp:991
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
Definition: nextion.cpp:1067
void circle(int center_x, int center_y, int radius, const char *color)
Draw a circle outline.
void set_component_pressed_background_color(const char *component, uint32_t color)
Set the pressed background color of a component.
uint32_t startup_override_ms_
Definition: nextion.h:884
void add_to_get_queue(NextionComponentBase *component) override
Definition: nextion.cpp:1039
std::vector< NextionComponentBase * > touch_
Definition: nextion.h:850
optional< nextion_writer_t > writer_
Definition: nextion.h:860
void filled_circle(int center_x, int center_y, int radius, const char *color)
Draw a filled circled.
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:690
A more user-friendly version of struct tm from time.h.
Definition: time.h:12
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:161
int upload_by_chunks_(HTTPClient *http, int range_start)
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:28
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:143
void set_wake_up_page_internal(uint8_t wake_up_page)
Definition: nextion.h:764
void setup() override
Definition: nextion.cpp:11
std::string serial_number_
Definition: nextion.h:865
void set_brightness(float brightness)
Definition: nextion.h:669
void register_binarysensor_component(NextionComponentBase *obj)
Definition: nextion.h:664
void register_touch_component(NextionComponentBase *obj)
Definition: nextion.h:662
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:282
BearSSL::WiFiClientSecure * wifi_client_secure_
Definition: nextion.h:817
void disable_component_touch(const char *component)
Disable touch for a component.
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:1001
void set_component_picture(const char *component, const char *picture)
Set the picture of an image component.
void set_component_pressed_font_color(const char *component, uint32_t color)
Set the pressed font color of a component.
void show_component(const char *component) override
Show a component.
void rectangle(int x1, int y1, int width, int height, const char *color)
Draw a rectangle outline.
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 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:665
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:964
void add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
Definition: nextion.cpp:153
void register_switch_component(NextionComponentBase *obj)
Definition: nextion.h:663
std::string flash_size_
Definition: nextion.h:866
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform 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_component_background_color(const char *component, uint32_t color)
Set the background color of a component.
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
Definition: nextion.cpp:769
CallbackManager< void()> setup_callback_
Definition: nextion.h:855
void set_nextion_text_state(const std::string &name, const std::string &state)
Definition: nextion.cpp:810
void set_wait_for_ack(bool wait_for_ack)
void loop() override
Definition: nextion.cpp:228
void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout)
Definition: nextion.h:761
std::deque< NextionQueue * > nextion_queue_
Definition: nextion.h:769
CallbackManager< void()> wake_callback_
Definition: nextion.h:857
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:766
void set_component_text(const char *component, const char *text)
Set the text of a component to a static string.
void add_no_result_to_queue_(const std::string &variable_name)
Definition: nextion.cpp:908
std::deque< NextionQueue * > waveform_queue_
Definition: nextion.h:770
void fill_area(int x1, int y1, int width, int height, const char *color)
Fill a rectangle with a color.
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:882
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:271
uint8_t * transfer_buffer_
Definition: nextion.h:872
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
Definition: nextion.h:863
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:938
void register_textsensor_component(NextionComponentBase *obj)
Definition: nextion.h:666
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
Definition: nextion.h:853
std::string tft_url_
Definition: nextion.h:871
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:852
void dump_config() override
Definition: nextion.cpp:122
std::vector< NextionComponentBase * > switchtype_
Definition: nextion.h:851
bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size)
start update tft file to nextion.
WiFiClient * wifi_client_
Definition: nextion.h:816
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
Implementation of SPI Controller mode.
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:929
void reset_(bool reset_nextion=true)
Definition: nextion.cpp:112
void update_components_by_prefix(const std::string &prefix)
Definition: nextion.cpp:841
void update() override
Definition: nextion.cpp:144
std::string firmware_version_
Definition: nextion.h:864
uint32_t touch_sleep_timeout_
Definition: nextion.h:786
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
Definition: nextion.cpp:860
std::function< void(Nextion &)> nextion_writer_t
Definition: nextion.h:30
std::vector< NextionComponentBase * > binarysensortype_
Definition: nextion.h:854
bool state
Definition: fan.h:34
int content_length_
will request chunk_size chunks from the web server and send each to the nextion
Definition: nextion.h:828
void set_writer(const nextion_writer_t &writer)
Definition: nextion.cpp:1099
void set_component_font_color(const char *component, uint32_t color)
Set the font color of a component.