Hi i create button and I want the button color to change when I click on the button but esp32 reboots when I click the button on the screen
What MCU/Processor/Board and compiler are you using?
2.8inch_ESP32-2432S028R
What LVGL version are you using?
8.3.9
What do you want to achieve?
What have you tried so far?
Code to reproduce
Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.
The code block(s) should be formatted like:
static void btn_event_cb(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED) {
client.subscribe(mqtt_topic);
client.setCallback(callback);
if (buttonState) {
client.publish(mqtt_topic, "0");
static lv_style_t style_btn_red2;
lv_style_init(&style_btn_red2);
lv_style_set_bg_color(&style_btn_red2, lv_color_make(0, 255, 0)); // BLUE RED GREEN
lv_style_set_bg_opa(&style_btn_red2, LV_OPA_COVER);
lv_obj_add_style(btn, &style_btn_red2, 0);
} else {
client.publish(mqtt_topic, "1");
}
}
}
Screenshot and/or video
If possible, add screenshots and/or videos about the current state.
Okay, so in the code you posted the btn was never actually created, right? So you should get the pointer to the button that the event triggered, and this is done with the line I posted before.
static void btn_event_cb(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED) {
client.subscribe(mqtt_topic);
client.setCallback(callback);
if (buttonState) {
lv_obj_t *btn = lv_event_get_target(e); //<-- add this line here
client.publish(mqtt_topic, "0");
static lv_style_t style_btn_red2;
lv_style_init(&style_btn_red2);
lv_style_set_bg_color(&style_btn_red2, lv_color_make(0, 255, 0)); // BLUE RED GREEN
lv_style_set_bg_opa(&style_btn_red2, LV_OPA_COVER);
lv_obj_add_style(btn, &style_btn_red2, 0);
} else {
client.publish(mqtt_topic, "1");
}
}
}
Unless the button btn in line lv_obj_add_style(btn, &style_btn_red2, 0); is actually static