modified: main/include/sht31.h
modified: main/src/main.c modified: main/src/sht31.c Fix floating point issue with Sensor readouts
This commit is contained in:
1 parent
34e0561f46
commit
1341a1000a
3 files changed
+15
-48
No files matched your search
@@ -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");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user