/*
Greenhouse Management System
Inputs:-
Taking analog values from:
1. Soil moisture sensor (simulated using potentiometer, Left = max moisture, Right = min moisture)
2. LDR sensor
3. Temperature sensor
I have converted these analog values of range 0 to 1024 into range 0 to 100 using the max() function for moisture and illumination
Outputs:-
1. Buzzer: when the soil moisture is less than 10%, it plays a tune as an indicator to ON water supply
2. Relay: when the light is below 50%, then it ON the LED by itself. Some plants need artificial light in the absence of sun
The Soil moisture, Illumination, and Temperature are displayed in the terminal in real-time.
I used FreeRTOS to schedule these 3 tasks.
Features:
- Real-time monitoring of concerned environmental conditions of a greenhouse
- When the farmer hears the buzzer he can provide water to the plants
- When illumination is less than required, lights in the greenhouse are ON on their own.
*/
#include <Arduino_FreeRTOS.h>
#include <SPI.h>
#include <Wire.h>
#define OLED_RESET 4
#define pot_in A0
#define ldr_in A1
#define relay_out 3
#define SPEAKER_PIN 8
#define temp_in A2
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
void setup()
{
Serial.begin(9600);
//LEDs' initialization!
pinMode(pot_in, INPUT);
pinMode(ldr_in, INPUT);
pinMode(relay_out, OUTPUT);
pinMode(SPEAKER_PIN, OUTPUT);
int moist=analogRead(pot_in);
int moist_percentage = map (moist, 1024, 0, 0, 25);
Serial.print("Starting Soil moisture:");
Serial.print(moist_percentage);
Serial.print("%");
Serial.println(F(" [Moisture sensor]"));
int light=analogRead(ldr_in);
int light_percentage = map (light, 1024, 0, 0, 100);
Serial.print("Starting Illumination:");
Serial.print(light_percentage);
Serial.print("%");
Serial.println(F(" [Light sensor]"));
int temp=analogRead(temp_in);
float temp_celsius = 1 / (log(1 / (1023. / temp - 1)) / BETA + 1.0 / 298.15) - 273.15;
//int temp_percentage = map (light, 0, 1024, 0, 100);
Serial.print("Starting Temperature:");
Serial.print(temp_celsius);
Serial.println(F(" [Temperature sensor]"));
if (light_percentage <= 50) {
digitalWrite(relay_out, LOW);
}
else if (light_percentage > 50){
digitalWrite(relay_out, HIGH);}
else if (moist_percentage <= 10) {
tone(SPEAKER_PIN, 330);
delay(150);
tone(SPEAKER_PIN, 392);
delay(150);
tone(SPEAKER_PIN, 659);
delay(150);
tone(SPEAKER_PIN, 523);
delay(150);
tone(SPEAKER_PIN, 587);
delay(150);
tone(SPEAKER_PIN, 784);
delay(150);
noTone(SPEAKER_PIN);
}
xTaskCreate(Task_1, "Task no. 1!", 100, NULL, 1, NULL);
xTaskCreate(Task_2, "Task no. 2!", 100, NULL, 2, NULL);
xTaskCreate(Task_3, "Task no. 3!", 100, NULL, 3, NULL);
}
static void Task_1(void* pvParameters) {
while (1) {
int moist=analogRead(pot_in);
int moist_percentage = map (moist, 1024, 0, 0, 25);
Serial.print("Soil moisture:");
Serial.print(moist_percentage);
Serial.print("%");
Serial.println(F(" [Moisture sensor]"));
if (moist_percentage <= 10) {
tone(SPEAKER_PIN, 330);
delay(150);
tone(SPEAKER_PIN, 392);
delay(150);
tone(SPEAKER_PIN, 659);
delay(150);
tone(SPEAKER_PIN, 523);
delay(150);
tone(SPEAKER_PIN, 587);
delay(150);
tone(SPEAKER_PIN, 784);
delay(150);
noTone(SPEAKER_PIN);
}
//else if (light_percentage > 50){
//digitalWrite(relay_out, HIGH);}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
//Task 2
static void Task_2(void* pvParameters) {
while (1) {
int light=analogRead(ldr_in);
int light_percentage = map (light, 0, 1024, 0, 100);
Serial.print("Illumination:");
Serial.print(light_percentage);
Serial.print("%");
Serial.println(F(" [Light sensor]"));
if (light_percentage <= 50) {
digitalWrite(relay_out, HIGH);
}
else if (light_percentage > 50){
digitalWrite(relay_out, LOW);}
vTaskDelay(1100 / portTICK_PERIOD_MS);
}
}
static void Task_3(void* pvParameters) {
while (1) {
int temp=analogRead(temp_in);
float temp_celsius = 1 / (log(1 / (1023. / temp - 1)) / BETA + 1.0 / 298.15) - 273.15;
//int temp_percentage = map (temp, 0, 1024, 0, 100);
Serial.print("Temperature:");
Serial.print(temp_celsius);
//Serial.print("%");
Serial.println(F(" [Temperature sensor]"));
vTaskDelay(1100 / portTICK_PERIOD_MS);
}
}
void loop()
{
}