WIP
This commit is contained in:
@ -76,8 +76,8 @@ void app_main(void)
|
||||
};
|
||||
i2c_master_bus_handle_t i2c_bus_handle;
|
||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||
zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
|
||||
#endif
|
||||
zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
dht_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
|
26
zh_dht.c
26
zh_dht.c
@ -65,8 +65,8 @@ esp_err_t zh_dht_init(const zh_dht_init_config_t *config)
|
||||
ESP_LOGE(TAG, "DHT initialization fail. Sensor not connected or not responded.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
ESP_LOGI(TAG, "DHT initialization success. I2C connection.");
|
||||
#endif
|
||||
ESP_LOGI(TAG, "DHT initialization success. I2C connection.");
|
||||
}
|
||||
_is_initialized = true;
|
||||
return ESP_OK;
|
||||
@ -169,12 +169,10 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
|
||||
esp_err_t esp_err = ESP_OK;
|
||||
uint8_t dht_data[I2C_DATA_SIZE] = {0};
|
||||
uint8_t read_command[] = {I2C_DATA_READ_COMMAND};
|
||||
uint8_t wakeup_command = {0};
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||
i2c_master_start(i2c_cmd_handle);
|
||||
i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, wakeup_command, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, false);
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
@ -186,13 +184,15 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
|
||||
i2c_cmd_handle = i2c_cmd_link_create();
|
||||
i2c_master_start(i2c_cmd_handle);
|
||||
i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, read_command[0], true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, read_command[1], true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, read_command[2], true);
|
||||
for (uint8_t i = 0; i < sizeof(read_command); ++i)
|
||||
{
|
||||
i2c_master_write_byte(i2c_cmd_handle, read_command[i], true);
|
||||
}
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
#else
|
||||
uint8_t wakeup_command = {0};
|
||||
esp_err = i2c_master_transmit(_dht_handle, &wakeup_command, sizeof(wakeup_command), 1000 / portTICK_PERIOD_MS);
|
||||
if (esp_err != ESP_OK)
|
||||
{
|
||||
@ -210,14 +210,10 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
|
||||
i2c_cmd_handle = i2c_cmd_link_create();
|
||||
i2c_master_start(i2c_cmd_handle);
|
||||
i2c_master_write_byte(i2c_cmd_handle, I2C_ADDRESS << 1 | I2C_MASTER_READ, true);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[0], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[1], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[2], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[3], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[4], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[5], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[6], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[7], I2C_MASTER_NACK);
|
||||
for (uint8_t i = 0; i < sizeof(dht_data); ++i)
|
||||
{
|
||||
i2c_master_read_byte(i2c_cmd_handle, &dht_data[i], i == (sizeof(dht_data) - 1) ? I2C_MASTER_NACK : I2C_MASTER_ACK);
|
||||
}
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
|
Reference in New Issue
Block a user