添加智能灯固件代码

This commit is contained in:
kerwincui
2021-07-13 17:14:51 +08:00
parent 332f74dd17
commit ecc0b91b8b
2568 changed files with 229441 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "sht3x.c"
INCLUDE_DIRS include
REQUIRES bus)

View File

@@ -0,0 +1,17 @@
# SHT3x
- This component will show you how to use I2C module read external i2c sensor data, here we use SHT3x-series temperature and humidity sensor(SHT30 is used this component).
- Pin assignment:
- GPIO21 is assigned as the data signal of i2c master port
- GPIO22 is assigned as the clock signal of i2c master port
- Connection:
* connect sda of sensor with GPIO21
* connect scl of sensor with GPIO22
- SHT3x measurement mode:
* single shot data acquisition mode: in this mode one issued measurement command triggers the acquisition of one data pair.
* periodic data acquisition mode: in this mode one issued measurement command yields a stream of data pairs. when use periodic data acquisition mode, you should send hex code 0xE000 firstly, and send 0x3093 to stop periodic mode.
# Notice
- SHT3x uses 8-bit CRC checksum, you can see `CheckCrc8() `for detail.
- The raw measurement data needs to be convert to physical scale. formulas are shown in `sht3x_get_humiture()`

View File

@@ -0,0 +1,8 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
COMPONENT_ADD_INCLUDEDIRS := include
COMPONENT_SRCDIRS := .

View File

