Replies: 12 comments
-
Okay, got it. It is not possible to init 2 sensors on the same thread correct? You have to use multiple threads/tasks. |
Beta Was this translation helpful? Give feedback.
-
Hi @tilman, have you made any progress with this problem? Thank you for answer |
Beta Was this translation helpful? Give feedback.
-
Hi @colesnicov! I added an example of working with several I2C devices on same bus (BMP280 and PCF8574), see examples/simplest_barometer. |
Beta Was this translation helpful? Give feedback.
-
Я заметил, что в подпрограммах i2cdev есть семафор, чтобы предотвратить что? Несанкционированный доступ к линии I2C при связи с другим устройством? Я задавался вопросом об этом, и что мне, что, возможно, нет необходимости.? Когда я прочитал это, и здесь, а также после некоторого общения здесь, я не уверен, что это необходимо. Что вы думаете? Это правильно или необходимо? Поскольку i2cdev использует собственный (esp-idf) драйвер I2C, который не является потокобезопасным, но безопасным для задач. Я еще не смотрел, как то, что представляет собой функцию port_ENTER_CRITICAL, но оно должно быть достаточно. Это также зависит от реализации... Я хотел бы понять это прежде, чем обернуть это в "коробку" .. |
Beta Was this translation helpful? Give feedback.
-
Да, в i2cdev используются два типа мьютексов: портовые и транзакционные. Портовые мьютексы (их всего два, по количеству портов I2C) необходимы, чтобы избежать проблем в ситуации, упомянутой в документации:
Они захватываются непосредственно перед выполнением одиночных I2C-транзакций и освобождаются сразу после них. Мьютексы второго типа создаются по одному на устройство и являются необходимыми в случае, если одно то же устройство используется в нескольких задачах: static i2c_dev_t some_dev;
void task1 (void *arg)
{
// some_dev used here
}
void task2 (void *arg)
{
// and here some_dev used too
} В подавляющем большинстве случаев это не несет никаких проблем (если есть портовые мьютексы), но иногда это критически важно: например, если одна операция с устройством требует выполнения нескольких I2C транзакций подряд. SPI-транзакции, в отличие от I2C полностью потокобезопасны. Yes, i2cdev uses two types of mutexes: port and transactional. Port mutexes (there are only two of them, by the number of I2C ports) are necessary to avoid problems in the situation mentioned in the documentation:
They are taken before executing single I2C transactions and are released immediately after them. Mutexes of the second type are created one per device and are necessary if the same device is used in several tasks: static i2c_dev_t some_dev;
void task1 (void * arg)
{
// some_dev used here
}
void task2 (void * arg)
{
// and here some_dev used too
} In the vast majority of cases, this does not pose any problems (if there are port mutexes), but sometimes it is critically important: for example, if single device operation requires several I2C transactions in a row. SPI transactions, unlike I2C, are completely thread safe. |
Beta Was this translation helpful? Give feedback.
-
I leave this thread open. |
Beta Was this translation helpful? Give feedback.
-
Hello friends, Can i connect 2 o 3 BMP280 sensors on the same port of I2C? |
Beta Was this translation helpful? Give feedback.
-
You either need to use different addresses on these sensors and connect them to the common SDA and SCL GPIOs, or you have to use separate GPIOs for these sensors, for example: // First sensor
#define SENSOR0_SDA_GPIO 16
#define SENSOR0_SCL_GPIO 17
// Second sensor
#define SENSOR1_SDA_GPIO 21
#define SENSOR1_SCL_GPIO 22
...
bmp280_t dev0, dev1;
memset(&dev0, 0, sizeof(bmp280_t));
memset(&dev1, 0, sizeof(bmp280_t));
// Init first sensor
ESP_ERROR_CHECK(bmp280_init_desc(&dev0, BMP280_I2C_ADDRESS_0, 0, SENSOR0_SDA_GPIO, SENSOR0_SCL_GPIO));
ESP_ERROR_CHECK(bmp280_init(&dev0, ¶ms0));
// Init second sensor
ESP_ERROR_CHECK(bmp280_init_desc(&dev1, BMP280_I2C_ADDRESS_0, 0, SENSOR1_SDA_GPIO, SENSOR1_SCL_GPIO));
ESP_ERROR_CHECK(bmp280_init(&dev1, ¶ms1));
....
// Use sensors
if (bmp280_read_float(&dev0, &temperature, &pressure, &humidity) == ESP_OK)
printf("Sensor0 pressure: %.2f Pa, temperature: %.2f C", pressure, temperature);
if (bmp280_read_float(&dev1, &temperature, &pressure, &humidity) == ESP_OK)
printf("Sensor1 pressure: %.2f Pa, temperature: %.2f C", pressure, temperature); |
Beta Was this translation helpful? Give feedback.
-
Uncle Rus, thanks for your help! I'm doing a project, have to connect 3 BMP280 sensors at most in one Esp32 (slave), but, maybe 2 or 1 sensors too and send the information about temperature and pressure to another Esp32 (master). I was thinking an I2C connection between all, is it possible? I don't want use wifi connection. |
Beta Was this translation helpful? Give feedback.
-
Hi,
If my two devices work in the same or maybe different threads, the should use the same In @UncleRus example in "examples/simplest_barometer" is see that Important question: Should two physical seinsor/actuator I²C devices on the same I²C port use the same instance of Thanks |
Beta Was this translation helpful? Give feedback.
-
I answered the question myself (partly). There is the macro |
Beta Was this translation helpful? Give feedback.
-
Hello friends, I have a project that I must carry out connecting two dht11 temperature sensors to the same ESP32. Could you give me any suggestions to start? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to use multiple sensors with the same port? I tried to use a bmp280 and a tsl2561 at the same i2c port but always got errors during init. Using them standalone worked fine. Would be great if you could provide an example of using 2 sensors.
Beta Was this translation helpful? Give feedback.
All reactions