#include <LiquidCrystal.h>
#include "Arduino_FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "DHT.h"
#define DHT_PIN 2
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
volatile float temperature, humidity;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
SemaphoreHandle_t xMutex;
void setup() {
// put your setup code here, to run once:
// put your main code here, to run repeatedly:
Serial.begin(9600);
lcd.begin(16, 2);
// you can now interact with the LCD, e.g.:
lcd.print("Hello World!");
dht.begin();
xTaskCreate(Task1,"Task-1", 400, NULL, 2, NULL);
xTaskCreate(Task2,"Task-2", 200, NULL, 1, NULL);
xTaskCreate(Idle,"Idle_Task", 100, NULL, 0, NULL);
xMutex = xSemaphoreCreateMutex();
vTaskStartScheduler();
}
void loop() {
}
void Task1()
{
while(1)
{
Serial.println("In Task-1");
float local_temp = dht.readTemperature();
float local_humidity = dht.readHumidity();
temperature = local_temp;
humidity = local_humidity;
vTaskDelay(10);
}
}
void Task2()
{
while(1)
{
Serial.println("In Task-2");
xSemaphoreTake(xMutex, portMAX_DELAY);
lcd.clear();
lcd.print("Temp is ");
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print("Hum is ");
lcd.print(humidity);
xSemaphoreGive(xMutex);
vTaskDelay(200);
}
}
void Idle()
{
while(1)
{
Serial.println("In Idle Task");
xSemaphoreTake(xMutex, portMAX_DELAY);
lcd.clear();
lcd.print("Welcome to PV2");
lcd.setCursor(0,1);
lcd.print("PlantCare Active");
xSemaphoreGive(xMutex);
vTaskDelay(1000);
}
}