From f4436308dc8e37eabe471fb297ffd21d02e6aa9d Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Sun, 11 Aug 2024 12:44:44 +0200 Subject: [PATCH] new file: main/include/functions.h modified: main/include/sht31.h renamed: main/include/lib.h -> main/src/functions.c modified: main/src/http_client.c modified: main/src/main.c modified: main/src/sht31.c --- firmware/main/include/functions.h | 10 +++++ firmware/main/include/sht31.h | 9 +--- .../main/{include/lib.h => src/functions.c} | 18 +++++--- firmware/main/src/http_client.c | 2 +- firmware/main/src/main.c | 32 +++----------- firmware/main/src/sht31.c | 42 ++++++++++--------- 6 files changed, 53 insertions(+), 60 deletions(-) create mode 100644 firmware/main/include/functions.h rename firmware/main/{include/lib.h => src/functions.c} (84%) diff --git a/firmware/main/include/functions.h b/firmware/main/include/functions.h new file mode 100644 index 0000000..1f4a218 --- /dev/null +++ b/firmware/main/include/functions.h @@ -0,0 +1,10 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +void split_float(float num, int *int_part, float *dec_part); + +char* floatToString(float num); + +char* intToString(int num); + +#endif diff --git a/firmware/main/include/sht31.h b/firmware/main/include/sht31.h index 230d783..b1dddb0 100644 --- a/firmware/main/include/sht31.h +++ b/firmware/main/include/sht31.h @@ -10,13 +10,8 @@ extern "C" { // Function to initialize the sensor esp_err_t sht31_init(void); -// Function to get the current humidity level -// Returns the humidity as a percentage (0-100) -float sht31_get_humidity(void); - -// Function to get the current temperature -// Returns the temperature in degrees Celsius -float sht31_get_temperature(void); +// Test sensor and print output to serial +void sht31_sensor_test_task(void *pvParameters); esp_err_t sht31_read_temp_hum(float *temperature, float *humidity); diff --git a/firmware/main/include/lib.h b/firmware/main/src/functions.c similarity index 84% rename from firmware/main/include/lib.h rename to firmware/main/src/functions.c index 5797337..4cf53b8 100644 --- a/firmware/main/include/lib.h +++ b/firmware/main/src/functions.c @@ -1,10 +1,18 @@ -#ifndef LIB_H -#define LIB_H +#include "functions.h" +#include "freertos/FreeRTOS.h" +static const char * TAG = "functions.c"; +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; +} // Helper function to reverse a string -void reverse(char* str, int len) { +static void reverse(char* str, int len) { int i = 0, j = len - 1; while (i < j) { char temp = str[i]; @@ -16,7 +24,7 @@ void reverse(char* str, int len) { } // Helper function to convert an integer to a string -int intToStr(int x, char str[], int d) { +static int intToStr(int x, char str[], int d) { int i = 0; while (x) { str[i++] = (x % 10) + '0'; @@ -108,4 +116,4 @@ char* intToString(int num) return str; } -#endif + diff --git a/firmware/main/src/http_client.c b/firmware/main/src/http_client.c index ff83367..47359bf 100644 --- a/firmware/main/src/http_client.c +++ b/firmware/main/src/http_client.c @@ -11,7 +11,7 @@ #include #include "http_client.h" -#include "lib.h" +#include "functions.h" #define MAX_HTTP_OUTPUT_BUFFER 2048 diff --git a/firmware/main/src/main.c b/firmware/main/src/main.c index 9420f05..bfd058b 100644 --- a/firmware/main/src/main.c +++ b/firmware/main/src/main.c @@ -17,7 +17,7 @@ static const char * TAG = "main.c"; -/* + static void wifi_init(void) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -34,7 +34,7 @@ static void wifi_init(void) { ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_connect()); } -*/ + static void split_float(float num, int *int_part, float *dec_part) { // Extract the integer part @@ -45,27 +45,6 @@ static void split_float(float num, int *int_part, float *dec_part) { } -void sensor_tester(void) -{ - ESP_ERROR_CHECK(sht31_init()); - - //float temperature, humidity; - float temperature, humidity, temp_dec_part, hum_dec_part; - int temp_int_part, hum_int_part; - - while (1) { - if (sht31_read_temp_hum(&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: %i.%i°C, Humidity: %i.%i%%", temp_int_part, (int)(temp_dec_part*1000), hum_int_part, (int)(hum_dec_part*1000)); - } 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; @@ -126,9 +105,8 @@ 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(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); + //xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 10, NULL); + xTaskCreate(sht31_sensor_test_task, "sht31_sensor_test_task", 2048, NULL, 10, NULL); //xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL); } diff --git a/firmware/main/src/sht31.c b/firmware/main/src/sht31.c index 229ec34..5045ec0 100644 --- a/firmware/main/src/sht31.c +++ b/firmware/main/src/sht31.c @@ -4,6 +4,7 @@ //#include #include #include +#include "functions.h" #define SDA_PIN GPIO_NUM_2 #define CLK_PIN GPIO_NUM_14 @@ -24,6 +25,27 @@ static const i2c_config_t config = { .clk_stretch_tick = 5 }; +void sht31_sensor_test_task(void *pvParameters) +{ + ESP_ERROR_CHECK(sht31_init()); + + //float temperature, humidity; + float temperature, humidity, temp_dec_part, hum_dec_part; + int temp_int_part, hum_int_part; + + while (1) { + if (sht31_read_temp_hum(&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: %i.%i°C, Humidity: %i.%i%%", temp_int_part, (int)(temp_dec_part*1000), hum_int_part, (int)(hum_dec_part*1000)); + } else { + ESP_LOGE(TAG, "Failed to read from SHT31 sensor"); + } + vTaskDelay(2000 / portTICK_RATE_MS); + } +} + // Function to initialize the sensor esp_err_t sht31_init(void) { esp_err_t install_rc = i2c_driver_install(I2C_NUM_0, config.mode); @@ -36,26 +58,6 @@ esp_err_t sht31_init(void) { 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];