• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32CYD LVGL How to pas a value fom code to button for logle on off.

Q&A Forum › Category: ESP32 › ESP32CYD LVGL How to pas a value fom code to button for logle on off.
0 Vote Up Vote Down
Risto Vesanto asked 4 months ago

Hi! I am using Firebase code to controll electric heating on and off over the net. I woud like to utilice the ESP32CYD-board for this purpose. The board accepts all the quite lenghtly code and is working over the net. At the actual positoin CYD-board will be used to controll heating on and off by the button fuction. The question is when the controllig is taken plase ower the net it should also change the button in the CYD accordinly. How to pass the value to the button to change.

Question Tags: CYD, Firebase, LVGL
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

Hi.
At the moment, we don’t have any example like that.
But, a solution would be to save the current button state on the data base whenever it is changed.
You can add a database change listener on both the Firebase application code and on the ESP32 code to detect when a change happens in the database and update both places when there’s a change so that everything is synchronized.
 
Regards,
Sara

0 Vote Up Vote Down
Risto Vesanto answered 4 months ago

Thank you Sara, this works already like you suggested, but the point is how to get the button change from state to other via code in the main program.
Below is the button code

// Callback that is triggered when button is clicked/toggled locally.
//Getting the button to togle state via main code from outside this code is open.
static void button_event_handler(lv_event_t * e) {
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t * button = (lv_obj_t*) lv_event_get_target(e);
  lv_obj_t * button_label = (lv_obj_t*) lv_event_get_user_data(e);
  if(code == LV_EVENT_VALUE_CHANGED) {
    LV_UNUSED(button);
    LV_LOG_USER(“State: %s”, lv_obj_has_state(button, LV_STATE_CHECKED) ? “On” : “Off”);
    lv_label_set_text_fmt(button_label, “Turn %s”, lv_obj_has_state(button, LV_STATE_CHECKED) ? “Off” : “On”);
    // Cheap Yellow Display built-in RGB LED is controlled with inverted logic
    digitalWrite(CYD_LED_RED, !lv_obj_has_state(button, LV_STATE_CHECKED));
    // If you are using a regular LED, comment previous line and uncomment the next line
    digitalWrite(CYD_ON_OFF_Pin, lv_obj_has_state(button, LV_STATE_CHECKED)); //this puts the heater on and off
    sendInt(TilaPath, lv_obj_has_state(button, LV_STATE_CHECKED)); // informs Firebase is the heater on or off is sendt also to Thigspeak field
  }
}

0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

Hi.
You can use the following function on your button:
https://docs.lvgl.io/master/API/core/lv_obj.html#_CPPv416lv_obj_add_stateP8lv_obj_t10lv_state_t
https://docs.lvgl.io/master/details/base-widget/obj.html
 
For example:

lv_obj_add_state(button, LV_STATE_CHECKED);

or 

lv_obj_remove_state(button, LV_STATE_CHECKED);

 
Just make sure to declare your button as a global LVGL object so that it can be accessed throughout all code.
 
I hope this helps.
 
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.