ESPHome  2023.5.5
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"
8 #include "nextion_base.h"
9 #include "nextion_component.h"
11 
12 #ifdef USE_NEXTION_TFT_UPLOAD
13 #ifdef USE_ESP32
14 #include <HTTPClient.h>
15 #endif
16 #ifdef USE_ESP8266
17 #include <ESP8266HTTPClient.h>
18 #include <WiFiClientSecure.h>
19 #endif
20 #endif
21 
22 #ifdef USE_TIME
24 #endif
25 
26 namespace esphome {
27 namespace nextion {
28 
29 class Nextion;
30 class NextionComponentBase;
31 
32 using nextion_writer_t = std::function<void(Nextion &)>;
33 
34 static const std::string COMMAND_DELIMITER{static_cast<char>(255), static_cast<char>(255), static_cast<char>(255)};
35 
36 class Nextion : public NextionBase, public PollingComponent, public uart::UARTDevice {
37  public:
50  void set_component_text(const char *component, const char *text);
66  void set_component_text_printf(const char *component, const char *format, ...) __attribute__((format(printf, 3, 4)));
79  void set_component_value(const char *component, int value);
92  void set_component_picture(const char *component, const char *picture);
105  void set_component_background_color(const char *component, uint32_t color);
120  void set_component_background_color(const char *component, const char *color);
133  void set_component_background_color(const char *component, Color color) override;
147  void set_component_pressed_background_color(const char *component, uint32_t color);
163  void set_component_pressed_background_color(const char *component, const char *color);
179  void set_component_pressed_background_color(const char *component, Color color) override;
180 
193  void set_component_pic(const char *component, uint8_t pic_id);
206  void set_component_picc(const char *component, uint8_t pic_id);
207 
220  void set_component_font_color(const char *component, uint32_t color);
235  void set_component_font_color(const char *component, const char *color);
250  void set_component_font_color(const char *component, Color color) override;
263  void set_component_pressed_font_color(const char *component, uint32_t color);
278  void set_component_pressed_font_color(const char *component, const char *color);
293  void set_component_pressed_font_color(const char *component, Color color) override;
307  void set_component_coordinates(const char *component, int x, int y);
320  void set_component_font(const char *component, uint8_t font_id) override;
321 #ifdef USE_TIME
322 
327 #endif
328 
340  void goto_page(const char *page);
352  void hide_component(const char *component) override;
364  void show_component(const char *component) override;
376  void enable_component_touch(const char *component);
388  void disable_component_touch(const char *component);
395  void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value);
396  void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value);
410  void display_picture(int picture_id, int x_start, int y_start);
428  void fill_area(int x1, int y1, int width, int height, const char *color);
446  void fill_area(int x1, int y1, int width, int height, Color color);
465  void line(int x1, int y1, int x2, int y2, const char *color);
484  void line(int x1, int y1, int x2, int y2, Color color);
503  void rectangle(int x1, int y1, int width, int height, const char *color);
522  void rectangle(int x1, int y1, int width, int height, Color color);
530  void circle(int center_x, int center_y, int radius, const char *color);
538  void circle(int center_x, int center_y, int radius, Color color);
555  void filled_circle(int center_x, int center_y, int radius, const char *color);
572  void filled_circle(int center_x, int center_y, int radius, Color color);
573 
585  void set_backlight_brightness(float brightness);
598  void set_touch_sleep_timeout(uint16_t timeout);
611  void set_wake_up_page(uint8_t page_id = 255);
624  void set_auto_wake_on_touch(bool auto_wake);
629  void sleep(bool sleep);
630 
631  // ========== INTERNAL METHODS ==========
632  // (In most use cases you won't need these)
633  void register_touch_component(NextionComponentBase *obj) { this->touch_.push_back(obj); }
634  void register_switch_component(NextionComponentBase *obj) { this->switchtype_.push_back(obj); }
636  void register_sensor_component(NextionComponentBase *obj) { this->sensortype_.push_back(obj); }
638 
639  void setup() override;
640  void set_brightness(float brightness) { this->brightness_ = brightness; }
641  float get_setup_priority() const override;
642  void update() override;
643  void loop() override;
644  void set_writer(const nextion_writer_t &writer);
645 
646  // This function has been deprecated
647  void set_wait_for_ack(bool wait_for_ack);
648 
655  bool send_command_printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
656 
657 #ifdef USE_NEXTION_TFT_UPLOAD
658 
661  void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
662 
663 #endif
664 
668  void upload_tft();
669  void dump_config() override;
670 
674  void soft_reset();
675 
680  void add_sleep_state_callback(std::function<void()> &&callback);
681 
686  void add_wake_state_callback(std::function<void()> &&callback);
687 
692  void add_setup_state_callback(std::function<void()> &&callback);
693 
698  void add_new_page_callback(std::function<void(uint8_t)> &&callback);
699 
700  void update_all_components();
701 
714  void set_nextion_sensor_state(int queue_type, const std::string &name, float state);
715  void set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state);
716  void set_nextion_text_state(const std::string &name, const std::string &state);
717 
718  void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override;
719  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
720  int state_value) override;
721 
722  void add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &state_value) override;
723  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
724  const std::string &state_value) override;
725 
726  void add_to_get_queue(NextionComponentBase *component) override;
727 
728  void add_addt_command_to_queue(NextionComponentBase *component) override;
729 
730  void update_components_by_prefix(const std::string &prefix);
731 
732  void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout) {
733  this->touch_sleep_timeout_ = touch_sleep_timeout;
734  }
735  void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
736  void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
737 
738  protected:
739  std::deque<NextionQueue *> nextion_queue_;
740  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
741  void all_components_send_state_(bool force_update = false);
742  uint64_t comok_sent_ = 0;
743  bool remove_from_q_(bool report_empty = true);
748  bool ignore_is_setup_ = false;
750  uint8_t nextion_event_;
751 
753  void process_serial_();
754  bool is_updating_ = false;
755  uint32_t touch_sleep_timeout_ = 0;
756  int wake_up_page_ = -1;
757  bool auto_wake_on_touch_ = true;
758 
763  bool send_command_(const std::string &command);
764  void add_no_result_to_queue_(const std::string &variable_name);
765  bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...)
766  __attribute__((format(printf, 3, 4)));
767  void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command);
768 
769  bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...)
770  __attribute__((format(printf, 3, 4)));
771 
772  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
773  const std::string &variable_name_to_send, int state_value,
774  bool is_sleep_safe = false);
775 
776  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
777  const std::string &variable_name_to_send,
778  const std::string &state_value, bool is_sleep_safe = false);
779 
780 #ifdef USE_NEXTION_TFT_UPLOAD
781 #ifdef USE_ESP8266
782  WiFiClient *wifi_client_{nullptr};
783  BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
784  WiFiClient *get_wifi_client_();
785 #endif
786 
795  int tft_size_ = 0;
796  int upload_by_chunks_(HTTPClient *http, int range_start);
797 
798  bool upload_with_range_(uint32_t range_start, uint32_t range_end);
799 
807  bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
808  void upload_end_();
809 
810 #endif // USE_NEXTION_TFT_UPLOAD
811 
812  bool get_is_connected_() { return this->is_connected_; }
813 
814  bool check_connect_();
815 
816  std::vector<NextionComponentBase *> touch_;
817  std::vector<NextionComponentBase *> switchtype_;
818  std::vector<NextionComponentBase *> sensortype_;
819  std::vector<NextionComponentBase *> textsensortype_;
820  std::vector<NextionComponentBase *> binarysensortype_;
825 
827  float brightness_{1.0};
828 
829  std::string device_model_;
830  std::string firmware_version_;
831  std::string serial_number_;
832  std::string flash_size_;
833 
835 
836 #ifdef USE_NEXTION_TFT_UPLOAD
837  std::string tft_url_;
838  uint8_t *transfer_buffer_{nullptr};
841 #endif
842 
843 #ifdef NEXTION_PROTOCOL_LOG
844  void print_queue_members_();
845 #endif
846  void reset_(bool reset_nextion = true);
847 
848  std::string command_data_;
849  bool is_connected_ = false;
850  uint32_t startup_override_ms_ = 8000;
851  uint32_t max_q_age_ms_ = 8000;
852  uint32_t started_ms_ = 0;
853  bool sent_setup_commands_ = false;
854 };
855 } // namespace nextion
856 } // namespace esphome
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.
Definition: nextion.h:748
void all_components_send_state_(bool force_update=false)
Definition: nextion.cpp:838
CallbackManager< void(uint8_t)> page_callback_
Definition: nextion.h:824
CallbackManager< void()> sleep_callback_
Definition: nextion.h:822
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_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:155
void set_nextion_rtc_time(time::ESPTime time)
Send the current time to the nextion display.
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:147
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:177
void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override
Definition: nextion.cpp:1008
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
Definition: nextion.cpp:1084
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:850
void add_to_get_queue(NextionComponentBase *component) override
Definition: nextion.cpp:1056
std::vector< NextionComponentBase * > touch_
Definition: nextion.h:816
optional< nextion_writer_t > writer_
Definition: nextion.h:826
void filled_circle(int center_x, int center_y, int radius, const char *color)
Draw a filled circled.
bool void set_tft_url(const std::string &tft_url)
Set the tft file URL.
Definition: nextion.h:661
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:151
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:133
void set_wake_up_page_internal(uint8_t wake_up_page)
Definition: nextion.h:735
void setup() override
Definition: nextion.cpp:11
std::string serial_number_
Definition: nextion.h:831
void set_brightness(float brightness)
Definition: nextion.h:640
void register_binarysensor_component(NextionComponentBase *obj)
Definition: nextion.h:635
void register_touch_component(NextionComponentBase *obj)
Definition: nextion.h:633
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:783
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:1018
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.
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:636
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:981
void add_sleep_state_callback(std::function< void()> &&callback)
Add a callback to be notified of sleep state changes.
Definition: nextion.cpp:143
void register_switch_component(NextionComponentBase *obj)
Definition: nextion.h:634
std::string flash_size_
Definition: nextion.h:832
void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value)
Add waveform data to a waveform component.
A more user-friendly version of struct tm from time.h.
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:786
CallbackManager< void()> setup_callback_
Definition: nextion.h:821
void set_nextion_text_state(const std::string &name, const std::string &state)
Definition: nextion.cpp:827
void set_wait_for_ack(bool wait_for_ack)
void loop() override
Definition: nextion.cpp:218
void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout)
Definition: nextion.h:732
std::deque< NextionQueue * > nextion_queue_
Definition: nextion.h:739
CallbackManager< void()> wake_callback_
Definition: nextion.h:823
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:736
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:925
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:848
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:257
uint8_t * transfer_buffer_
Definition: nextion.h:838
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
Definition: nextion.h:829
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:955
void register_textsensor_component(NextionComponentBase *obj)
Definition: nextion.h:637
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
Definition: nextion.h:819
std::string tft_url_
Definition: nextion.h:837
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:818
void dump_config() override
Definition: nextion.cpp:116
std::vector< NextionComponentBase * > switchtype_
Definition: nextion.h:817
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:782
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
Definition: a4988.cpp:4
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
Definition: nextion.cpp:946
void reset_(bool reset_nextion=true)
Definition: nextion.cpp:107
void update_components_by_prefix(const std::string &prefix)
Definition: nextion.cpp:858
void update() override
Definition: nextion.cpp:134
std::string firmware_version_
Definition: nextion.h:830
uint32_t touch_sleep_timeout_
Definition: nextion.h:755
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
Definition: nextion.cpp:877
std::function< void(Nextion &)> nextion_writer_t
Definition: nextion.h:32
std::vector< NextionComponentBase * > binarysensortype_
Definition: nextion.h:820
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:794
void set_writer(const nextion_writer_t &writer)
Definition: nextion.cpp:1105
void set_component_font_color(const char *component, uint32_t color)
Set the font color of a component.