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:
Sven Luethi committed 2024-08-10 12:27:57 +02:00
1 parent 2c62e70390
commit 34e0561f46
5 files changed
+182 -60

No files matched your search

@@ -1,20 +1,25 @@
#ifndef SENSOR_H #ifndef SHT31_H
#define SENSOR_H #define SHT31_H
#include <esp_err.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// Function to initialize the sensor // Function to initialize the sensor
void initSensor(void); esp_err_t sht31_init(void);
// Function to get the current humidity level // Function to get the current humidity level
// Returns the humidity as a percentage (0-100) // Returns the humidity as a percentage (0-100)
float getHumidity(void); float sht31_get_humidity(void);
// Function to get the current temperature // Function to get the current temperature
// Returns the temperature in degrees Celsius // Returns the temperature in degrees Celsius
float getTemperature(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 #ifdef __cplusplus
} }
+24 -2
View File
@@ -12,11 +12,11 @@
#include <string.h> #include <string.h>
#include "leds.h" #include "leds.h"
#include "sensor.h" #include "sht31.h"
#include "http_client.h" #include "http_client.h"
//static const char * TAG = "main.c"; static const char * TAG = "main.c";
/* /*
static void wifi_init(void) { static void wifi_init(void) {
tcpip_adapter_init(); tcpip_adapter_init();
@@ -36,6 +36,25 @@ static void wifi_init(void) {
} }
*/ */
void sensor_tester(void)
{
ESP_ERROR_CHECK(sht31_init());
//float temperature, humidity;
int temperature, humidity;
while (1) {
//if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) {
if (sht31_read_temp_hum_int(&temperature, &humidity) == ESP_OK) {
//ESP_LOGI(TAG, "Temperature: %.2f°C, Humidity: %.2f%%", temperature, humidity);
ESP_LOGI(TAG, "Temperature: %i°C, Humidity: %i%%", temperature, humidity);
} else {
ESP_LOGE(TAG, "Failed to read from SHT31 sensor");
}
vTaskDelay(2000 / portTICK_RATE_MS);
}
}
// Task to read data from UART // Task to read data from UART
void uart_read_task(void *pvParameters) { void uart_read_task(void *pvParameters) {
const unsigned bufferSize = 1024; const unsigned bufferSize = 1024;
@@ -95,6 +114,9 @@ void app_main()
//wifi_init(); //wifi_init();
initLEDs(); initLEDs();
sensor_tester();
//xTaskCreate(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); //xTaskCreate(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 10, NULL); xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 10, NULL);
//xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL); //xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
-50
View File
@@ -1,50 +0,0 @@
#include "sensor.h"
#include <stdio.h>
#include "driver/i2c.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.
// 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
void initSensor(void) {
esp_err_t err = i2c_driver_install(I2C_NUM_0, config.mode);
esp_err_t err2 = i2c_param_config(I2C_NUM_0, &config);
// Initialize the sensor hardware
// For example, set up I2C communication, configure registers, etc.
printf("Sensor initialized.\n");
}
// Function to get the current humidity level
// Returns the humidity as a percentage (0-100)
float getHumidity(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 getTemperature(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;
}
+145
View File
@@ -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;
}
+3 -3
View File
@@ -108,9 +108,9 @@ CONFIG_ESP8266_NMI_WDT=y
# CONFIG_ESP8266_XTAL_FREQ_40 is not set # CONFIG_ESP8266_XTAL_FREQ_40 is not set
CONFIG_ESP8266_XTAL_FREQ_26=y CONFIG_ESP8266_XTAL_FREQ_26=y
CONFIG_ESP8266_XTAL_FREQ=26 CONFIG_ESP8266_XTAL_FREQ=26
# CONFIG_ESP8266_DEFAULT_CPU_FREQ_80 is not set CONFIG_ESP8266_DEFAULT_CPU_FREQ_80=y
CONFIG_ESP8266_DEFAULT_CPU_FREQ_160=y # CONFIG_ESP8266_DEFAULT_CPU_FREQ_160 is not set
CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ=160 CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ=80
CONFIG_ESP_FILENAME_MACRO_NO_PATH=y CONFIG_ESP_FILENAME_MACRO_NO_PATH=y
# CONFIG_ESP_FILENAME_MACRO_RAW is not set # CONFIG_ESP_FILENAME_MACRO_RAW is not set
# CONFIG_ESP_FILENAME_MACRO_NULL is not set # CONFIG_ESP_FILENAME_MACRO_NULL is not set