ESPHome  2022.11.3
nextion.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <deque>
4 #include "esphome/core/defines.h"
6 #include "nextion_base.h"
7 #include "nextion_component.h"
9 
10 #ifdef USE_NEXTION_TFT_UPLOAD
11 #ifdef USE_ESP32
12 #include <HTTPClient.h>
13 #endif
14 #ifdef USE_ESP8266
15 #include <ESP8266HTTPClient.h>
16 #include <WiFiClientSecure.h>
17 #endif
18 #endif
19 
20 #ifdef USE_TIME
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;
319 #ifdef USE_TIME
320 
325 #endif
326 
338  void goto_page(const char *page);
350  void hide_component(const char *component) override;
362  void show_component(const char *component) override;
374  void enable_component_touch(const char *component);
386  void disable_component_touch(const char *component);
393  void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value);
394  void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value);
408  void display_picture(int picture_id, int x_start, int y_start);
426  void fill_area(int x1, int y1, int width, int height, const char *color);
444  void fill_area(int x1, int y1, int width, int height, Color color);
463  void line(int x1, int y1, int x2, int y2, const char *color);
482  void line(int x1, int y1, int x2, int y2, Color color);
501  void rectangle(int x1, int y1, int width, int height, const char *color);
520  void rectangle(int x1, int y1, int width, int height, Color color);
528  void circle(int center_x, int center_y, int radius, const char *color);
536  void circle(int center_x, int center_y, int radius, Color color);
553  void filled_circle(int center_x, int center_y, int radius, const char *color);
570  void filled_circle(int center_x, int center_y, int radius, Color color);
571 
583  void set_backlight_brightness(float brightness);
596  void set_touch_sleep_timeout(uint16_t timeout);
609  void set_wake_up_page(uint8_t page_id = 255);
622  void set_auto_wake_on_touch(bool auto_wake);
627  void sleep(bool sleep);
628 
629  // ========== INTERNAL METHODS ==========
630  // (In most use cases you won't need these)
631  void register_touch_component(NextionComponentBase *obj) { this->touch_.push_back(obj); }
632  void register_switch_component(NextionComponentBase *obj) { this->switchtype_.push_back(obj); }
634  void register_sensor_component(NextionComponentBase *obj) { this->sensortype_.push_back(obj); }
636 
637  void setup() override;
638  void set_brightness(float brightness) { this->brightness_ = brightness; }
639  float get_setup_priority() const override;
640  void update() override;
641  void loop() override;
642  void set_writer(const nextion_writer_t &writer);
643 
644  // This function has been deprecated
645  void set_wait_for_ack(bool wait_for_ack);
646 
653  bool send_command_printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
654 
655 #ifdef USE_NEXTION_TFT_UPLOAD
656 
659  void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
660 
661 #endif
662 
666  void upload_tft();
667  void dump_config() override;
668 
672  void soft_reset();
673 
678  void add_sleep_state_callback(std::function<void()> &&callback);
679 
684  void add_wake_state_callback(std::function<void()> &&callback);
685 
690  void add_setup_state_callback(std::function<void()> &&callback);
691 
696  void add_new_page_callback(std::function<void(uint8_t)> &&callback);
697 
698  void update_all_components();
699 
712  void set_nextion_sensor_state(int queue_type, const std::string &name, float state);
713  void set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state);
714  void set_nextion_text_state(const std::string &name, const std::string &state);
715 
716  void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override;
717  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
718  int state_value) override;
719 
720  void add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &state_value) override;
721  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
722  const std::string &state_value) override;
723 
724  void add_to_get_queue(NextionComponentBase *component) override;
725 
726  void add_addt_command_to_queue(NextionComponentBase *component) override;
727 
728  void update_components_by_prefix(const std::string &prefix);
729 
730  void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout) {
731  this->touch_sleep_timeout_ = touch_sleep_timeout;
732  }
733  void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
734  void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
735 
736  protected:
737  std::deque<NextionQueue *> nextion_queue_;
738  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
739  void all_components_send_state_(bool force_update = false);
740  uint64_t comok_sent_ = 0;
741  bool remove_from_q_(bool report_empty = true);
746  bool ignore_is_setup_ = false;
748  uint8_t nextion_event_;
749 
751  void process_serial_();
752  bool is_updating_ = false;
753  uint32_t touch_sleep_timeout_ = 0;
754  int wake_up_page_ = -1;
755  bool auto_wake_on_touch_ = true;
756 
761  bool send_command_(const std::string &command);
762  void add_no_result_to_queue_(const std::string &variable_name);
763  bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...)
764  __attribute__((format(printf, 3, 4)));
765  void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command);
766 
767  bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...)
768  __attribute__((format(printf, 3, 4)));
769 
770  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
771  const std::string &variable_name_to_send, int state_value,
772  bool is_sleep_safe = false);
773 
774  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
775  const std::string &variable_name_to_send,
776  const std::string &state_value, bool is_sleep_safe = false);
777 
778 #ifdef USE_NEXTION_TFT_UPLOAD
779 #ifdef USE_ESP8266
780  WiFiClient *wifi_client_{nullptr};
781  BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
782  WiFiClient *get_wifi_client_();
783 #endif
784 
793  int tft_size_ = 0;
794  int upload_by_chunks_(HTTPClient *http, int range_start);
795 
796  bool upload_with_range_(uint32_t range_start, uint32_t range_end);
797 
805  bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
806  void upload_end_();
807 
808 #endif // USE_NEXTION_TFT_UPLOAD
809 
810  bool get_is_connected_() { return this->is_connected_; }
811 
812  bool check_connect_();
813 
814  std::vector<NextionComponentBase *> touch_;
815  std::vector<NextionComponentBase *> switchtype_;
816  std::vector<NextionComponentBase *> sensortype_;
817  std::vector<NextionComponentBase *> textsensortype_;
818  std::vector<NextionComponentBase *> binarysensortype_;
823 
825  float brightness_{1.0};
826 
827  std::string device_model_;
828  std::string firmware_version_;
829  std::string serial_number_;
830  std::string flash_size_;
831 
833 
834 #ifdef USE_NEXTION_TFT_UPLOAD
835  std::string tft_url_;
836  uint8_t *transfer_buffer_{nullptr};
839 #endif
840 
841 #ifdef NEXTION_PROTOCOL_LOG
842  void print_queue_members_();
843 #endif
844  void reset_(bool reset_nextion = true);
845 
846  std::string command_data_;
847  bool is_connected_ = false;
848  uint32_t startup_override_ms_ = 8000;
849  uint32_t max_q_age_ms_ = 8000;
850  uint32_t started_ms_ = 0;
851  bool sent_setup_commands_ = false;
852 };
853 } // namespace nextion
854 } // 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:746
void all_components_send_state_(bool force_update=false)
Definition: nextion.cpp:837
CallbackManager< void(uint8_t)> page_callback_
Definition: nextion.h:822
CallbackManager< void()> sleep_callback_
Definition: nextion.h:820
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:1007
void add_addt_command_to_queue(NextionComponentBase *component) override
Add addt command to the queue.
Definition: nextion.cpp:1083
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:848
void add_to_get_queue(NextionComponentBase *component) override
Definition: nextion.cpp:1055
std::vector< NextionComponentBase * > touch_
Definition: nextion.h:814
optional< nextion_writer_t > writer_
Definition: nextion.h:824
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:659
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:733
void setup() override
Definition: nextion.cpp:11
std::string serial_number_
Definition: nextion.h:829
void set_brightness(float brightness)
Definition: nextion.h:638
void register_binarysensor_component(NextionComponentBase *obj)
Definition: nextion.h:633
void register_touch_component(NextionComponentBase *obj)
Definition: nextion.h:631
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:267
BearSSL::WiFiClientSecure * wifi_client_secure_
Definition: nextion.h:781
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:1017
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:634
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:980
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:632
std::string flash_size_
Definition: nextion.h:830
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:785
CallbackManager< void()> setup_callback_
Definition: nextion.h:819
void set_nextion_text_state(const std::string &name, const std::string &state)
Definition: nextion.cpp:826
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:730
std::deque< NextionQueue * > nextion_queue_
Definition: nextion.h:737
CallbackManager< void()> wake_callback_
Definition: nextion.h:821
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:734
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:924
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:846
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:836
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
Definition: nextion.h:827
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:954
void register_textsensor_component(NextionComponentBase *obj)
Definition: nextion.h:635
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
Definition: nextion.h:817
std::string tft_url_
Definition: nextion.h:835
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:816
void dump_config() override
Definition: nextion.cpp:116
std::vector< NextionComponentBase * > switchtype_
Definition: nextion.h:815
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:780
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:945
void reset_(bool reset_nextion=true)
Definition: nextion.cpp:107
void update_components_by_prefix(const std::string &prefix)
Definition: nextion.cpp:857
void update() override
Definition: nextion.cpp:134
std::string firmware_version_
Definition: nextion.h:828
uint32_t touch_sleep_timeout_
Definition: nextion.h:753
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
Definition: nextion.cpp:876
std::function< void(Nextion &)> nextion_writer_t
Definition: nextion.h:30
std::vector< NextionComponentBase * > binarysensortype_
Definition: nextion.h:818
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:792
void set_writer(const nextion_writer_t &writer)
Definition: nextion.cpp:1104
void set_component_font_color(const char *component, uint32_t color)
Set the font color of a component.