Greenhouse Climate Observation Module

Dependencies:   BSP_DISCO_F746NG DHT

Revision:
3:c21057907d21
Parent:
2:739ba85b7a90
Child:
4:accd2aab9abf
--- a/main.cpp	Fri Jan 31 10:32:08 2020 +0000
+++ b/main.cpp	Wed Feb 05 12:15:21 2020 +0000
@@ -5,34 +5,47 @@
 
 Brief description:
     Microcontroller observing the climate within a greenhouse, and displaying warnings/information as needed.
-    Sensors monitor light levels, as well as temperature and humidity.
-    To simulate an actual greenhouse, LEDs will be used in place of actual influences.
-
-Objectives:
-- Able to set controller location (Which section/greenhouse is it located in)
-- Able to open window at certain temperature tresholds (For 5 minutes)
-- Water plants at regular intervals (Twice a day)
-- Plants must have light for at least 12 hours. If weather is cloudy, artificial lights.
-- Testmode: Alternate time-tracking system, much faster than 24 hour system.
-
-REMEMBER TO RETURN BORROWED LED SOCKETS
+    Sensors monitor temperature and humidity.
+    To simulate an actual greenhouse, LEDs will be used in place of actual enviromental influences.
+    Simulator mode has been added for debugging and presentation purposes.
 */
 
 #include "mbed.h" // Mbed header from mbed-os version 5
 #include "stm32746g_discovery_lcd.h" // Last updated 27/11/2019
 #include "DHT.h" // Author: Wim De Roeve
+#include <iostream>
 #include "bootup.h" // Author: Christian Andresen
 
 // Call Bootup class from bootup.h as LCD_Start
 bootup LCD_Start;
 
 /* Inputs */
+AnalogIn Temp_Sensor(A0);
+DHT Humid_Sensor(A1, DHT22);
+InterruptIn simbutton(D2);
 
 /* Outputs */
-DigitalOut Light_LED(D2);
+DigitalOut Temp_LED(D3); // Red LED
+DigitalOut Humid_LED(D4); // Blue LED
+DigitalOut Light_LED(D5); // Yellow LED
+DigitalOut Water_LED(D6); // Blue LED
+DigitalOut Window_LED(D7); // Green LED
 
 /* Multithreads */
 Thread tTimeCounter;
+Thread tLocation_Select;
+Thread tTemp;
+Thread tHumid;
+Thread tTemp_Check;
+Thread tHumid_Check;
+
+/* SIMULATION MODE */
+bool simmode = 1;
+void Sim_Toggle();
+
+/* Device location */
+void Location_Select();
+int location_id;
 
 /* Day & Night */
 void TimeCounter();
@@ -41,29 +54,90 @@
 int timecount = 0;
 bool timeofday = 1;
 
+/* Temp and Humidity */
+void Temp_Display(); // Displays temperatures in Celsius on screen
+void Humid_Display(); // Displays humidity in % on screen
+void Temp_Check();
+void Humid_Check();
+float temp;
+float humid;
+
+/* Greenhouse mechanisms */
+void Vent_Air(); 
+void Heat_Lamp();
+void Dehumidifier();
+void Spray_Water();
+
+uint8_t text[30]; // Used to display values with added text.
+uint8_t text2[30]; // Used to display values with added text.
+
+void startup()
+{
+    cout << "Enter Greenhouse ID: \n \r";
+    cin >> location_id;
+    cout << "Greenhouse ID is: " << location_id;
+}
 
 int main()
 {
+    startup();
     LCD_Start.LCD_Bootup();
     tTimeCounter.start(&TimeCounter);
+    tLocation_Select.start(&Location_Select);
+    simbutton.rise(&Sim_Toggle);
+    tTemp.start(&Temp_Display);
+    tHumid.start(&Humid_Display);
+    tTemp_Check.start(&Temp_Check);
+    tHumid_Check.start(&Humid_Check);
     while(1) {}
 }
 
