The code above is sensor read:
QueueHandle_t qSensor_Display_A;
QueueHandle_t qSensor_Display_B;
QueueHandle_t qSensor_Display_C;
QueueHandle_t qSensor_Display_D;
QueueHandle_t qSensor_MQTT_A;
QueueHandle_t qSensor_MQTT_B;
QueueHandle_t qSensor_MQTT_D;
QueueHandle_t qSensor_MQTT_C;
QueueHandle_t qSensor_Datalogger_A;
QueueHandle_t qSensor_Datalogger_B;
QueueHandle_t qSensor_Datalogger_C;
QueueHandle_t qSensor_Datalogger_D;
QueueHandle_t qSensor_Alarme_A;
QueueHandle_t qSensor_Alarme_B;
QueueHandle_t qSensor_Alarme_C;
QueueHandle_t qSensor_Alarme_D;
TaskHandle_t hDisplayTempDesc = NULL;
TaskHandle_t hLerSensor = NULL;
//extern void f_DisplayTempDesc(void *parameters);
#define TEMP_BUFFER_SIZE 10
#define DS18B20_PIN 22
#define AM2301_PIN 21
#define SENSOR_DISCONNECTED_TEMP -127.00
#define TAG "SensorTask"
//static ds18b20_device_handle_t ds18b20_handle;
#define MAX_SENSORS 2 // Número máximo de sensores DS18B20
static ds18b20_device_handle_t ds18b20_handles[MAX_SENSORS];
static int sensor_count = 0;
// char* f_temperatura() {
// float tempReceived;
// if (xQueueReceive(qSensor_Display, &tempReceived, pdMS_TO_TICKS(100))) {
// static char tempStr[TEMP_BUFFER_SIZE];
// snprintf(tempStr, TEMP_BUFFER_SIZE, "%.2f", tempReceived);
// return tempStr;
// }
// return NULL;
// }
void f_startLerSensor(){
xTaskCreatePinnedToCore(v_LerSensor, "v_LerSensor", 2400, NULL, tskIDLE_PRIORITY + 1, &hLerSensor, PRO_CPU_NUM);
xTaskCreatePinnedToCore(f_LerSensorAM2301, "f_LerSensorAM2301", 2400, NULL, tskIDLE_PRIORITY + 1, NULL, PRO_CPU_NUM);
}
void f_startStopLerSensor(){
xTaskCreate(f_stopLerSensor, "f_stopLerSensor", 2400, NULL, tskIDLE_PRIORITY + 10, &hLerSensor);
}
void f_stopLerSensor(){
ESP_LOGI(TAG, "Stop ler sensor");
while(1){
if(hLerSensor!=NULL){
vTaskDelete(hLerSensor);
vTaskDelete(NULL);
}
}
}
#define MAX_SENSORS 2
void v_LerSensor(void *parameters) {
// Criação das filas para cada sensor
qSensor_Display_A = xQueueCreate(1, sizeof(float));
qSensor_Display_B = xQueueCreate(1, sizeof(float));
qSensor_MQTT_A = xQueueCreate(1, sizeof(float));
qSensor_MQTT_B = xQueueCreate(1, sizeof(float));
qSensor_Datalogger_A = xQueueCreate(1, sizeof(float));
qSensor_Datalogger_B = xQueueCreate(1, sizeof(float));
qSensor_Alarme_A = xQueueCreate(1, sizeof(float));
qSensor_Alarme_B = xQueueCreate(1, sizeof(float));
// Verificação das filas
if (!qSensor_Display_A || !qSensor_Display_B ||
!qSensor_MQTT_A || !qSensor_MQTT_B ||
!qSensor_Datalogger_A || !qSensor_Datalogger_B ||
!qSensor_Alarme_A || !qSensor_Alarme_B) {
ESP_LOGE(TAG, "Erro ao criar as filas");
vTaskDelete(NULL);
return;
}
// Configuração do barramento 1-Wire
onewire_bus_handle_t bus = NULL;
onewire_bus_config_t bus_config = {.bus_gpio_num = DS18B20_PIN};
onewire_bus_rmt_config_t rmt_config = {.max_rx_bytes = 10};
ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus));
onewire_device_iter_handle_t iter;
onewire_device_t device;
ESP_ERROR_CHECK(onewire_new_device_iter(bus, &iter));
// Detectar e armazenar sensores encontrados
int sensor_count = 0;
static ds18b20_device_handle_t ds18b20_handles[MAX_SENSORS];
while (sensor_count < MAX_SENSORS && onewire_device_iter_get_next(iter, &device) == ESP_OK) {
ds18b20_config_t ds_cfg = {};
if (ds18b20_new_device(&device, &ds_cfg, &ds18b20_handles[sensor_count]) == ESP_OK) {
ESP_LOGI(TAG, "DS18B20 encontrado: %016llX", device.address);
sensor_count++;
} else {
ESP_LOGW(TAG, "Dispositivo desconhecido: %016llX", device.address);
}
}
ESP_ERROR_CHECK(onewire_del_device_iter(iter));
if (sensor_count < MAX_SENSORS) {
ESP_LOGE(TAG, "Sensores DS18B20 insuficientes detectados");
vTaskDelete(NULL);
return;
}
float temperatures[MAX_SENSORS] = {0};
// Loop principal de leitura dos sensores
while (1) {
for (int i = 0; i < sensor_count; i++) {
// Inicia a conversão de temperatura e realiza a leitura
if (ds18b20_trigger_temperature_conversion(ds18b20_handles[i]) == ESP_OK &&
ds18b20_get_temperature(ds18b20_handles[i], &temperatures[i]) == ESP_OK) {
if (i == 0) {
xQueueOverwrite(qSensor_Display_A, &temperatures[i]);
xQueueOverwrite(qSensor_MQTT_A, &temperatures[i]);
xQueueOverwrite(qSensor_Datalogger_A, &temperatures[i]);
xQueueOverwrite(qSensor_Alarme_A, &temperatures[i]);
//ESP_LOGI(TAG, "Sensor %d - Temperatura: %.2f °C", i, temperatures[i]);
} else if (i == 1) {
xQueueOverwrite(qSensor_Display_B, &temperatures[i]);
xQueueOverwrite(qSensor_MQTT_B, &temperatures[i]);
xQueueOverwrite(qSensor_Datalogger_B, &temperatures[i]);
xQueueOverwrite(qSensor_Alarme_B, &temperatures[i]);
//ESP_LOGI(TAG, "Sensor %d - Temperatura: %.2f °C", i, temperatures[i]);
}
} else {
ESP_LOGE(TAG, "Erro ao ler o sensor %d", i);
}
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void f_LerSensorAM2301(void *args) {
ESP_LOGW(TAG, "Lendo Sensor AM2301 com sucesso");
qSensor_Display_C = xQueueCreate(1, sizeof(float));
qSensor_MQTT_C = xQueueCreate(1, sizeof(float));
qSensor_Datalogger_C = xQueueCreate(1, sizeof(float));
qSensor_Alarme_C = xQueueCreate(1, sizeof(float));
qSensor_Display_D = xQueueCreate(1, sizeof(float));
qSensor_MQTT_D = xQueueCreate(1, sizeof(float));
qSensor_Datalogger_D = xQueueCreate(1, sizeof(float));
qSensor_Alarme_D = xQueueCreate(1, sizeof(float));
float temp = 0.0;
float humid = 0.0;
while(true){
if (dht_read_float_data(DHT_TYPE_AM2301, AM2301_PIN, &humid, &temp) == ESP_OK) {
//ESP_LOGW(TAG, "Temp(%f), Humid(%f)", temp, humid);
xQueueOverwrite(qSensor_Display_C, &temp);
xQueueOverwrite(qSensor_MQTT_C, &temp);
xQueueOverwrite(qSensor_Datalogger_C, &temp);
xQueueOverwrite(qSensor_Alarme_C, &temp);
xQueueOverwrite(qSensor_Display_D, &humid);
xQueueOverwrite(qSensor_MQTT_D, &humid);
xQueueOverwrite(qSensor_Datalogger_D, &humid);
xQueueOverwrite(qSensor_Alarme_D, &humid);
} else {
vTaskDelay(pdMS_TO_TICKS(500));
//ESP_LOGE(TAG, "Falha ao ler o sensor");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelete(NULL);
}