mirror of
https://github.com/alexandrebobkov/ESP-Nodes.git
synced 2025-10-23 17:33:25 +00:00
esp32 temperature node
This commit is contained in:
@@ -138,66 +138,5 @@ static esp_err_t i2c_driver_initialize(void)
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// CONNECT TO BMP280
|
||||
ESP_ERROR_CHECK(i2c_driver_initialize());
|
||||
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
uint8_t address = 0x76;
|
||||
i2c_cmd_handle_t command = i2c_cmd_link_create();
|
||||
i2c_master_start(command);
|
||||
i2c_master_write_byte(command, (address << 1) | I2C_MASTER_WRITE, I2C_ACKS);// 0x1); // 0x1 -> check ACK from slave
|
||||
i2c_master_stop(command);
|
||||
esp_err_t cmd_ret = i2c_master_cmd_begin(I2C_NUM_0, command, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(command);
|
||||
i2c_driver_delete(I2C_NUM_0);
|
||||
if (cmd_ret == ESP_OK)
|
||||
ESP_LOGI(TAG, "I2C device found at address 0x%X", address);
|
||||
else
|
||||
ESP_LOGI(TAG, "error %X", cmd_ret);
|
||||
|
||||
// REQUEST DEVICE ID
|
||||
// The sequence of commands corresponds to the BME-280 I2C protocol
|
||||
int len = 1; // 1 byte
|
||||
uint8_t *data = malloc(len);
|
||||
//uint8_t address = 0x76;
|
||||
uint8_t register_address = 0xD0; // Register that stores device ID
|
||||
ESP_ERROR_CHECK(i2c_driver_initialize());
|
||||
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
command = i2c_cmd_link_create();
|
||||
// Step 1. START
|
||||
i2c_master_start(command);
|
||||
// Step 2. Send device address, WRITE bit, and wait for acknowledgment
|
||||
i2c_master_write_byte(command, address << 1 | I2C_MASTER_WRITE, 0x1);
|
||||
// Step 3. Specify (write) register address, and wait for acknowledgment
|
||||
i2c_master_write_byte(command, register_address, 0x1);
|
||||
// Step 4. START
|
||||
i2c_master_start(command);
|
||||
// Step 5. Send device address, READ bit, and wait for acknowledgment
|
||||
i2c_master_write_byte(command, address << 1 | I2C_MASTER_READ, 0x1);
|
||||
if (len > 1)
|
||||
i2c_master_read(command, data, len-1, 0x0);
|
||||
// Step 6. Read one byte of data from register
|
||||
i2c_master_read_byte(command, data+len-1, 0x1);
|
||||
// Step 8. STOP
|
||||
i2c_master_stop(command);
|
||||
// Step 7. Read one byte of data from register
|
||||
cmd_ret = i2c_master_cmd_begin(I2C_NUM_0, command, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(command);
|
||||
if (cmd_ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Register read success");
|
||||
for (int i = 0; i < len; i++) {
|
||||
//printf("0x%02x ", data[i]);
|
||||
if (data[i] == 0x60)
|
||||
ESP_LOGI(TAG, "Device ID is 0x%X (BME-280)", data[i]);
|
||||
else if (data[i] == 0x58)
|
||||
ESP_LOGI(TAG, "Device ID is 0x%X (BMP-280)", data[i]);
|
||||
else
|
||||
ESP_LOGI(TAG, "Device ID is 0x%X", data[i]);
|
||||
}
|
||||
} else if (cmd_ret == ESP_ERR_TIMEOUT) {
|
||||
ESP_LOGW(TAG, "Bus is busy");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Read failed");
|
||||
}
|
||||
free(data);
|
||||
i2c_driver_delete(I2C_NUM_0);
|
||||
}
|
Reference in New Issue
Block a user