renamed: main/include/sensor.h -> main/include/sht31.h
modified: main/src/main.c deleted: main/src/sensor.c new file: main/src/sht31.c modified: sdkconfig
This commit is contained in:
1 parent
2c62e70390
commit
34e0561f46
5 files changed
+182
-60
No files matched your search
@@ -0,0 +1,145 @@
|
||||
#include "sht31.h"
|
||||
#include <stdio.h>
|
||||
#include <driver/i2c.h>
|
||||
//#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
#define SDA_PIN GPIO_NUM_2
|
||||
#define CLK_PIN GPIO_NUM_14
|
||||
#define SENSOR_ADDR 0x45 //default address is 0x44 (pin 2 logic low). We chose 0x45 with logic high in v0.9 and 0x44 with newer versions where second sensor is removed.
|
||||
#define SHT31_MEASURE_CMD_HIGHREP 0x2400
|
||||
|
||||
static const char * TAG = "sht31.c";
|
||||
|
||||
// Dummy implementations for the purpose of this example
|
||||
// Replace these with actual sensor initialization and data retrieval logic
|
||||
|
||||
static const i2c_config_t config = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = GPIO_NUM_2,
|
||||
.scl_io_num = GPIO_NUM_14,
|
||||
.sda_pullup_en = GPIO_PULLUP_DISABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_DISABLE,
|
||||
.clk_stretch_tick = 5
|
||||
};
|
||||
|
||||
// Function to initialize the sensor
|
||||
esp_err_t sht31_init(void) {
|
||||
esp_err_t install_rc = i2c_driver_install(I2C_NUM_0, config.mode);
|
||||
esp_err_t config_rc = i2c_param_config(I2C_NUM_0, &config);
|
||||
if( install_rc != ESP_OK || config_rc != ESP_OK ) {
|
||||
printf("Sensor initialization failed!\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
printf("Sensor initialized.\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Function to get the current humidity level
|
||||
// Returns the humidity as a percentage (0-100)
|
||||
float sht31_get_humidity(void) {
|
||||
// Read humidity data from the sensor
|
||||
// This is a dummy value for the purpose of this example
|
||||
float humidity = 50.0; // Replace with actual sensor reading
|
||||
printf("Humidity read: %.2f%%\n", humidity);
|
||||
return humidity;
|
||||
}
|
||||
|
||||
// Function to get the current temperature
|
||||
// Returns the temperature in degrees Celsius
|
||||
float sht31_get_temperature(void) {
|
||||
// Read temperature data from the sensor
|
||||
// This is a dummy value for the purpose of this example
|
||||
float temperature = 22.0; // Replace with actual sensor reading
|
||||
printf("Temperature read: %.2f°C\n", temperature);
|
||||
return temperature;
|
||||
}
|
||||
|
||||
esp_err_t sht31_read_temp_hum(float *temperature, float *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;
|
||||
}
|
||||
|
||||
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