This commit is contained in:
2024-06-22 17:07:14 +03:00
parent 8c3d60d8fd
commit daf75ec9d6
3 changed files with 56 additions and 27 deletions

View File

@ -82,7 +82,7 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
}
if (_is_initialized == false)
{
ESP_LOGE(TAG, "DHT read fail. BH1750 not initialized.");
ESP_LOGE(TAG, "DHT read fail. DHT not initialized.");
return ESP_ERR_NOT_FOUND;
}
if (_init_config.sensor_pin != 0xFF)
@ -171,13 +171,27 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
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, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
// i2c_master_write_byte(i2c_cmd_handle, _command, 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);
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_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);
if (esp_err != ESP_OK)
{
ESP_LOGE(TAG, "DHT read fail. I2C driver error.");
return esp_err;
}
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);
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
esp_err = i2c_master_transmit(_dht_handle, &wakeup_command, sizeof(wakeup_command), 1000 / portTICK_PERIOD_MS);
if (esp_err != ESP_OK)
@ -193,14 +207,20 @@ esp_err_t zh_dht_read(float *humidity, float *temperature)
return esp_err;
}
#ifdef CONFIG_IDF_TARGET_ESP8266
// i2c_cmd_handle = i2c_cmd_link_create();
// i2c_master_start(i2c_cmd_handle);
// i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true);
// i2c_master_read_byte(i2c_cmd_handle, &sensor_data[0], I2C_MASTER_ACK);
// i2c_master_read_byte(i2c_cmd_handle, &sensor_data[1], I2C_MASTER_NACK);
// 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);
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);
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
esp_err = i2c_master_receive(_dht_handle, dht_data, sizeof(dht_data), 1000 / portTICK_PERIOD_MS);
#endif