Files
fastbee/firmware/esp-idf/wumei-smart-firmware/main/i2c_temp.c
2021-07-13 17:15:20 +08:00

82 lines
2.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************
* 作者kerwincui
* 时间2021-06-08
* 邮箱164770707@qq.com
* 源码地址https://gitee.com/kerwincui/wumei-smart
* author: kerwincui
* create: 2021-06-08
* email164770707@qq.com
* source:https://github.com/kerwincui/wumei-smart
******************************************************************************/
#include "i2c_temp.h"
#include "common.h"
#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)IO_SENSOR_SCL /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO (gpio_num_t)IO_SENSOR_SDA /*!< 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()
{
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*/
}
void sht3x_deinit()
{
sht3x_delete(&sht3x);
i2c_bus_delete(&i2c_bus);
}
static void sht3x_get_data()
{
float Tem_val, Hum_val;
int cnt = 1000;
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(3000 / portTICK_RATE_MS);
}
}
void i2c_temp_start(void)
{
sht3x_init();
// vTaskDelay(1000 / portTICK_RATE_MS);
// sht3x_get_data();
// sht3x_deinit();
}
//获取空气温湿度
void get_temp_humi(){
if (sht3x_get_humiture(sht3x, &air_temperature, &air_humidity) == 0) {
printf("temperature %.2f°C ", air_temperature);
printf("humidity:%.2f %%\n", air_humidity);
}
}