Displaying sensor readings in a table or label object

Description Displaying sensor readings in a table or label object.

What MCU/Processor/Board and compiler are you using? ESP32 Wroom Devkit C and Arduino IDE

What do you want to achieve?

I am reading temperature, weight and humidity from sensors and save the read values in float variables.
I would like to display the values of these variables in a label or better in a table object.
But as far as I understand i just can use char variables for the set cell value function :

This is from the example

lv_table_set_cell_value(table, 2, 0, “Banana”);

I hope that this question is not to noobish but I just have started ESP32 programming a month ago and don’t know how solve this.
I would be really happ if somebody can push me to the right direction.

What have you tried so far?

I managed to to define a char variable for testing purposes and the value of this variable is shown on the display. But a char is not enough so i would like to show the value of an float variable.
But i can’t find out how to make littlevGL to use a float ( or a float converted to a string) for a table or a label object.

Code to reproduce

This is a slightly modified version of the example from the documentation for a table. It reads the value of the test char variable and shows it on my screen.
It works and shows me the value of the variable “test” on my display.

/* const char test = '9' ;
/*Fill the first column*/
    lv_table_set_cell_value(table, 0, 0, "Name");
    lv_table_set_cell_value(table, 1, 0, &test );
    lv_table_set_cell_value(table, 2, 0, "Banana");
    lv_table_set_cell_value(table, 3, 0, "Citron");

    
*/

Code to reproduce

This is version where I am trying to convert a float into a string and make LittlevGL to show me the value of the float/string in a table

  float test = 12.4;
  String test2;
  test2 = String(test);

/*Fill the first column*/
    lv_table_set_cell_value(table, 0, 0, "Name");
    lv_table_set_cell_value(table, 1, 0, &test2 );
    lv_table_set_cell_value(table, 2, 0, "Banana");
    lv_table_set_cell_value(table, 3, 0, "Citron");

    
*/

But this leads to a compiler error :

cannot convert ‘String*’ to ‘const char*’

Because ‘String’ isnt a C datatype.

You want something like this:

  #define MAX_STRING_SIZE 10
  float test = 12.4;
  char test2[MAX_STRING_SIZE];
  sprintf(test2, "%f", test);

/*Fill the first column*/
    lv_table_set_cell_value(table, 0, 0, "Name");
    lv_table_set_cell_value(table, 1, 0, test2 );
    lv_table_set_cell_value(table, 2, 0, "Banana");
    lv_table_set_cell_value(table, 3, 0, "Citron");
1 Like

This is working like a charm.
I hope my question wasn’t to stupid but i would like to thank you very much.
This helped me alot and i will read into sprintf to learn something.
Thank you very much for your fast reply.

I would recommend to both of you to use snprintf instead. sprintf makes it easy to overflow the buffer without realizing it (and that leads to subtle bugs).

  #define MAX_STRING_SIZE 10
  float test = 12.4;
  char test2[MAX_STRING_SIZE];
  snprintf(test2, MAX_STRING_SIZE, "%f", test);
3 Likes

This does not update the value from the sensor data.

For that you could use a lv_task. From there you update the test2 variable. When lvgl updates the display it also shows the updated value.

You need to do more than just change the test2 variable.

  • When you create the label you should call lv_label_set_static_text(label, test2).
  • When you update it you also have to calll lv_label_set_static_text(label, NULL) to trigger a redrawing of the label. Otherwise LittlevGL has no way of knowing that the string changed.

from my initGui i create a task.

lv_task_create(&update_ui, 300, 3, nullptr);

Then in the update_ui I update the label and my bar from the sensor.

void update_ui(lv_task_t* task) {
	Device* d = Device::get_device();
	int perc = d->get_bat_perc();
	char char_buff[10];
	snprintf(char_buff, 10, "%d%", perc);
	lv_label_set_text(label_battery_percent, char_buff);
	lv_bar_set_value(bar_battery_percent, perc, true);
}

is this the correct approuch?

Yes, that’s fine.