ESPHome  2023.8.3
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 hide_component(const char *component) override;
360  void show_component(const char *component) override;
372  void enable_component_touch(const char *component);
384  void disable_component_touch(const char *component);
391  void add_waveform_data(int component_id, uint8_t channel_number, uint8_t value);
392  void open_waveform_channel(int component_id, uint8_t channel_number, uint8_t value);
406  void display_picture(int picture_id, int x_start, int y_start);
424  void fill_area(int x1, int y1, int width, int height, const char *color);
442  void fill_area(int x1, int y1, int width, int height, Color color);
461  void line(int x1, int y1, int x2, int y2, const char *color);
480  void line(int x1, int y1, int x2, int y2, Color color);
499  void rectangle(int x1, int y1, int width, int height, const char *color);
518  void rectangle(int x1, int y1, int width, int height, Color color);
526  void circle(int center_x, int center_y, int radius, const char *color);
534  void circle(int center_x, int center_y, int radius, Color color);
551  void filled_circle(int center_x, int center_y, int radius, const char *color);
568  void filled_circle(int center_x, int center_y, int radius, Color color);
569 
581  void set_backlight_brightness(float brightness);
594  void set_touch_sleep_timeout(uint16_t timeout);
607  void set_wake_up_page(uint8_t page_id = 255);
620  void set_auto_wake_on_touch(bool auto_wake);
625  void sleep(bool sleep);
626 
627  // ========== INTERNAL METHODS ==========
628  // (In most use cases you won't need these)
629  void register_touch_component(NextionComponentBase *obj) { this->touch_.push_back(obj); }
630  void register_switch_component(NextionComponentBase *obj) { this->switchtype_.push_back(obj); }
632  void register_sensor_component(NextionComponentBase *obj) { this->sensortype_.push_back(obj); }
634 
635  void setup() override;
636  void set_brightness(float brightness) { this->brightness_ = brightness; }
637  float get_setup_priority() const override;
638  void update() override;
639  void loop() override;
640  void set_writer(const nextion_writer_t &writer);
641 
642  // This function has been deprecated
643  void set_wait_for_ack(bool wait_for_ack);
644 
651  bool send_command_printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
652 
653 #ifdef USE_NEXTION_TFT_UPLOAD
654 
657  void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
658 
659 #endif
660 
664  void upload_tft();
665  void dump_config() override;
666 
670  void soft_reset();
671 
676  void add_sleep_state_callback(std::function<void()> &&callback);
677 
682  void add_wake_state_callback(std::function<void()> &&callback);
683 
688  void add_setup_state_callback(std::function<void()> &&callback);
689 
694  void add_new_page_callback(std::function<void(uint8_t)> &&callback);
695 
696  void update_all_components();
697 
710  void set_nextion_sensor_state(int queue_type, const std::string &name, float state);
711  void set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state);
712  void set_nextion_text_state(const std::string &name, const std::string &state);
713 
714  void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value) override;
715  void add_no_result_to_queue_with_set(const std::string &variable_name, const std::string &variable_name_to_send,
716  int state_value) override;
717 
718  void add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &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  const std::string &state_value) override;
721 
722  void add_to_get_queue(NextionComponentBase *component) override;
723 
724  void add_addt_command_to_queue(NextionComponentBase *component) override;
725 
726  void update_components_by_prefix(const std::string &prefix);
727 
728  void set_touch_sleep_timeout_internal(uint32_t touch_sleep_timeout) {
729  this->touch_sleep_timeout_ = touch_sleep_timeout;
730  }
731  void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
732  void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
733 
734  protected:
735  std::deque<NextionQueue *> nextion_queue_;
736  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
737  void all_components_send_state_(bool force_update = false);
738  uint64_t comok_sent_ = 0;
739  bool remove_from_q_(bool report_empty = true);
744  bool ignore_is_setup_ = false;
746  uint8_t nextion_event_;
747 
749  void process_serial_();
750  bool is_updating_ = false;
751  uint32_t touch_sleep_timeout_ = 0;
752  int wake_up_page_ = -1;
753  bool auto_wake_on_touch_ = true;
754 
759  bool send_command_(const std::string &command);
760  void add_no_result_to_queue_(const std::string &variable_name);
761  bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...)
762  __attribute__((format(printf, 3, 4)));
763  void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command);
764 
765  bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...)
766  __attribute__((format(printf, 3, 4)));
767 
768  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
769  const std::string &variable_name_to_send, int state_value,
770  bool is_sleep_safe = false);
771 
772  void add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
773  const std::string &variable_name_to_send,
774  const std::string &state_value, bool is_sleep_safe = false);
775 
776 #ifdef USE_NEXTION_TFT_UPLOAD
777 #ifdef USE_ESP8266
778  WiFiClient *wifi_client_{nullptr};
779  BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
780  WiFiClient *get_wifi_client_();
781 #endif
782 
791  int tft_size_ = 0;
792  int upload_by_chunks_(HTTPClient *http, int range_start);
793 
794  bool upload_with_range_(uint32_t range_start, uint32_t range_end);
795 
803  bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
804  void upload_end_();
805 
806 #endif // USE_NEXTION_TFT_UPLOAD
807 
808  bool get_is_connected_() { return this->is_connected_; }
809 
810  bool check_connect_();
811 
812  std::vector<NextionComponentBase *> touch_;
813  std::vector<NextionComponentBase *> switchtype_;
814  std::vector<NextionComponentBase *> sensortype_;
815  std::vector<NextionComponentBase *> textsensortype_;
816  std::vector<NextionComponentBase *> binarysensortype_;
821 
823  float brightness_{1.0};
824 
825  std::string device_model_;
826  std::string firmware_version_;
827  std::string serial_number_;
828  std::string flash_size_;
829 
831 
832 #ifdef USE_NEXTION_TFT_UPLOAD
833  std::string tft_url_;
834  uint8_t *transfer_buffer_{nullptr};
837 #endif
838 
839 #ifdef NEXTION_PROTOCOL_LOG
840  void print_queue_members_();
841 #endif
842  void reset_(bool reset_nextion = true);
843 
844  std::string command_data_;
845  bool is_connected_ = false;
846  uint32_t startup_override_ms_ = 8000;
847  uint32_t max_q_age_ms_ = 8000;
848  uint32_t started_ms_ = 0;
849  bool sent_setup_commands_ = false;
850 };
851 } // namespace nextion
852 } // 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:744
void all_components_send_state_(bool force_update=false)
Definition: nextion.cpp:838
CallbackManager< void(uint8_t)> page_callback_
Definition: nextion.h:820
CallbackManager< void()> sleep_callback_
Definition: nextion.h:818
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 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:846
void add_to_get_queue(NextionComponentBase *component) override
Definition: nextion.cpp:1056
std::vector< NextionComponentBase * > touch_
Definition: nextion.h:812
optional< nextion_writer_t > writer_
Definition: nextion.h:822
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:657
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: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:731
void setup() override
Definition: nextion.cpp:11
std::string serial_number_
Definition: nextion.h:827
void set_brightness(float brightness)
Definition: nextion.h:636
void register_binarysensor_component(NextionComponentBase *obj)
Definition: nextion.h:631
void register_touch_component(NextionComponentBase *obj)
Definition: nextion.h:629
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:779
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.
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:632
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:630
std::string flash_size_
Definition: nextion.h:828
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:786
CallbackManager< void()> setup_callback_
Definition: nextion.h:817
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:728
std::deque< NextionQueue * > nextion_queue_
Definition: nextion.h:735
CallbackManager< void()> wake_callback_
Definition: nextion.h:819
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:732
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:844
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:834
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
std::string device_model_
Definition: nextion.h:825
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:633
void set_touch_sleep_timeout(uint16_t timeout)
Set the touch sleep timeout of the display.
std::vector< NextionComponentBase * > textsensortype_
Definition: nextion.h:815
std::string tft_url_
Definition: nextion.h:833
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:814
void dump_config() override
Definition: nextion.cpp:116
std::vector< NextionComponentBase * > switchtype_
Definition: nextion.h:813
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:778
void set_auto_wake_on_touch(bool auto_wake)
Sets if Nextion should auto-wake from sleep when touch press occurs.
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:826
uint32_t touch_sleep_timeout_
Definition: nextion.h:751
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:30
std::vector< NextionComponentBase * > binarysensortype_
Definition: nextion.h:816
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:790
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.