Fix float output formatting in sensor readings

- Replace printf with printk using integer formatting for floats
- Add stdio.h include for proper printf support
- Fix SHT4X sensor value display issues
- Temperature and humidity values now display correctly
This commit is contained in:
xlemmingx 2025-10-07 18:53:38 +02:00
parent c46a47e125
commit 7a6f7c46d0
2 changed files with 8 additions and 5 deletions

View File

@ -5,6 +5,7 @@
#include <zephyr/sys/printk.h> #include <zephyr/sys/printk.h>
#include <zephyr/drivers/sensor/sht4x.h> #include <zephyr/drivers/sensor/sht4x.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
#include <stdio.h>
#include <lora/lorawan.h> #include <lora/lorawan.h>
#include <sensors/mlx90614.h> #include <sensors/mlx90614.h>
@ -39,11 +40,11 @@ int main(void)
request_sensor_data_sht4x(); request_sensor_data_sht4x();
humidity = get_value_from_current_sample_sht4x(SENSOR_CHAN_HUMIDITY); humidity = get_value_from_current_sample_sht4x(SENSOR_CHAN_HUMIDITY);
temp = get_value_from_current_sample_sht4x(SENSOR_CHAN_AMBIENT_TEMP); temp = get_value_from_current_sample_sht4x(SENSOR_CHAN_AMBIENT_TEMP);
printk("hum: %.2f%%\n", humidity); printk("hum: %d.%02d%%\n", (int)humidity, (int)(humidity * 100) % 100);
printk("temp: %.2f °C\n", temp); printk("temp: %d.%02d °C\n", (int)temp, (int)(temp * 100) % 100);
request_sensor_data_mlx90614(MLX90614_IR_TEMP_ADDR); request_sensor_data_mlx90614(MLX90614_IR_TEMP_ADDR);
floor_temp = get_temp_from_raw_data_mlx90614(); floor_temp = get_temp_from_raw_data_mlx90614();
printk("floor_temp: %.2f °C\n", floor_temp); printk("floor_temp: %d.%02d °C\n", (int)floor_temp, (int)(floor_temp * 100) % 100);
/* Set relais based on temperature if not manually overridden */ /* Set relais based on temperature if not manually overridden */
if (!relais_manual_override) { if (!relais_manual_override) {

View File

@ -48,9 +48,11 @@ float get_value_from_current_sample_sht4x(enum sensor_channel sensor)
float result; float result;
if (sensor_channel_get(sht, sensor, &value) == 0) { if (sensor_channel_get(sht, sensor, &value) == 0) {
result = sensor_value_to_double(&value); result = sensor_value_to_double(&value);
printf("Value '%s' successfully read [%f]", sensor, result); const char* sensor_name = (sensor == SENSOR_CHAN_AMBIENT_TEMP) ? "temperature" : "humidity";
LOG_DBG("SHT4X %s value: %.2f", sensor_name, result);
} else { } else {
LOG_ERR("Can't read sensor value '%s' from sample!", sensor); const char* sensor_name = (sensor == SENSOR_CHAN_AMBIENT_TEMP) ? "temperature" : "humidity";
LOG_ERR("Can't read SHT4X %s from sample!", sensor_name);
return 0.0; return 0.0;
} }
return result; return result;