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:
Sven Luethi committed 2024-08-11 11:20:15 +02:00
1 parent 34e0561f46
commit 1341a1000a
3 files changed
+15 -48

No files matched your search

-43
View File
@@ -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;
}