@@ -0,0 +1,233 @@
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _SHT3x_H_
#define _SHT3x_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include "driver/i2c.h"
#include "i2c_bus.h"
#include "esp_log.h"
#include "math.h"
typedef enum {
SOFT_RESET_CMD = 0x30A2, /*!< Command to soft reset*/
READOUT_FOR_PERIODIC_MODE = 0xE000, /*!< Command to read Periodic*/
READ_SERIAL_NUMBER = 0x3780, /*!< Command to read senser number*/
SHT3x_STOP_PERIODIC = 0x3093, /*!< Command to break or stop periodic mode*/
SHT3x_ART_CMD = 0x2B32, /*!< Command to accelerated response time*/
/* Single Shot Data Acquisition Mode*/
SHT3x_SINGLE_HIGH_ENABLED = 0x2C06, /*!< Command to set measure mode as Single Shot Data Acquisition mode in high repeatability and Clock Stretching enabled*/
SHT3x_SINGLE_MEDIUM_ENABLED = 0x2C0D, /*!< Command to set measure mode as Single Shot Data Acquisition mode in medium repeatability and Clock Stretching enabled*/
SHT3x_SINGLE_LOW_ENABLED = 0x2C10, /*!< Command to set measure mode as Single Shot Data Acquisition mode in low repeatability and Clock Stretching enabled*/
SHT3x_SINGLE_HIGH_DISABLED = 0x2400, /*!< Command to set measure mode as Single Shot Data Acquisition mode in high repeatability and Clock Stretching disabled*/
SHT3x_SINGLE_MEDIUM_DISABLED = 0x240B, /*!< Command to set measure mode as Single Shot Data Acquisition mode in medium repeatability and Clock Stretching disabled*/
SHT3x_SINGLE_LOW_DISABLED = 0x2416, /*!< Command to set measure mode as Single Shot Data Acquisition mode in low repeatability and Clock Stretching disabled*/
/* Periodic Data Acquisition mode*/
SHT3x_PER_0_5_HIGH = 0x2032, /*!< Command to set measure mode as Periodic Data Acquisition mode in high repeatability and 0.5 mps*/
SHT3x_PER_0_5_MEDIUM = 0x2024, /*!< Command to set measure mode as Periodic Data Acquisition mode in medium repeatability and 0.5 mps*/
SHT3x_PER_0_5_LOW = 0x202F, /*!< Command to set measure mode as Periodic Data Acquisition mode in low repeatability and 0.5 mps*/
SHT3x_PER_1_HIGH = 0x2130, /*!< Command to set measure mode as Periodic Data Acquisition mode in high repeatability and 1 mps*/
SHT3x_PER_1_MEDIUM = 0x2126, /*!< Command to set measure mode as Periodic Data Acquisition mode in medium repeatability and 1 mps*/
SHT3x_PER_1_LOW = 0x212D, /*!< Command to set measure mode as Periodic Data Acquisition mode in low repeatability and 1 mps*/
SHT3x_PER_2_HIGH = 0x2236, /*!< Command to set measure mode as Periodic Data Acquisition mode in high repeatability and 2 mps*/
SHT3x_PER_2_MEDIUM = 0x2220, /*!< Command to set measure mode as Periodic Data Acquisition mode in medium repeatability and 2 mps*/
SHT3x_PER_2_LOW = 0x222B, /*!< Command to set measure mode as Periodic Data Acquisition mode in low repeatability and 2 mps*/
SHT3x_PER_4_HIGH = 0x2334, /*!< Command to set measure mode as Periodic Data Acquisition mode in high repeatability and 4 mps*/
SHT3x_PER_4_MEDIUM = 0x2322, /*!< Command to set measure mode as Periodic Data Acquisition mode in medium repeatability and 4 mps*/
SHT3x_PER_4_LOW = 0x2329, /*!< Command to set measure mode as Periodic Data Acquisition mode in low repeatability and 4 mps*/
SHT3x_PER_10_HIGH = 0x2737, /*!< Command to set measure mode as Periodic Data Acquisition mode in high repeatability and 10 mps*/
SHT3x_PER_10_MEDIUM = 0x2721, /*!< Command to set measure mode as Periodic Data Acquisition mode in medium repeatability and 10 mps*/
SHT3x_PER_10_LOW = 0x272A, /*!< Command to set measure mode as Periodic Data Acquisition mode in low repeatability and 10 mps*/
/* cmd for sht3x heater condition*/
SHT3x_HEATER_ENABLE = 0x306D, /*!< Command to enable the heater*/
SHT3x_HEATER_DISABLED = 0x3066, /*!< Command to disable the heater*/
} sht3x_cmd_measure_t;
typedef enum {
SHT3x_ADDR_PIN_SELECT_VSS = 0x44, /*!< set address PIN select VSS */
SHT3x_ADDR_PIN_SELECT_VDD = 0x45, /*!< set address PIN select VDD */
} sht3x_set_address_t;
typedef void *sht3x_handle_t;
/**
* @brief Create sht3x handle_t
*
* @param bus sensorice object handle of sht3x
* @param dev_addr sensorice address
*
* @return
* - sht3x handle_t
*/
sht3x_handle_t sht3x_create(i2c_bus_handle_t bus, uint8_t dev_addr);
/**
* @brief Delete sht3x handle_t
*
* @param sensor point to sensorice object handle of sht3x
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_delete(sht3x_handle_t *sensor);
/**
* @brief Get temperature and humidity
*
* @param sensor object handle of shd3x
* @param Tem_val temperature data buffer
* @param Hum_val humidity data buffer
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_get_humiture(sht3x_handle_t sensor, float *Tem_val, float *Hum_val);
/**
* @brief Get temperature and humidity just once
*
* @param sensor object handle of shd3x
* @param Tem_val temperature data
* @param Hum_val humidity data
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_get_single_shot(sht3x_handle_t sensor, float *Tem_val, float *Hum_val);
/**
* @brief Soft reset for sht3x
*
* @param sensor object handle of sht3x
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_soft_reset(sht3x_handle_t sensor);
/**
* @brief stop or break or stop periodic mode
*
* @param sensor object handle of sht3x
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_stop_periodic(sht3x_handle_t sensor);
/**
* @brief accelerated response time
*
* @param sensor object handle of sht3x
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_art(sht3x_handle_t sensor);
/**
* @brief set measure mode of sht3x
*
* @param sensor object handle of shd3x
* @param sht3x_cmd_measure_t the instruction to set measurement mode
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t sht3x_set_measure_mode(sht3x_handle_t sensor, sht3x_cmd_measure_t sht3x_measure_mode);
/**
* @brief change the condition of sht3x heater
*
* @param sensor object handle of shd3x
* @param sht3x_cmd_measure_t the instruction to turn on/off heater of sht3x
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
* @note
* the default condition of heater is disabled
*/
esp_err_t sht3x_heater(sht3x_handle_t sensor, sht3x_cmd_measure_t sht3x_heater_condition);
/***implements of humiture hal interface****/
#ifdef CONFIG_SENSOR_HUMITURE_INCLUDED_SHT3X
/**
* @brief initialize sht3x with default configurations
*
* @param i2c_bus i2c bus handle the sensor will attached to
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t humiture_sht3x_init(i2c_bus_handle_t handle);
/**
* @brief de-initialize sht3x
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t humiture_sht3x_deinit(void);
/**
* @brief test if sht3x is active
*
* @return
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t humiture_sht3x_test(void);
/**
* @brief acquire relative humidity result one time.
*
* @param h point to result data (unit:percentage)
* @return esp_err_t
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t humiture_sht3x_acquire_humidity(float *h);
/**
* @brief acquire temperature result one time.
*
* @param t point to result data (unit:dCelsius)
* @return esp_err_t
* - ESP_OK Success
* - ESP_FAIL Fail
*/
esp_err_t humiture_sht3x_acquire_temperature(float *t);
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,318 @@
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "driver/i2c.h"
#include "i2c_bus.h"
#include "esp_log.h"
#include "esp_system.h"
#include "sht3x.h"
typedef struct {
i2c_bus_device_handle_t i2c_dev;
uint8_t dev_addr;
} sht3x_sensor_t;
sht3x_handle_t sht3x_create(i2c_bus_handle_t bus, uint8_t dev_addr)
{
sht3x_sensor_t *sens = (sht3x_sensor_t *) calloc(1, sizeof(sht3x_sensor_t));
sens->i2c_dev = i2c_bus_device_create(bus, dev_addr, i2c_bus_get_current_clk_speed(bus));
if (sens->i2c_dev == NULL) {
free(sens);
return NULL;
}
sens->dev_addr = dev_addr;
return (sht3x_handle_t) sens;
}
esp_err_t sht3x_delete(sht3x_handle_t *sensor)
{
if (*sensor == NULL) {
return ESP_OK;
}
sht3x_sensor_t *sens = (sht3x_sensor_t *)(*sensor);
i2c_bus_device_delete(&sens->i2c_dev);
free(sens);
*sensor = NULL;
return ESP_OK;
}
static esp_err_t sht3x_write_cmd(sht3x_handle_t sensor, sht3x_cmd_measure_t sht3x_cmd)
{
sht3x_sensor_t *sens = (sht3x_sensor_t *) sensor;
uint8_t cmd_buffer[2];
cmd_buffer[0] = sht3x_cmd >> 8;
cmd_buffer[1] = sht3x_cmd;
esp_err_t ret = i2c_bus_write_bytes(sens->i2c_dev, NULL_I2C_MEM_ADDR, 2, cmd_buffer);
return ret;
}
static esp_err_t sht3x_get_data(sht3x_handle_t sensor, uint8_t data_len, uint8_t *data_arr)
{
sht3x_sensor_t *sens = (sht3x_sensor_t *) sensor;
esp_err_t ret = i2c_bus_read_bytes(sens->i2c_dev, NULL_I2C_MEM_ADDR, data_len, data_arr);
return ret;
}
static uint8_t CheckCrc8(uint8_t *const message, uint8_t initial_value)
{
uint8_t crc;
int i = 0, j = 0;
crc = initial_value;
for (j = 0; j < 2; j++) {
crc ^= message[j];
for (i = 0; i < 8; i++) {
if (crc & 0x80) {
crc = (crc << 1) ^ 0x31; /*!< 0x31 is Polynomial for 8-bit CRC checksum*/
} else {
crc = (crc << 1);
}
}
}
return crc;
}
esp_err_t sht3x_measure_period(bool set, uint16_t *min_delay)
{
static uint16_t s_min_delay = 250;
//get value
if (!set) {
*min_delay = s_min_delay;
return ESP_OK;
}
//set value
uint8_t delay_mode = *min_delay >> 8;
switch (delay_mode) {
case 0x20:
s_min_delay = 2000;
break;
case 0x21:
s_min_delay = 1000;
break;
case 0x22:
s_min_delay = 500;
break;
case 0x24:
s_min_delay = 250;
break;
case 0x27:
s_min_delay = 100;
break;
default:
s_min_delay = 250;
break;
}
return ESP_OK;
}
esp_err_t sht3x_get_humiture(sht3x_handle_t sensor, float *Tem_val, float *Hum_val)
{
uint8_t buff[6];
uint16_t tem, hum;
float Temperature = 0;
float Humidity = 0;
sht3x_write_cmd(sensor, READOUT_FOR_PERIODIC_MODE); /*!< if you want to read data just onetime, Comment this code*/
sht3x_get_data(sensor, 6, buff);
/* check crc */
if (CheckCrc8(buff, 0xFF) != buff[2] || CheckCrc8(&buff[3], 0xFF) != buff[5]) {
return ESP_FAIL;
}
tem = (((uint16_t)buff[0] << 8) | buff[1]);
Temperature = (175.0 * (float)tem / 65535.0 - 45.0) ; /*!< T = -45 + 175 * tem / (2^16-1), this temperature conversion formula is for Celsius °C */
//Temperature= (315.0*(float)tem/65535.0-49.0) ; /*!< T = -45 + 175 * tem / (2^16-1), this temperature conversion formula is for Fahrenheit °F */
hum = (((uint16_t)buff[3] << 8) | buff[4]);
Humidity = (100.0 * (float)hum / 65535.0); /*!< RH = hum*100 / (2^16-1) */
if ((Temperature >= -20) && (Temperature <= 125) && (Humidity >= 0) && (Humidity <= 100)) {
*Tem_val = Temperature;
*Hum_val = Humidity;
return ESP_OK; /*!< here is mesurement range */
} else {
return ESP_FAIL;
}
}
esp_err_t sht3x_get_single_shot(sht3x_handle_t sensor, float *Tem_val, float *Hum_val)
{
uint8_t buff[6];
uint16_t tem, hum;
static float Temperature = 0;
static float Humidity = 0;
static int64_t last_shot_time = 0;
int64_t current_time = esp_timer_get_time();
uint16_t min_delay = 0;
int64_t min_delay_us = 0;
sht3x_measure_period(false, &min_delay);
min_delay_us = min_delay * 1000;
if ((current_time - last_shot_time) < min_delay_us) {
*Tem_val = Temperature;
*Hum_val = Humidity;
return ESP_OK;
}
esp_err_t ret = sht3x_get_data(sensor, 6, buff);
/* check crc */
if (ret != ESP_OK || CheckCrc8(buff, 0xFF) != buff[2] || CheckCrc8(&buff[3], 0xFF) != buff[5]) {
return ESP_FAIL;
}
last_shot_time = current_time;
tem = (((uint16_t)buff[0] << 8) | buff[1]);
Temperature = (175.0 * (float)tem / 65535.0 - 45.0) ; /*!< T = -45 + 175 * tem / (2^16-1), this temperature conversion formula is for Celsius °C */
//Temperature= (315.0*(float)tem/65535.0-49.0) ; /*!< T = -45 + 175 * tem / (2^16-1), this temperature conversion formula is for Fahrenheit °F */
hum = (((uint16_t)buff[3] << 8) | buff[4]);
Humidity = (100.0 * (float)hum / 65535.0); /*!< RH = hum*100 / (2^16-1) */
if ((Temperature >= -20) && (Temperature <= 125) && (Humidity >= 0) && (Humidity <= 100)) {
*Tem_val = Temperature;
*Hum_val = Humidity;
return ESP_OK;
} else {
return ESP_FAIL;
}
}
esp_err_t sht3x_soft_reset(sht3x_handle_t sensor)
{
esp_err_t ret = sht3x_write_cmd(sensor, SOFT_RESET_CMD);
return ret;
}
esp_err_t sht3x_stop_periodic(sht3x_handle_t sensor)
{
esp_err_t ret = sht3x_write_cmd(sensor, SHT3x_STOP_PERIODIC);
return ret;
}
esp_err_t sht3x_art(sht3x_handle_t sensor)
{
esp_err_t ret = sht3x_write_cmd(sensor, SHT3x_ART_CMD);
return ret;
}
esp_err_t sht3x_set_measure_mode(sht3x_handle_t sensor, sht3x_cmd_measure_t sht3x_measure_mode)
{
esp_err_t ret = sht3x_write_cmd(sensor, sht3x_measure_mode);
sht3x_measure_period(true, (uint16_t *)&sht3x_measure_mode);
return ret;
}
esp_err_t sht3x_heater(sht3x_handle_t sensor, sht3x_cmd_measure_t sht3x_heater_condition)
{
esp_err_t ret = sht3x_write_cmd(sensor, sht3x_heater_condition);
return ret;
}
#ifdef CONFIG_SENSOR_HUMITURE_INCLUDED_SHT3X
static sht3x_handle_t sht3x = NULL;
static bool is_init = false;
esp_err_t humiture_sht3x_init(i2c_bus_handle_t i2c_bus)
{
if (is_init || !i2c_bus) {
return ESP_FAIL;
}
sht3x = sht3x_create(i2c_bus, SHT3x_ADDR_PIN_SELECT_VSS);
if (!sht3x) {
return ESP_FAIL;
}
esp_err_t ret = sht3x_set_measure_mode(sht3x, SHT3x_PER_4_MEDIUM); /**medium accuracy/repeatability with 250ms period (1000ms/4)**/
if (ret != ESP_OK) {
return ESP_FAIL;
}
is_init = true;
return ESP_OK;
}
esp_err_t humiture_sht3x_deinit(void)
{
if (!is_init) {
return ESP_FAIL;
}
esp_err_t ret = sht3x_delete(&sht3x);
if (ret != ESP_OK) {
return ESP_FAIL;
}
is_init = false;
return ESP_OK;
}
esp_err_t humiture_sht3x_test(void)
{
if (!is_init) {
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t humiture_sht3x_acquire_humidity(float *h)
{
if (!is_init) {
return ESP_FAIL;
}
float temperature = 0;
float humidity = 0;
esp_err_t ret = sht3x_get_single_shot(sht3x, &temperature, &humidity);
if (ret == ESP_OK) {
*h = humidity;
return ESP_OK;
}
*h = 0;
return ESP_FAIL;
}
esp_err_t humiture_sht3x_acquire_temperature(float *t)
{
if (!is_init) {
return ESP_FAIL;
}
float temperature = 0;
float humidity = 0;
esp_err_t ret = sht3x_get_single_shot(sht3x, &temperature, &humidity);
if (ret == ESP_OK) {
*t = temperature;
return ESP_OK;
}
*t = 0;
return ESP_FAIL;
}
#endif

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "sht3x_test.c"
INCLUDE_DIRS .
REQUIRES unity sht3x bus)

View File

@@ -0,0 +1,5 @@
#
#Component Makefile
#
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View File

@@ -0,0 +1,73 @@
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "unity.h"
#include "driver/i2c.h"
#include "i2c_bus.h"
#include "esp_system.h"
#include "sht3x.h"
#define I2C_MASTER_SCL_IO (gpio_num_t)22 /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO (gpio_num_t)21 /*!< gpio number for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
static i2c_bus_handle_t i2c_bus = NULL;
static sht3x_handle_t sht3x = NULL;
/**
* @brief i2c master initialization
*/
static void sht3x_init_test()
{
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_bus = i2c_bus_create(I2C_MASTER_NUM, &conf);
sht3x = sht3x_create(i2c_bus, SHT3x_ADDR_PIN_SELECT_VSS);
sht3x_set_measure_mode(sht3x, SHT3x_PER_2_MEDIUM); /*!< here read data in periodic mode*/
}
static void sht3x_deinit_test()
{
sht3x_delete(&sht3x);
i2c_bus_delete(&i2c_bus);
}
void sht3x_get_data_test()
{
float Tem_val, Hum_val;
int cnt = 10;
while (cnt--) {
if (sht3x_get_humiture(sht3x, &Tem_val, &Hum_val) == 0) {
printf("temperature %.2f°C ", Tem_val);
printf("humidity:%.2f %%\n", Hum_val);
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
TEST_CASE("Sensor sht3x test", "[sht3x][iot][sensor]")
{
sht3x_init_test();
vTaskDelay(1000 / portTICK_RATE_MS);
sht3x_get_data_test();
sht3x_deinit_test();
}