+void Sim_Toggle()
+{
+    simmode = !simmode;
+}
+
+void Location_Select()
+{
+    while(1) {
+        sprintf((char*)text2, "Greenhouse %d", location_id);
+        BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)&text2, LEFT_MODE);
+        if(simmode) {
+            BSP_LCD_DisplayStringAt(0, 25, (uint8_t *) "SIM MODE", LEFT_MODE);
+        } else if(simmode == 0) {
+            BSP_LCD_DisplayStringAt(0, 25, (uint8_t *) "        ", LEFT_MODE);
+        }
+    }
+}
+
 void TimeCounter()
 {
     // Timecounter counts up to one cycle of 720 minutes (Sim 7.20 secs)
     while(1) {
-        timecount++;
-        wait(0.01);
-        printf("%d \n \r", timecount);
-        if (timecount == 721) {
-            timeofday = !timeofday;
-            printf("Time of day is %d \n \r", timeofday);
-            timecount = 0;
-            if (timeofday) {
-                DayMode();
-            } else {
-                NightMode();
+        if(simmode) {
+            timecount++;
+            wait(0.01);
+            if (timecount == 721) {
+                timeofday = !timeofday;
+                printf("Time of day is %d \n \r", timeofday);
+                timecount = 0;
+                if (timeofday) {
+                    DayMode();
+                } else {
+                    NightMode();
+                }
+            }
+        } else if(simmode == 0) {
+            timecount++;
+            wait(1);
+            if (timecount == 721) {
+                timeofday = !timeofday;
+                timecount = 0;
+                if (timeofday) {
+                    DayMode();
+                } else {
+                    NightMode();
+                }
             }
         }
     }
@@ -74,8 +148,7 @@
     BSP_LCD_Clear(LCD_COLOR_WHITE);
     BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
     BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
-    BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "Time: Day", CENTER_MODE);
-    Light_LED = 0;
+    BSP_LCD_DisplayStringAt(0, 0, (uint8_t *) "  Time: Day  ", RIGHT_MODE);
 }
 
 void NightMode()
@@ -83,6 +156,107 @@
     BSP_LCD_Clear(LCD_COLOR_BLACK);
     BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
     BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-    BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "Time: Night", CENTER_MODE);
+    BSP_LCD_DisplayStringAt(0, 0, (uint8_t *) " Time: Night ", RIGHT_MODE);
+}
+
+void Temp_Display()
+{
+    while(1) {
+        // Monitor temperatures and display results on LCD screen
+        sprintf((char*)text, "Temperature: %.1f C   ", temp);
+        BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)&text, CENTER_MODE);
+        wait(1);
+    }
+}
+
+void Humid_Display()
+{
+    while(1) {
+        // Monitor humidity and display results on LCD screen
+        sprintf((char*)text, "Humidity: %.1f   ", humid);
+        BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)&text, CENTER_MODE);
+        wait(1);
+    }
+}
+
+void Temp_Check()
+{
+    while(1) {
+        temp = Temp_Sensor.read() * 100 / 2; // Read temperature sensor input. Since this is flawed, correct to adjust.
+        if(timeofday) { // If it's daytime, keep temps between 25 and 30 celsius
+            if (temp >= 25 && temp <= 30) {
+                printf("Greenhouse temperature within acceptable parameters");
+                wait(1);
+            }  else if (temp < 25) {
+                Heat_Lamp(); // Raise temperature by turning on the heat lamps in the greenhouse
+            } else if (temp > 30) {
+                Vent_Air(); // Reduce temperature by venting the hot air out of the greenhouse
+            }
+        } else if(timeofday == false) { // If it's nighttime, keep temps between 15 and 20
+            if (temp >= 15 && temp <= 20) {
+                Temp_LED = 1;
+                wait(1);
+            } else if (temp < 15) {
+                Heat_Lamp();
+
+            } else if (temp > 20) {
+                Vent_Air();
+            }
+        }
+    }
+}
+
+void Humid_Check()
+{
+    while(1) {
+        humid = Humid_Sensor.ReadHumidity();
+        int dataread = Humid_Sensor.readData(); // No idea why, but the humidity won't display on screen without it.
+        if (humid >= 55 && humid <= 65) { // Check if humidity is within acceptable parameters (55% - 65%)
+            printf("Greenhouse humidity is within acceptable parameters");
+        } else if (humid < 55) {
+            Spray_Water(); // Raise humidity by spraying water into the plants and greenhouse.
+        } else if ( humid > 65) {
+            Dehumidifier(); // Lower humidity by dehumidifying the greenhouse.
+        }
+    }
+}
+
+void Vent_Air()
+{
+    Window_LED = 1;
+    wait(1);
+    Window_LED = 0;
+    if(simmode) {
+        printf("Greenhouse temperature above sustainable levels. Venting air from Greenhouse to rectify. \n \r");
+    }
+}
+
+void Heat_Lamp()
+{
     Light_LED = 1;
+    wait(1);
+    Light_LED = 0;
+    if(simmode) {
+        printf("Greenhouse temperature below acceptable levels. Turning on heat lamps to rectify. \n \r");
+    }
+}
+
+void Dehumidifier()
+{
+    Water_LED = 1;
+    wait(0.5);
+    Water_LED = 0;
+    if(simmode) {
+        printf("Greenhouse humidity above acceptable levels. Dehumidifying to rectify. \n \r");
+    }
+}
+
+void Spray_Water()
+{
+    Water_LED = 1;
+    wait(1);
+    Water_LED = 0;
+    if(simmode) {
+        printf("Greenhouse humidity below acceptable levels. Spraying water to rectify. \n \r");
+    }
 }
\ No newline at end of file