diff --git a/examples/sht3x/README.md b/examples/sht3x/README.md new file mode 100644 index 0000000..47ce3a1 --- /dev/null +++ b/examples/sht3x/README.md @@ -0,0 +1,7 @@ +# SHT3x Driver Examples + +This example references two addtional drivers [i2c](https://github.com/kanflo/esp-open-rtos-driver-i2c) and [sht3x](https://github.com/gschorcht/esp-open-rtos/extras/sht3x), which are provided in the `../../extras` folder. + +If you plan to use one or both of this drivers in your own projects, please check the main development pages for updated versions or reported issues. + +To run this example connect the SHT3x SCL to GPIO5 and SDA to GPIO4. diff --git a/examples/sht3x/sht3x_callback_one_sensor/Makefile b/examples/sht3x/sht3x_callback_one_sensor/Makefile new file mode 100644 index 0000000..7f15752 --- /dev/null +++ b/examples/sht3x/sht3x_callback_one_sensor/Makefile @@ -0,0 +1,3 @@ +PROGRAM=SHT3x_Reader +EXTRA_COMPONENTS = extras/i2c extras/sht3x +include ../../../common.mk diff --git a/examples/sht3x/sht3x_callback_one_sensor/sht3x_callback_one_sensor.c b/examples/sht3x/sht3x_callback_one_sensor/sht3x_callback_one_sensor.c new file mode 100644 index 0000000..45e6a02 --- /dev/null +++ b/examples/sht3x/sht3x_callback_one_sensor/sht3x_callback_one_sensor.c @@ -0,0 +1,92 @@ +/** + * Simple example with one sensor using SHT3x driver using callback function + * to get the results. + */ + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" + +// include SHT3x driver + +#include "sht3x/sht3x.h" + +// define I2C interface at which SHTx3 sensor is connected +#define I2C_BUS 0 +#define I2C_SCL_PIN GPIO_ID_PIN((5)) +#define I2C_SDA_PIN GPIO_ID_PIN((4)) + +#define SHT3x_ADDR 0x45 + +static uint32_t sensor; + +/** + * Everything you need is a callback function that can handle + * callbacks from the background measurement task of the + * SHT3x driver. This function is called periodically + * and delivers the actual and average sensor value sets for + * given sensor. + */ +static void my_callback_function (uint32_t sensor, + sht3x_value_set_t actual, + sht3x_value_set_t average) +{ + printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) F, %.2f (%.2f) \n", + (double)sdk_system_get_time()*1e-3, sensor, + actual.c_temperature, average.c_temperature, + actual.f_temperature, average.f_temperature, + actual.humidity, average.humidity); +} + + +void user_init(void) +{ + // Set UART Parameter + uart_set_baud(0, 115200); + + // Give the UART some time to settle + sdk_os_delay_us(500); + + // Init I2C bus interface at which SHT3x sensor is connected + i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K); + + // Init SHT3x driver + if (!sht3x_init()) + { + // error message + return; + } + + // Create sensors + if ((sensor = sht3x_create_sensor (I2C_BUS, SHT3x_ADDR)) < 0) + { + // error message + return; + } + + // Set the callback function + if (!sht3x_set_callback_function (sensor, &my_callback_function)) + { + // error message + return; + } + + /* + // Optional: Set the period of measurements (default 1000 ms) + if (!sht3x_set_measurement_period (sensor, 1000)) + { + // error message + return; + } + + // Optional: Set the weight for expontial moving average (default 0.2) + if (!sht3x_set_average_weight (sensor, 0.2)) + { + // error message + return; + } + */ + + // That's it. +} diff --git a/examples/sht3x/sht3x_callback_two_sensors/Makefile b/examples/sht3x/sht3x_callback_two_sensors/Makefile new file mode 100644 index 0000000..7f15752 --- /dev/null +++ b/examples/sht3x/sht3x_callback_two_sensors/Makefile @@ -0,0 +1,3 @@ +PROGRAM=SHT3x_Reader +EXTRA_COMPONENTS = extras/i2c extras/sht3x +include ../../../common.mk diff --git a/examples/sht3x/sht3x_callback_two_sensors/sht3x_callback_two_sensors.c b/examples/sht3x/sht3x_callback_two_sensors/sht3x_callback_two_sensors.c new file mode 100644 index 0000000..a287632 --- /dev/null +++ b/examples/sht3x/sht3x_callback_two_sensors/sht3x_callback_two_sensors.c @@ -0,0 +1,85 @@ +/** + * Simple example with two sensors connected to two different I2C interfaces + * using SHT3x driver using callback function to get the results. + */ + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" + +// include SHT3x driver + +#include "sht3x/sht3x.h" + +// define I2C interfaces at which SHTx3 sensors are connected +#define I2C_1_BUS 0 +#define I2C_1_SCL_PIN GPIO_ID_PIN((5)) +#define I2C_1_SDA_PIN GPIO_ID_PIN((4)) + +#define I2C_2_BUS 1 +#define I2C_2_SCL_PIN GPIO_ID_PIN((16)) +#define I2C_2_SDA_PIN GPIO_ID_PIN((14)) + +#define SHT3x_ADDR_1 0x44 +#define SHT3x_ADDR_2 0x45 + +static uint32_t sensor1; +static uint32_t sensor2; + +/** + * Everything you need is a callback function that can handle + * callbacks from the background measurement task of the + * SHT3x driver. This function is called periodically + * and delivers the actual and average sensor values for + * given sensor. + */ +static void my_callback_function (uint32_t sensor, + sht3x_value_set_t actual, + sht3x_value_set_t average) +{ + printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) F, %.2f (%.2f) \n", + (double)sdk_system_get_time()*1e-3, sensor, + actual.c_temperature, average.c_temperature, + actual.f_temperature, average.f_temperature, + actual.humidity, average.humidity); +} + + +void user_init(void) +{ + // Please note: Return values are not considered for readability reasons. + // All functions return a boolean that allows effecitve error handling. + + // Set UART Parameter + uart_set_baud(0, 115200); + + // Give the UART some time to settle + sdk_os_delay_us(500); + + // Init I2C bus interfaces at which SHT3x sensors are connected + // (different busses are possible) + i2c_init(I2C_1_BUS, I2C_1_SCL_PIN, I2C_1_SDA_PIN, I2C_FREQ_100K); + i2c_init(I2C_2_BUS, I2C_2_SCL_PIN, I2C_2_SDA_PIN, I2C_FREQ_100K); + + // Init SHT3x driver (parameter is the number of SHT3x sensors used) + sht3x_init(); + + // Create sensors + sensor1 = sht3x_create_sensor (I2C_1_BUS, SHT3x_ADDR_1); + sensor2 = sht3x_create_sensor (I2C_2_BUS, SHT3x_ADDR_2); + + // Set the callback function + sht3x_set_callback_function (sensor1, &my_callback_function); + sht3x_set_callback_function (sensor2, &my_callback_function); + + // Optional: Set the period of measurements (default 1000 ms) + sht3x_set_measurement_period (sensor1, 1000); + sht3x_set_measurement_period (sensor2, 5000); + + // Optional: Set the weight for expontial moving average (default 0.2) + sht3x_set_average_weight (sensor1, 0.15); + sht3x_set_average_weight (sensor2, 0.25); + + // That's it. +} diff --git a/examples/sht3x/sht3x_task_one_sensor/Makefile b/examples/sht3x/sht3x_task_one_sensor/Makefile new file mode 100644 index 0000000..7f15752 --- /dev/null +++ b/examples/sht3x/sht3x_task_one_sensor/Makefile @@ -0,0 +1,3 @@ +PROGRAM=SHT3x_Reader +EXTRA_COMPONENTS = extras/i2c extras/sht3x +include ../../../common.mk diff --git a/examples/sht3x/sht3x_task_one_sensor/sht3x_task_one_sensor.c b/examples/sht3x/sht3x_task_one_sensor/sht3x_task_one_sensor.c new file mode 100644 index 0000000..565c367 --- /dev/null +++ b/examples/sht3x/sht3x_task_one_sensor/sht3x_task_one_sensor.c @@ -0,0 +1,81 @@ +/** + * Simple example with one sensor using SHT3x driver in periodic mode + * and a user task. + */ + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "task.h" + +// include SHT3x driver + +#include "sht3x/sht3x.h" + +// define I2C interface at which SHTx3 sensor is connected +#define I2C_BUS 0 +#define I2C_SCL_PIN GPIO_ID_PIN((5)) +#define I2C_SDA_PIN GPIO_ID_PIN((4)) + +#define SHT3x_ADDR 0x45 + +static uint32_t sensor; + +/** + * Task that executes function sht3x_get_values to get last + * actual and average sensor values for given sensor. + */ +void my_user_task(void *pvParameters) +{ + sht3x_value_set_t actual; + sht3x_value_set_t average; + + while (1) + { + sht3x_get_values(sensor, &actual, &average); + + printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) F, %.2f (%.2f) \n", + (double)sdk_system_get_time()*1e-3, sensor, + actual.c_temperature, average.c_temperature, + actual.f_temperature, average.f_temperature, + actual.humidity, average.humidity); + + // Delay value has to be equal or greater than the period of measurment + // task + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + + +void user_init(void) +{ + // Please note: Return values are not considered for readability reasons. + // All functions return a boolean that allows effecitve error handling. + + // Set UART Parameter + uart_set_baud(0, 115200); + + // Give the UART some time to settle + sdk_os_delay_us(500); + + // Init I2C bus interface at which SHT3x sensor is connected + i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K); + + // Init SHT3x driver + sht3x_init(); + + // Create sensors + sensor = sht3x_create_sensor (I2C_BUS, SHT3x_ADDR); + + // Optional: Set the period of measurements (default 1000 ms) + // sht3x_set_measurement_period (sensor, 1000); + + // Optional: Set the weight for expontial moving average (default 0.2) + // sht3x_set_average_weight (sensor, 0.2); + + // Create a task for getting results + xTaskCreate(my_user_task, "my_user_task", 256, NULL, 2, NULL); + + // That's it. +} diff --git a/examples/sht3x/sht3x_timer_one_sensor/Makefile b/examples/sht3x/sht3x_timer_one_sensor/Makefile new file mode 100644 index 0000000..7f15752 --- /dev/null +++ b/examples/sht3x/sht3x_timer_one_sensor/Makefile @@ -0,0 +1,3 @@ +PROGRAM=SHT3x_Reader +EXTRA_COMPONENTS = extras/i2c extras/sht3x +include ../../../common.mk diff --git a/examples/sht3x/sht3x_timer_one_sensor/sht3x_periodic_timer.c b/examples/sht3x/sht3x_timer_one_sensor/sht3x_periodic_timer.c new file mode 100644 index 0000000..efcb98b --- /dev/null +++ b/examples/sht3x/sht3x_timer_one_sensor/sht3x_periodic_timer.c @@ -0,0 +1,80 @@ +/** + * Simple example with one sensor using SHT3x driver using function + * sht3x_get_values triggered by software timers to get the results. + */ + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "timers.h" + +// include SHT3x driver + +#include "sht3x/sht3x.h" + +// define I2C interface at which SHTx3 sensor is connected +#define I2C_BUS 0 +#define I2C_SCL_PIN GPIO_ID_PIN((5)) +#define I2C_SDA_PIN GPIO_ID_PIN((4)) + +#define SHT3x_ADDR 0x45 + +static TimerHandle_t timerHandle; +static uint32_t sensor; + +/** + * Timer callback function that executes function sht3x_get_values to get last + * actual and average sensor values for given sensor. + */ +static void my_sensor_timer_cb(TimerHandle_t xTimer) +{ + sht3x_value_set_t actual; + sht3x_value_set_t average; + + sht3x_get_values(sensor, &actual, &average); + + printf("%.3f Sensor %d: %.2f (%.2f) C, %.2f (%.2f) F, %.2f (%.2f) \n", + (double)sdk_system_get_time()*1e-3, sensor, + actual.c_temperature, average.c_temperature, + actual.f_temperature, average.f_temperature, + actual.humidity, average.humidity); +} + + +void user_init(void) +{ + // Please note: Return values are not considered for readability reasons. + // All functions return a boolean that allows effecitve error handling. + + // Set UART Parameter + uart_set_baud(0, 115200); + + // Give the UART some time to settle + sdk_os_delay_us(500); + + // Init I2C bus interface at which SHT3x sensor is connected + i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K); + + // Init SHT3x driver + sht3x_init(); + + // Create sensors + sensor = sht3x_create_sensor (I2C_BUS, SHT3x_ADDR); + + // Optional: Set the period of measurements (default 1000 ms) + // sht3x_set_measurement_period (sensor, 1000); + + // Optional: Set the weight for expontial moving average (default 0.2) + // sht3x_set_average_weight (sensor, 0.2); + + // Create and start a timer that calls sht3x_i2c_timer_cb every 1000 ms + // Timer value has to be equal or greater than the period of measurment + // task above + + if ((timerHandle = xTimerCreate("SHT3x Trigger", 1000/portTICK_PERIOD_MS, + pdTRUE, NULL, my_sensor_timer_cb))) + xTimerStart(timerHandle, 0); + + // That's it. +}