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
@@ -19,7 +19,6 @@ float sht31_get_humidity(void);
|
|||||||
float sht31_get_temperature(void);
|
float sht31_get_temperature(void);
|
||||||
|
|
||||||
esp_err_t sht31_read_temp_hum(float *temperature, float *humidity);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
void sensor_tester(void)
|
||||||
{
|
{
|
||||||
ESP_ERROR_CHECK(sht31_init());
|
ESP_ERROR_CHECK(sht31_init());
|
||||||
|
|
||||||
//float temperature, humidity;
|
//float temperature, humidity;
|
||||||
int temperature, humidity;
|
float temperature, humidity, temp_dec_part, hum_dec_part;
|
||||||
|
int temp_int_part, hum_int_part;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
//if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) {
|
if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) {
|
||||||
if (sht31_read_temp_hum_int(&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: %.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 {
|
} else {
|
||||||
ESP_LOGE(TAG, "Failed to read from SHT31 sensor");
|
ESP_LOGE(TAG, "Failed to read from SHT31 sensor");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,46 +100,3 @@ esp_err_t sht31_read_temp_hum(float *temperature, float *humidity)
|
|||||||
return ESP_OK;
|
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;
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user