From 1341a1000acaddd05f44c24779a52e706b032775 Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Sun, 11 Aug 2024 11:20:15 +0200 Subject: [PATCH] modified: main/include/sht31.h modified: main/src/main.c modified: main/src/sht31.c Fix floating point issue with Sensor readouts --- firmware/main/include/sht31.h | 1 - firmware/main/src/main.c | 19 ++++++++++++---- firmware/main/src/sht31.c | 43 ----------------------------------- 3 files changed, 15 insertions(+), 48 deletions(-) diff --git a/firmware/main/include/sht31.h b/firmware/main/include/sht31.h index 201597c..230d783 100644 --- a/firmware/main/include/sht31.h +++ b/firmware/main/include/sht31.h @@ -19,7 +19,6 @@ float sht31_get_humidity(void); float sht31_get_temperature(void); esp_err_t sht31_read_temp_hum(float *temperature, float *humidity); -esp_err_t sht31_read_temp_hum_int(int *temperature, int *humidity); #ifdef __cplusplus } diff --git a/firmware/main/src/main.c b/firmware/main/src/main.c index 1646907..9420f05 100644 --- a/firmware/main/src/main.c +++ b/firmware/main/src/main.c @@ -36,18 +36,29 @@ static void wifi_init(void) { } */ +static void split_float(float num, int *int_part, float *dec_part) { + // Extract the integer part + *int_part = (float)((int)num); + + // Calculate the fractional part by subtracting the integer part from the original number + *dec_part = num - *int_part; +} + + void sensor_tester(void) { ESP_ERROR_CHECK(sht31_init()); //float temperature, humidity; - int temperature, humidity; + float temperature, humidity, temp_dec_part, hum_dec_part; + int temp_int_part, hum_int_part; while (1) { - //if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) { - if (sht31_read_temp_hum_int(&temperature, &humidity) == ESP_OK) { + if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) { + split_float(temperature, &temp_int_part, &temp_dec_part); + split_float(humidity, &hum_int_part, &hum_dec_part); //ESP_LOGI(TAG, "Temperature: %.2f°C, Humidity: %.2f%%", temperature, humidity); - ESP_LOGI(TAG, "Temperature: %i°C, Humidity: %i%%", temperature, humidity); + ESP_LOGI(TAG, "Temperature: %i.%i°C, Humidity: %i.%i%%", temp_int_part, (int)(temp_dec_part*1000), hum_int_part, (int)(hum_dec_part*1000)); } else { ESP_LOGE(TAG, "Failed to read from SHT31 sensor"); } diff --git a/firmware/main/src/sht31.c b/firmware/main/src/sht31.c index b6cd0fb..229ec34 100644 --- a/firmware/main/src/sht31.c +++ b/firmware/main/src/sht31.c @@ -100,46 +100,3 @@ esp_err_t sht31_read_temp_hum(float *temperature, float *humidity) return ESP_OK; } -esp_err_t sht31_read_temp_hum_int(int *temperature, int *humidity) -{ - uint8_t data[6]; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - - // Send the measurement command - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (SENSOR_ADDR << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(cmd, SHT31_MEASURE_CMD_HIGHREP >> 8, true); - i2c_master_write_byte(cmd, SHT31_MEASURE_CMD_HIGHREP & 0xFF, true); - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to send command to SHT31"); - return ret; - } - - vTaskDelay(500 / portTICK_RATE_MS); - - // Request 6 bytes of data from the sensor - cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (SENSOR_ADDR << 1) | I2C_MASTER_READ, true); - i2c_master_read(cmd, data, sizeof(data) - 1, I2C_MASTER_ACK); - i2c_master_read_byte(cmd, data + sizeof(data) - 1, I2C_MASTER_NACK); - i2c_master_stop(cmd); - ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to read data from SHT31"); - return ret; - } - - // Process the data - uint16_t t = (data[0] << 8) | data[1]; - uint16_t h = (data[3] << 8) | data[4]; - - *temperature = -45 + 175 * ((float)t / 65535.0); - *humidity = 100 * ((float)h / 65535.0); - - return ESP_OK; -}