From 34e0561f46e5f125e9dd98dcd359283c01faf4dc Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Sat, 10 Aug 2024 12:27:57 +0200 Subject: [PATCH] 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 --- firmware/main/include/{sensor.h => sht31.h} | 15 +- firmware/main/src/main.c | 26 +++- firmware/main/src/sensor.c | 50 ------- firmware/main/src/sht31.c | 145 ++++++++++++++++++++ firmware/sdkconfig | 6 +- 5 files changed, 182 insertions(+), 60 deletions(-) rename firmware/main/include/{sensor.h => sht31.h} (51%) delete mode 100644 firmware/main/src/sensor.c create mode 100644 firmware/main/src/sht31.c diff --git a/firmware/main/include/sensor.h b/firmware/main/include/sht31.h similarity index 51% rename from firmware/main/include/sensor.h rename to firmware/main/include/sht31.h index 0d6152a..201597c 100644 --- a/firmware/main/include/sensor.h +++ b/firmware/main/include/sht31.h @@ -1,20 +1,25 @@ -#ifndef SENSOR_H -#define SENSOR_H +#ifndef SHT31_H +#define SHT31_H +#include #ifdef __cplusplus extern "C" { #endif + // Function to initialize the sensor -void initSensor(void); +esp_err_t sht31_init(void); // Function to get the current humidity level // Returns the humidity as a percentage (0-100) -float getHumidity(void); +float sht31_get_humidity(void); // Function to get the current temperature // 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 } diff --git a/firmware/main/src/main.c b/firmware/main/src/main.c index cf044c0..1646907 100644 --- a/firmware/main/src/main.c +++ b/firmware/main/src/main.c @@ -12,11 +12,11 @@ #include #include "leds.h" -#include "sensor.h" +#include "sht31.h" #include "http_client.h" -//static const char * TAG = "main.c"; +static const char * TAG = "main.c"; /* static void wifi_init(void) { 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 void uart_read_task(void *pvParameters) { const unsigned bufferSize = 1024; @@ -95,6 +114,9 @@ void app_main() //wifi_init(); initLEDs(); + + sensor_tester(); + //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(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL); diff --git a/firmware/main/src/sensor.c b/firmware/main/src/sensor.c deleted file mode 100644 index 6e4b3f7..0000000 --- a/firmware/main/src/sensor.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "sensor.h" -#include -#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; -} - diff --git a/firmware/main/src/sht31.c b/firmware/main/src/sht31.c new file mode 100644 index 0000000..b6cd0fb --- /dev/null +++ b/firmware/main/src/sht31.c @@ -0,0 +1,145 @@ +#include "sht31.h" +#include +#include +//#include +#include +#include + +#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; +} diff --git a/firmware/sdkconfig b/firmware/sdkconfig index e550462..27cd472 100644 --- a/firmware/sdkconfig +++ b/firmware/sdkconfig @@ -108,9 +108,9 @@ CONFIG_ESP8266_NMI_WDT=y # CONFIG_ESP8266_XTAL_FREQ_40 is not set CONFIG_ESP8266_XTAL_FREQ_26=y CONFIG_ESP8266_XTAL_FREQ=26 -# CONFIG_ESP8266_DEFAULT_CPU_FREQ_80 is not set -CONFIG_ESP8266_DEFAULT_CPU_FREQ_160=y -CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ=160 +CONFIG_ESP8266_DEFAULT_CPU_FREQ_80=y +# CONFIG_ESP8266_DEFAULT_CPU_FREQ_160 is not set +CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ=80 CONFIG_ESP_FILENAME_MACRO_NO_PATH=y # CONFIG_ESP_FILENAME_MACRO_RAW is not set # CONFIG_ESP_FILENAME_MACRO_NULL is not set