/*
Usando FreeRTOS con ESP32
FreeRTOS queue
https://www.freertos.org/Embedded-RTOS-Queues.html
*/
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
constexpr uint8_t DHT_PIN = 4;
constexpr uint8_t DHT_TYPE = DHT22;
QueueHandle_t structQueue;
struct sensorValues {
float temperature;
float humidity;
};
LiquidCrystal_I2C lcd{0x27, 16, 2};
DHT dht{DHT_PIN, DHT_TYPE};
void TaskReadSensor(void* pvParameters) {
(void)pvParameters;
for (;;) {
struct sensorValues currentSensorValues;
currentSensorValues.temperature = dht.readTemperature();
currentSensorValues.humidity = dht.readHumidity();
// Post an item on a queue
// https://www.freertos.org/a00117.html
xQueueSend(structQueue, ¤tSensorValues, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void TaskSerial(void* pvParameters) {
(void)pvParameters;
for (;;) {
struct sensorValues currentSensorValues;
// Read an item from a queue
// https://www.freertos.org/a00118.html
if (xQueueReceive(structQueue, ¤tSensorValues, portMAX_DELAY) == pdPASS) {
Serial.printf("Temperatura: %f \r\n", currentSensorValues.temperature);
Serial.printf("Humedad: %f \r\n", currentSensorValues.humidity);
}
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void TaskLiquidCrystal(void* pvParameters) {
(void)pvParameters;
for (;;) {
struct sensorValues currentSensorValues;
if (xQueueReceive(structQueue, ¤tSensorValues, portMAX_DELAY) == pdPASS) {
lcd.setCursor(0, 0);
lcd.print("Temperatura:");
lcd.print(currentSensorValues.temperature);
lcd.setCursor(0, 1);
lcd.print("Humedad:");
lcd.print(currentSensorValues.humidity);
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
dht.begin();
// Create a queue
// https://www.freertos.org/a00116.html
structQueue = xQueueCreate(10, sizeof(struct sensorValues));
if (structQueue != NULL) {
xTaskCreate(TaskSerial, "Serial", 512, NULL, 1, NULL);
xTaskCreate(TaskReadSensor, "DHT Sensor", 1000, NULL, 2, NULL);
xTaskCreate(TaskLiquidCrystal, "LCD", 512, NULL, 1, NULL);
}
}
void loop() {
}