Strawberry_Greenhouse_V1.0

Dependencies:   mbed C12832 Servo HCSR04 DHT DHT11

Revision:
2:2621304bdec1
Parent:
1:81cec44cec2e
Child:
3:fdbd4ffb9ca6
--- a/main.cpp	Tue Oct 19 17:14:52 2021 +0000
+++ b/main.cpp	Tue Oct 19 18:55:00 2021 +0000
@@ -1,23 +1,35 @@
 #include "mbed.h"
-
 #include "C12832.h"
-
 #include "tank.h"
-
 #include "HCSR04.h"
+#include "DHT.h"
+#include "Servo.h"
 
 
 C12832 lcd(p5, p7, p6, p8, p11);        // LCD screen
 PwmOut r (p23);                         // r = red LED
 PwmOut g (p24);                         // g = green LED
+HCSR04 sensor(p9, p20);                 // ultrasonic distance sensor
+DHT sensorB(p16,DHT11);                 // DHT11 temp & humi sensor
+DigitalOut fan(p18);                     //grove relay control DC fan
+DigitalOut myled(p14);                  //assigning greenled as digital output pin14
+AnalogIn ldr(p15);                      //assigning ldr as analog input p15
+AnalogIn soil_moisture(p19);             //soil moisture sensor
+Servo myservo(p21);                      //servo
+
 
 int main()
 
 {
+    while(1)
+    {
+
+
+//water tank level system
 
 //welcome message on LCD
 
-    lcd.cls();
+        lcd.cls();
 
     lcd.locate(25,5);
 
@@ -36,7 +48,6 @@
 
     {
 
-        HCSR04 sensor(p9, p20);
         sensor.setRanges(0, 500);       // water tank level 500mm
 
 
@@ -47,11 +58,11 @@
         //if needed:   lcd.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
         //If needed:   sensor.getMinRange(), sensor.getMaxRange());
 
-        while(true) {
+        
             timer.reset();
             timer.start();
             sensor.startMeasurement();
-            while(!sensor.isNewDataReady()) {
+            /*while(!sensor.isNewDataReady()) {*/
                 // wait for new data
                 // waiting time depends on the distance
             }
@@ -63,7 +74,7 @@
             wait_ms(500 - timer.read_ms()); // time the loop
 
             //turn RGB LED red if distance >400mm
-  
+
             if( sensor.getDistance_mm()< 400) r= 1; //red light on
             else r = 0;
 
@@ -95,14 +106,175 @@
             lcd.copy_to_lcd();
 
 
-        }
-    }
+
+            wait (10.0f);
+
+
+//Temperature Control System
+
+
+            //system welcome message on LCD
+
+            lcd.cls();
+
+            lcd.locate(15,5);
+
+            lcd.printf("Temp & Humidi Level");
+
+            lcd.locate(2,15);
+
+            lcd.printf("Greenhouse Control System");
+
+            wait(2.5);
+
+            lcd.cls();      //clear LCD
 
 
-// https://os.mbed.com/media/uploads/Njbuch/wiley_publishing_-_c_for_dummies_2nd.edition_-2004-.pdf
+            int error = 0;
+            float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
+            lcd.cls();
+            char tmpString[16];
+            int temp_format = 0;
+            int humid_format = 0;
+
+            //while(1)
+            
+
+                wait(0.5f);
+                error = sensorB.readData();
+                if (0 == error) {
+                    c   = sensorB.ReadTemperature(CELCIUS);
+                    f   = sensorB.ReadTemperature(FARENHEIT);
+                    k   = sensorB.ReadTemperature(KELVIN);
+                    h   = sensorB.ReadHumidity();
+                    dp  = sensorB.CalcdewPoint(c, h);
+                    dpf = sensorB.CalcdewPointFast(c, h);
+                    printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
+                    printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
+                    printf("Tempformat: %d HumidFormat: %d\n", temp_format, humid_format);
+                    switch(temp_format) {
+                        case 0 :
+                            sprintf(tmpString, " Temp: %4.2fC", c);
+                            break;
+                        case 1 :
+                            sprintf(tmpString, " Temp: %4.2fF", f);
+                            break;
+                        case 2 :
+                            sprintf(tmpString, " Temp: %4.2fK", k);
+                            break;
+                    
+
+                    //display the results onto the LCD
+                    lcd.locate(0,0);
+                    lcd.printf("Temperature: %4.2fC", c);
+                    lcd.locate(0,10);
+                    lcd.printf("Humidity: %4.2f", h);
+                    wait(2);
+
+                    //turn fan on if temperature is above 26
+                    if(c > 26) fan=1;   //fan is on
+                    else fan=0;         //Fan is off
+
+
+                    //wait 2 seconds and check temp again
+                    wait(10.0);
 
 
 
 
+//Light Sensor System
+
+                    lcd.cls();
+
+                    lcd.locate(25,5);
+
+                    lcd.printf("Light Level");
+
+                    lcd.locate(2,15);
+
+                    lcd.printf("Greenhouse Control System");
+
+                    wait(2.5);
+
+                    lcd.cls();
+
+                    //while(1) 
+                    float ldrout=ldr.read();                        //read ldr value
+                    lcd.locate(10,10);
+                    lcd.printf("Light Level is: %f\n :",ldrout);       //print ldr value on LCD
+                    if(ldrout<0.6) {                                //if ldr value is smaller than 0.6 green LED turns on
+
+                        myled = 1;                                  //LED on
+                    } else {
+                        myled=0;                                    //LED off
+
+                        wait(10.0);
+                        lcd.cls();
+                    
+                }
+
+            }
+        }
+    }
 }
 
+/*
+
+//Soil Moisture System
+
+//lcd.cls();
+
+                    lcd.locate(25,5);
+
+                    lcd.printf("Soil Moisture Level");
+
+                    lcd.locate(2,15);
+
+                    lcd.printf("Greenhouse Control System");
+
+                    wait(2.5);
+
+
+float range = 0.0005;
+    float position = 0.5;
+
+    float moisture_value = 0.0f;
+
+
+    while(1) {
+
+        moisture_value = soil_moisture*100;                                               //soil moisture value
+        lcd.locate(0,0);                                                               //text location on screen
+        lcd.cls();
+        lcd.printf("Moisture reading is: %2.2f\n", moisture_value);                     //display soil moisture value as a percentage
+        wait(1.0f);
+        //  lcd.printf("Moisture reading is: %2.2f\n", value);
+
+        if (moisture_value > 30&& moisture_value <= 50) {                                 //if soil moisture level is between 30 and 60 moisture level okay
+            lcd.printf("Moisture level okay");
+            position = 0.0;
+        }
+
+        else if (moisture_value <= 30) {                                        //if soil moisture level is below 30, soil is dry
+            position = 1.0;                                             //move servo to open position to water plant
+
+
+        } else {
+            position = 0.0;                                             //if soil moisture is above 65 soil is wet, do not water
+        }
+
+
+
+        lcd.printf("position = %.1f, range = +/-%0.4f\n", position, range);
+        myservo.calibrate(range, 45.0);
+        myservo = position;
+
+        wait (10);
+
+    }
+}
+
+
+*/
+
+