Skip to content

Instantly share code, notes, and snippets.

@sepfy
Last active February 17, 2020 12:52
Show Gist options
  • Save sepfy/873c98c678747e849fecb06e690b5432 to your computer and use it in GitHub Desktop.
Save sepfy/873c98c678747e849fecb06e690b5432 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
static const char *TAG = "sht31 example";
static i2c_port_t i2c_port = I2C_NUM_0;
#define I2C_MASTER_TX_BUF_DISABLE 0
#define I2C_MASTER_RX_BUF_DISABLE 0
#define WRITE_BIT I2C_MASTER_WRITE
#define READ_BIT I2C_MASTER_READ
#define ACK_CHECK_EN 0x1
#define ACK_CHECK_DIS 0x0
#define ACK_VAL 0x0
#define NACK_VAL 0x1
static esp_err_t sht31_init(void)
{
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = 21;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = 22;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 1000000;
i2c_param_config(i2c_port, &conf);
return i2c_driver_install(i2c_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
static esp_err_t sht31_read_temp_humi(float *temp, float *humi)
{
// See http://wiki.seeedstudio.com/Grove-TempAndHumi_Sensor-SHT31/
// Write 0x00 to address 0x24
unsigned char data_wr[] = {0x24, 0x00};
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x44 << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write(cmd, data_wr, sizeof(data_wr), ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
if (ret != ESP_OK) {
return ret;
}
i2c_cmd_link_delete(cmd);
// Delay 160 ms
vTaskDelay(160 / portTICK_PERIOD_MS);
// Read 6 bytes
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x44 << 1) | READ_BIT, ACK_CHECK_EN);
size_t size = 6;
uint8_t *data_rd = malloc(size);
if (size > 1) {
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
if (ret != ESP_OK) {
return ret;
}
i2c_cmd_link_delete(cmd);
*temp = -45 + (175 *(float)(data_rd[0] * 256 + data_rd[1])/ 65535.0);
*humi = 100 * (float)(data_rd[3] * 256 + data_rd[4]) / 65535.0;
return ESP_OK;
}
void app_main(void)
{
float temp, humi;
ESP_ERROR_CHECK(sht31_init());
while(true) {
ESP_ERROR_CHECK(sht31_read_temp_humi(&temp, &humi));
ESP_LOGI(TAG, "temp = %.2f, humi = %.2f\n", temp, humi);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment