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.
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
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
}
}
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