Driver for Sensirion SHT3x sensor added
This is a driver for Sensirion SHT3x temperature and humidity sensors connected via I2C. This commit is a rebasing and contains some interface changes based on the review from @ourairquality. SHT3x driver changes - additional include to satisfy the Travis CI test build - README.md has been shortened - special handling for the timer overflow in sht3x_is_measuring removed - some whitespaces removed - crc8 computation changed to a non table lookup version - measurement duration is now given in ticks and can be used directly for vTaskDelay (documentation and examples changed accordingly) - number of ticks for measurement duration takes now into account portTICK_PERIOD_MS - clock stretching disabled on sensor to avoid blocking when data are not ready to read - calculation of maesurement duration adds now one and a half ticks to be sure that measurement duration is not too short - function sht3x_is_measuring is now private and only for internal use, user task has always to use function vTaskDelay to wait for measurement results - function sht3x_is_measuring was simplified and returns now just a boolean value - private function sht3x_reset added which is used to reset the sensor during initialization - active flag in sensor data structure not needed anymore - function sht3_get_raw_data simplified - function sht3x_start_measurement returns now only a boolean - function sht3x_start_measurement does not check anymore whether there is already a measurment running - new function sht3x_get_measurement_duration which returns the measurement duration in ticks for configured repeatability
This commit is contained in:
parent
87a3503f93
commit
b8c22d2d21
9 changed files with 1269 additions and 0 deletions
46
examples/sht3x/README.md
Normal file
46
examples/sht3x/README.md
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# SHT3x Driver Examples
|
||||
|
||||
These examples demonstrate the usage of the SHT3x driver with only one and multiple SHT3x sensors.
|
||||
|
||||
## Hardware setup
|
||||
|
||||
There are examples for only one sensor and examples for two sensors.
|
||||
|
||||
To run examples with **one sensor**, just use GPIO5 (SCL) and GPIO4 (SDA) to connect to the SHT3x sensor's I2C interface.
|
||||
|
||||
```
|
||||
+------------------------+ +--------+
|
||||
| ESP8266 Bus 0 | | SHT3x |
|
||||
| GPIO 5 (SCL) >---- > SCL |
|
||||
| GPIO 4 (SDA) ------- SDA |
|
||||
| | +--------+
|
||||
+------------------------+
|
||||
```
|
||||
|
||||
If you want to run examples with **two sensors**, you could do this with only one bus and different I2C addresses or with two buses and the same or different I2C addresses. In later case, use GPIO14 (SCL) and GPIO12 (SDA) for the second bus to connect to the second SHT3x sensor's I2C interface.
|
||||
|
||||
```
|
||||
+------------------------+ +----------+
|
||||
| ESP8266 Bus 0 | | SHT3x_1 |
|
||||
| GPIO 5 (SCL) >-----> SCL |
|
||||
| GPIO 4 (SDA) ------- SDA |
|
||||
| | +----------+
|
||||
| Bus 1 | | SHT3x_2 |
|
||||
| GPIO 14 (SCL) >-----> SCL |
|
||||
| GPIO 12 (SDA) ------- SDA |
|
||||
+------------------------+ +----------+
|
||||
```
|
||||
|
||||
## Example description
|
||||
|
||||
Examples show how to use the driver in single shot as well as in periodic mode.
|
||||
|
||||
|
||||
### _sht3x_one_sensor_
|
||||
|
||||
The simple example show how to use the driver with one SHT3x sensor. It demonstrates two different user task implementations, one in *single shot mode* and one in *periodic mode*.
|
||||
|
||||
### _sht3x_two_sensors_
|
||||
|
||||
This simple example shows how to use the driver for two sensors. As with the example _sht3x_one_sensor_, it uses two different user task implementations.
|
||||
|
||||
3
examples/sht3x/sht3x_one_sensor/Makefile
Normal file
3
examples/sht3x/sht3x_one_sensor/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=SHT3x_One_Sensor
|
||||
EXTRA_COMPONENTS = extras/i2c extras/sht3x
|
||||
include ../../../common.mk
|
||||
119
examples/sht3x/sht3x_one_sensor/sht3x_one_sensor.c
Normal file
119
examples/sht3x/sht3x_one_sensor/sht3x_one_sensor.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
* Simple example with one SHT3x sensor.
|
||||
*
|
||||
* It shows two different user task implementations, one in *single shot mode*
|
||||
* and one in *periodic mode*.
|
||||
*
|
||||
* Which implementation is used is defined by constant SINGLE_SHOT_MODE.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* +------------------------+ +----------+
|
||||
* | ESP8266 Bus 0 | | SHT3x |
|
||||
* | GPIO 5 (SCL) ------> SCL |
|
||||
* | GPIO 4 (SDA) ------- SDA |
|
||||
* +------------------------+ +----------+
|
||||
*/
|
||||
|
||||
// #define SINGLE_SHOT_MODE
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
// include SHT3x driver
|
||||
#include "sht3x/sht3x.h"
|
||||
|
||||
// define I2C interfaces at which SHTx3 sensors are connected
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
static sht3x_sensor_t* sensor; // sensor device data structure
|
||||
|
||||
#ifdef SINGLE_SHOT_MODE
|
||||
/*
|
||||
* User task that triggers a measurement every 5 seconds. Due to
|
||||
* power efficiency reasons, it uses the SHT3x *single_shot*.
|
||||
*/
|
||||
void user_task (void *pvParameters)
|
||||
{
|
||||
sht3x_values_t values;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Trigger one measurement in single shot mode.
|
||||
sht3x_start_measurement (sensor, single_shot);
|
||||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sensor));
|
||||
|
||||
// retrieve the values and do something with them
|
||||
if (sht3x_get_results (sensor, &values))
|
||||
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity);
|
||||
|
||||
// wait until 5 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
#else // PERIODIC MODE
|
||||
/*
|
||||
* User task that fetches latest measurement results of sensor every 2
|
||||
* seconds. It starts the SHT3x in periodic mode with 1 measurements per
|
||||
* second (*periodic_1mps*).
|
||||
*/
|
||||
void user_task (void *pvParameters)
|
||||
{
|
||||
sht3x_values_t values;
|
||||
|
||||
// Start periodic measurements with 1 measurement per second.
|
||||
sht3x_start_measurement (sensor, periodic_1mps);
|
||||
|
||||
// Wait until first measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sensor));
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Get the values and do something with them.
|
||||
if (sht3x_get_results (sensor, &values))
|
||||
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity);
|
||||
|
||||
// Wait until 2 seconds (cycle time) are over.
|
||||
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
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 interfaces at which SHT3x sensors are connected
|
||||
// (different busses are possible).
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Create the sensors.
|
||||
if ((sensor = sht3x_init_sensor (I2C_BUS, SHT3x_ADDR_2)))
|
||||
{
|
||||
// Create a user task that uses the sensors.
|
||||
xTaskCreate(user_task, "user_task", 256, NULL, 2, 0);
|
||||
}
|
||||
|
||||
// That's it.
|
||||
}
|
||||
3
examples/sht3x/sht3x_two_sensors/Makefile
Normal file
3
examples/sht3x/sht3x_two_sensors/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=SHT3x_Two_Sensors
|
||||
EXTRA_COMPONENTS = extras/i2c extras/sht3x
|
||||
include ../../../common.mk
|
||||
130
examples/sht3x/sht3x_two_sensors/sht3x_two_sensors.c
Normal file
130
examples/sht3x/sht3x_two_sensors/sht3x_two_sensors.c
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/**
|
||||
* Simple example with two SHT3x sensors.
|
||||
*
|
||||
* It shows two different user task implementations, one in *single shot mode*
|
||||
* and one in *periodic mode*.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* +------------------------+ +----------+
|
||||
* | ESP8266 Bus 0 | | SHT3x_1 |
|
||||
* | GPIO 5 (SCL) ------> SCL |
|
||||
* | GPIO 4 (SDA) ------- SDA |
|
||||
* | | +----------+
|
||||
* | Bus 1 | | SHT3x_2 |
|
||||
* | GPIO 14 (SCL) ------> SCL |
|
||||
* | GPIO 12 (SDA) ------- SDA |
|
||||
* +------------------------+ +----------+
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.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((14))
|
||||
#define I2C_2_SDA_PIN GPIO_ID_PIN((12))
|
||||
|
||||
static sht3x_sensor_t* sensor1;
|
||||
static sht3x_sensor_t* sensor2;
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor1 every 5 seconds. Due to
|
||||
* power efficiency reasons, it uses the SHT3x *single_shot* mode.
|
||||
*/
|
||||
void user_task_sensor1 (void *pvParameters)
|
||||
{
|
||||
sht3x_values_t values;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Trigger one measurement in single shot mode.
|
||||
sht3x_start_measurement (sensor1, single_shot);
|
||||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sensor1));
|
||||
|
||||
// retrieve the values and do something with them
|
||||
if (sht3x_get_results (sensor1, &values))
|
||||
printf("%.3f SHT3x Sensor1: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity);
|
||||
|
||||
// wait until 5 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* User task that fetches latest measurement results of sensor2 every 2
|
||||
* seconds. It starts the SHT3x in periodic mode with 10 measurements per
|
||||
* second (*periodic_10mps*).
|
||||
*/
|
||||
void user_task_sensor2 (void *pvParameters)
|
||||
{
|
||||
sht3x_values_t values;
|
||||
|
||||
// start periodic measurement mode
|
||||
sht3x_start_measurement (sensor2, periodic_10mps);
|
||||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sensor2));
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Retrieve the values and do something with them.
|
||||
if (sht3x_get_results (sensor2, &values))
|
||||
printf("%.3f SHT3x Sensor2: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity);
|
||||
|
||||
// Wait until 2 seconds are over.
|
||||
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 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);
|
||||
|
||||
// Create sensors.
|
||||
sensor1 = sht3x_init_sensor (I2C_1_BUS, SHT3x_ADDR_2);
|
||||
sensor2 = sht3x_init_sensor (I2C_2_BUS, SHT3x_ADDR_1);
|
||||
|
||||
if (sensor1 && sensor2)
|
||||
{
|
||||
// Create the tasks that use the sensors.
|
||||
xTaskCreate(user_task_sensor1, "user_task_sensor1", 256, NULL, 2, 0);
|
||||
xTaskCreate(user_task_sensor2, "user_task_sensor2", 256, NULL, 2, 0);
|
||||
}
|
||||
|
||||
// That's it.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue