Strawberry_Greenhouse_V1.0
Dependencies: mbed C12832 Servo HCSR04 DHT DHT11
main.cpp@2:2621304bdec1, 2021-10-19 (annotated)
- Committer:
- tollidal
- Date:
- Tue Oct 19 18:55:00 2021 +0000
- Revision:
- 2:2621304bdec1
- Parent:
- 1:81cec44cec2e
- Child:
- 3:fdbd4ffb9ca6
Strawberry Greenhouse V1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbrown57 | 0:c48b28944702 | 1 | #include "mbed.h" |
jbrown57 | 0:c48b28944702 | 2 | #include "C12832.h" |
jbrown57 | 0:c48b28944702 | 3 | #include "tank.h" |
jbrown57 | 0:c48b28944702 | 4 | #include "HCSR04.h" |
tollidal | 2:2621304bdec1 | 5 | #include "DHT.h" |
tollidal | 2:2621304bdec1 | 6 | #include "Servo.h" |
jbrown57 | 0:c48b28944702 | 7 | |
jbrown57 | 0:c48b28944702 | 8 | |
jbrown57 | 0:c48b28944702 | 9 | C12832 lcd(p5, p7, p6, p8, p11); // LCD screen |
jbrown57 | 0:c48b28944702 | 10 | PwmOut r (p23); // r = red LED |
jbrown57 | 0:c48b28944702 | 11 | PwmOut g (p24); // g = green LED |
tollidal | 2:2621304bdec1 | 12 | HCSR04 sensor(p9, p20); // ultrasonic distance sensor |
tollidal | 2:2621304bdec1 | 13 | DHT sensorB(p16,DHT11); // DHT11 temp & humi sensor |
tollidal | 2:2621304bdec1 | 14 | DigitalOut fan(p18); //grove relay control DC fan |
tollidal | 2:2621304bdec1 | 15 | DigitalOut myled(p14); //assigning greenled as digital output pin14 |
tollidal | 2:2621304bdec1 | 16 | AnalogIn ldr(p15); //assigning ldr as analog input p15 |
tollidal | 2:2621304bdec1 | 17 | AnalogIn soil_moisture(p19); //soil moisture sensor |
tollidal | 2:2621304bdec1 | 18 | Servo myservo(p21); //servo |
tollidal | 2:2621304bdec1 | 19 | |
jbrown57 | 0:c48b28944702 | 20 | |
jbrown57 | 0:c48b28944702 | 21 | int main() |
jbrown57 | 0:c48b28944702 | 22 | |
jbrown57 | 0:c48b28944702 | 23 | { |
tollidal | 2:2621304bdec1 | 24 | while(1) |
tollidal | 2:2621304bdec1 | 25 | { |
tollidal | 2:2621304bdec1 | 26 | |
tollidal | 2:2621304bdec1 | 27 | |
tollidal | 2:2621304bdec1 | 28 | //water tank level system |
jbrown57 | 0:c48b28944702 | 29 | |
jbrown57 | 0:c48b28944702 | 30 | //welcome message on LCD |
jbrown57 | 0:c48b28944702 | 31 | |
tollidal | 2:2621304bdec1 | 32 | lcd.cls(); |
jbrown57 | 0:c48b28944702 | 33 | |
jbrown57 | 0:c48b28944702 | 34 | lcd.locate(25,5); |
jbrown57 | 0:c48b28944702 | 35 | |
jbrown57 | 0:c48b28944702 | 36 | lcd.printf("Tank Water Level"); |
jbrown57 | 0:c48b28944702 | 37 | |
jbrown57 | 0:c48b28944702 | 38 | lcd.locate(2,15); |
jbrown57 | 0:c48b28944702 | 39 | |
jbrown57 | 0:c48b28944702 | 40 | lcd.printf("Greenhouse Control System"); |
jbrown57 | 0:c48b28944702 | 41 | |
jbrown57 | 0:c48b28944702 | 42 | wait(2.5); |
jbrown57 | 0:c48b28944702 | 43 | |
jbrown57 | 0:c48b28944702 | 44 | lcd.cls(); //clear LCD |
jbrown57 | 0:c48b28944702 | 45 | |
jbrown57 | 1:81cec44cec2e | 46 | |
jbrown57 | 0:c48b28944702 | 47 | Timer timer; |
jbrown57 | 0:c48b28944702 | 48 | |
jbrown57 | 0:c48b28944702 | 49 | { |
jbrown57 | 0:c48b28944702 | 50 | |
jbrown57 | 0:c48b28944702 | 51 | sensor.setRanges(0, 500); // water tank level 500mm |
jbrown57 | 0:c48b28944702 | 52 | |
jbrown57 | 0:c48b28944702 | 53 | |
jbrown57 | 0:c48b28944702 | 54 | |
jbrown57 | 1:81cec44cec2e | 55 | lcd.print_bm(bitmEmptyTank,80,0); // print Empty Tank at location x=80, y=0 |
jbrown57 | 0:c48b28944702 | 56 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 57 | //if needed: lcd.locate(0,10); |
jbrown57 | 0:c48b28944702 | 58 | //if needed: lcd.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", |
jbrown57 | 0:c48b28944702 | 59 | //If needed: sensor.getMinRange(), sensor.getMaxRange()); |
jbrown57 | 0:c48b28944702 | 60 | |
tollidal | 2:2621304bdec1 | 61 | |
jbrown57 | 0:c48b28944702 | 62 | timer.reset(); |
jbrown57 | 0:c48b28944702 | 63 | timer.start(); |
jbrown57 | 0:c48b28944702 | 64 | sensor.startMeasurement(); |
tollidal | 2:2621304bdec1 | 65 | /*while(!sensor.isNewDataReady()) {*/ |
jbrown57 | 0:c48b28944702 | 66 | // wait for new data |
jbrown57 | 0:c48b28944702 | 67 | // waiting time depends on the distance |
jbrown57 | 0:c48b28944702 | 68 | } |
jbrown57 | 0:c48b28944702 | 69 | lcd.locate(20,5); |
jbrown57 | 0:c48b28944702 | 70 | lcd.printf("Distance:"); |
jbrown57 | 0:c48b28944702 | 71 | lcd.locate(20,17); |
jbrown57 | 0:c48b28944702 | 72 | lcd.printf ("%5.1f mm\r", sensor.getDistance_mm()); |
jbrown57 | 0:c48b28944702 | 73 | timer.stop(); |
jbrown57 | 0:c48b28944702 | 74 | wait_ms(500 - timer.read_ms()); // time the loop |
jbrown57 | 0:c48b28944702 | 75 | |
jbrown57 | 0:c48b28944702 | 76 | //turn RGB LED red if distance >400mm |
tollidal | 2:2621304bdec1 | 77 | |
jbrown57 | 1:81cec44cec2e | 78 | if( sensor.getDistance_mm()< 400) r= 1; //red light on |
jbrown57 | 0:c48b28944702 | 79 | else r = 0; |
jbrown57 | 0:c48b28944702 | 80 | |
jbrown57 | 1:81cec44cec2e | 81 | if( sensor.getDistance_mm()> 400) g=1; //green light on |
jbrown57 | 0:c48b28944702 | 82 | else g=0; //green light off |
jbrown57 | 0:c48b28944702 | 83 | |
jbrown57 | 0:c48b28944702 | 84 | |
jbrown57 | 0:c48b28944702 | 85 | if( sensor.getDistance_mm() < 40 ) lcd.print_bm(bitmTank,80,0); //full tank |
jbrown57 | 0:c48b28944702 | 86 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 87 | if( sensor.getDistance_mm() > 80 ) lcd.print_bm(bitmNinetyTank,80,0); //90% full tank |
jbrown57 | 0:c48b28944702 | 88 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 89 | if( sensor.getDistance_mm() > 120 ) lcd.print_bm(bitmEightyTank,80,0); //80% full tank |
jbrown57 | 0:c48b28944702 | 90 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 91 | if( sensor.getDistance_mm() > 160 ) lcd.print_bm(bitmSeventyTank,80,0); //70% full tank |
jbrown57 | 0:c48b28944702 | 92 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 93 | if( sensor.getDistance_mm() > 200 ) lcd.print_bm(bitmSixtyTank,80,0); //60% full tank |
jbrown57 | 0:c48b28944702 | 94 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 95 | if( sensor.getDistance_mm() > 240 ) lcd.print_bm(bitmFiftyTank,80,0); //50% full tank |
jbrown57 | 0:c48b28944702 | 96 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 97 | if( sensor.getDistance_mm() > 280 ) lcd.print_bm(bitmFourtyTank,80,0); //40% full tank |
jbrown57 | 0:c48b28944702 | 98 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 99 | if( sensor.getDistance_mm() > 320 ) lcd.print_bm(bitmThirtyTank,80,0); //30% full tank |
jbrown57 | 0:c48b28944702 | 100 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 101 | if( sensor.getDistance_mm() > 360 ) lcd.print_bm(bitmTwentyTank,80,0); //20% full tank |
jbrown57 | 0:c48b28944702 | 102 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 103 | if( sensor.getDistance_mm() > 400 ) lcd.print_bm(bitmTenTank,80,0); //10% full tank |
jbrown57 | 0:c48b28944702 | 104 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 105 | if( sensor.getDistance_mm() > 440 ) lcd.print_bm(bitmEmptyTank,80,0); //empty tank |
jbrown57 | 0:c48b28944702 | 106 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 107 | |
jbrown57 | 1:81cec44cec2e | 108 | |
tollidal | 2:2621304bdec1 | 109 | |
tollidal | 2:2621304bdec1 | 110 | wait (10.0f); |
tollidal | 2:2621304bdec1 | 111 | |
tollidal | 2:2621304bdec1 | 112 | |
tollidal | 2:2621304bdec1 | 113 | //Temperature Control System |
tollidal | 2:2621304bdec1 | 114 | |
tollidal | 2:2621304bdec1 | 115 | |
tollidal | 2:2621304bdec1 | 116 | //system welcome message on LCD |
tollidal | 2:2621304bdec1 | 117 | |
tollidal | 2:2621304bdec1 | 118 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 119 | |
tollidal | 2:2621304bdec1 | 120 | lcd.locate(15,5); |
tollidal | 2:2621304bdec1 | 121 | |
tollidal | 2:2621304bdec1 | 122 | lcd.printf("Temp & Humidi Level"); |
tollidal | 2:2621304bdec1 | 123 | |
tollidal | 2:2621304bdec1 | 124 | lcd.locate(2,15); |
tollidal | 2:2621304bdec1 | 125 | |
tollidal | 2:2621304bdec1 | 126 | lcd.printf("Greenhouse Control System"); |
tollidal | 2:2621304bdec1 | 127 | |
tollidal | 2:2621304bdec1 | 128 | wait(2.5); |
tollidal | 2:2621304bdec1 | 129 | |
tollidal | 2:2621304bdec1 | 130 | lcd.cls(); //clear LCD |
jbrown57 | 0:c48b28944702 | 131 | |
jbrown57 | 0:c48b28944702 | 132 | |
tollidal | 2:2621304bdec1 | 133 | int error = 0; |
tollidal | 2:2621304bdec1 | 134 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
tollidal | 2:2621304bdec1 | 135 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 136 | char tmpString[16]; |
tollidal | 2:2621304bdec1 | 137 | int temp_format = 0; |
tollidal | 2:2621304bdec1 | 138 | int humid_format = 0; |
tollidal | 2:2621304bdec1 | 139 | |
tollidal | 2:2621304bdec1 | 140 | //while(1) |
tollidal | 2:2621304bdec1 | 141 | |
tollidal | 2:2621304bdec1 | 142 | |
tollidal | 2:2621304bdec1 | 143 | wait(0.5f); |
tollidal | 2:2621304bdec1 | 144 | error = sensorB.readData(); |
tollidal | 2:2621304bdec1 | 145 | if (0 == error) { |
tollidal | 2:2621304bdec1 | 146 | c = sensorB.ReadTemperature(CELCIUS); |
tollidal | 2:2621304bdec1 | 147 | f = sensorB.ReadTemperature(FARENHEIT); |
tollidal | 2:2621304bdec1 | 148 | k = sensorB.ReadTemperature(KELVIN); |
tollidal | 2:2621304bdec1 | 149 | h = sensorB.ReadHumidity(); |
tollidal | 2:2621304bdec1 | 150 | dp = sensorB.CalcdewPoint(c, h); |
tollidal | 2:2621304bdec1 | 151 | dpf = sensorB.CalcdewPointFast(c, h); |
tollidal | 2:2621304bdec1 | 152 | printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); |
tollidal | 2:2621304bdec1 | 153 | printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); |
tollidal | 2:2621304bdec1 | 154 | printf("Tempformat: %d HumidFormat: %d\n", temp_format, humid_format); |
tollidal | 2:2621304bdec1 | 155 | switch(temp_format) { |
tollidal | 2:2621304bdec1 | 156 | case 0 : |
tollidal | 2:2621304bdec1 | 157 | sprintf(tmpString, " Temp: %4.2fC", c); |
tollidal | 2:2621304bdec1 | 158 | break; |
tollidal | 2:2621304bdec1 | 159 | case 1 : |
tollidal | 2:2621304bdec1 | 160 | sprintf(tmpString, " Temp: %4.2fF", f); |
tollidal | 2:2621304bdec1 | 161 | break; |
tollidal | 2:2621304bdec1 | 162 | case 2 : |
tollidal | 2:2621304bdec1 | 163 | sprintf(tmpString, " Temp: %4.2fK", k); |
tollidal | 2:2621304bdec1 | 164 | break; |
tollidal | 2:2621304bdec1 | 165 | |
tollidal | 2:2621304bdec1 | 166 | |
tollidal | 2:2621304bdec1 | 167 | //display the results onto the LCD |
tollidal | 2:2621304bdec1 | 168 | lcd.locate(0,0); |
tollidal | 2:2621304bdec1 | 169 | lcd.printf("Temperature: %4.2fC", c); |
tollidal | 2:2621304bdec1 | 170 | lcd.locate(0,10); |
tollidal | 2:2621304bdec1 | 171 | lcd.printf("Humidity: %4.2f", h); |
tollidal | 2:2621304bdec1 | 172 | wait(2); |
tollidal | 2:2621304bdec1 | 173 | |
tollidal | 2:2621304bdec1 | 174 | //turn fan on if temperature is above 26 |
tollidal | 2:2621304bdec1 | 175 | if(c > 26) fan=1; //fan is on |
tollidal | 2:2621304bdec1 | 176 | else fan=0; //Fan is off |
tollidal | 2:2621304bdec1 | 177 | |
tollidal | 2:2621304bdec1 | 178 | |
tollidal | 2:2621304bdec1 | 179 | //wait 2 seconds and check temp again |
tollidal | 2:2621304bdec1 | 180 | wait(10.0); |
jbrown57 | 0:c48b28944702 | 181 | |
jbrown57 | 0:c48b28944702 | 182 | |
jbrown57 | 0:c48b28944702 | 183 | |
jbrown57 | 0:c48b28944702 | 184 | |
tollidal | 2:2621304bdec1 | 185 | //Light Sensor System |
tollidal | 2:2621304bdec1 | 186 | |
tollidal | 2:2621304bdec1 | 187 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 188 | |
tollidal | 2:2621304bdec1 | 189 | lcd.locate(25,5); |
tollidal | 2:2621304bdec1 | 190 | |
tollidal | 2:2621304bdec1 | 191 | lcd.printf("Light Level"); |
tollidal | 2:2621304bdec1 | 192 | |
tollidal | 2:2621304bdec1 | 193 | lcd.locate(2,15); |
tollidal | 2:2621304bdec1 | 194 | |
tollidal | 2:2621304bdec1 | 195 | lcd.printf("Greenhouse Control System"); |
tollidal | 2:2621304bdec1 | 196 | |
tollidal | 2:2621304bdec1 | 197 | wait(2.5); |
tollidal | 2:2621304bdec1 | 198 | |
tollidal | 2:2621304bdec1 | 199 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 200 | |
tollidal | 2:2621304bdec1 | 201 | //while(1) |
tollidal | 2:2621304bdec1 | 202 | float ldrout=ldr.read(); //read ldr value |
tollidal | 2:2621304bdec1 | 203 | lcd.locate(10,10); |
tollidal | 2:2621304bdec1 | 204 | lcd.printf("Light Level is: %f\n :",ldrout); //print ldr value on LCD |
tollidal | 2:2621304bdec1 | 205 | if(ldrout<0.6) { //if ldr value is smaller than 0.6 green LED turns on |
tollidal | 2:2621304bdec1 | 206 | |
tollidal | 2:2621304bdec1 | 207 | myled = 1; //LED on |
tollidal | 2:2621304bdec1 | 208 | } else { |
tollidal | 2:2621304bdec1 | 209 | myled=0; //LED off |
tollidal | 2:2621304bdec1 | 210 | |
tollidal | 2:2621304bdec1 | 211 | wait(10.0); |
tollidal | 2:2621304bdec1 | 212 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 213 | |
tollidal | 2:2621304bdec1 | 214 | } |
tollidal | 2:2621304bdec1 | 215 | |
tollidal | 2:2621304bdec1 | 216 | } |
tollidal | 2:2621304bdec1 | 217 | } |
tollidal | 2:2621304bdec1 | 218 | } |
jbrown57 | 0:c48b28944702 | 219 | } |
jbrown57 | 0:c48b28944702 | 220 | |
tollidal | 2:2621304bdec1 | 221 | /* |
tollidal | 2:2621304bdec1 | 222 | |
tollidal | 2:2621304bdec1 | 223 | //Soil Moisture System |
tollidal | 2:2621304bdec1 | 224 | |
tollidal | 2:2621304bdec1 | 225 | //lcd.cls(); |
tollidal | 2:2621304bdec1 | 226 | |
tollidal | 2:2621304bdec1 | 227 | lcd.locate(25,5); |
tollidal | 2:2621304bdec1 | 228 | |
tollidal | 2:2621304bdec1 | 229 | lcd.printf("Soil Moisture Level"); |
tollidal | 2:2621304bdec1 | 230 | |
tollidal | 2:2621304bdec1 | 231 | lcd.locate(2,15); |
tollidal | 2:2621304bdec1 | 232 | |
tollidal | 2:2621304bdec1 | 233 | lcd.printf("Greenhouse Control System"); |
tollidal | 2:2621304bdec1 | 234 | |
tollidal | 2:2621304bdec1 | 235 | wait(2.5); |
tollidal | 2:2621304bdec1 | 236 | |
tollidal | 2:2621304bdec1 | 237 | |
tollidal | 2:2621304bdec1 | 238 | float range = 0.0005; |
tollidal | 2:2621304bdec1 | 239 | float position = 0.5; |
tollidal | 2:2621304bdec1 | 240 | |
tollidal | 2:2621304bdec1 | 241 | float moisture_value = 0.0f; |
tollidal | 2:2621304bdec1 | 242 | |
tollidal | 2:2621304bdec1 | 243 | |
tollidal | 2:2621304bdec1 | 244 | while(1) { |
tollidal | 2:2621304bdec1 | 245 | |
tollidal | 2:2621304bdec1 | 246 | moisture_value = soil_moisture*100; //soil moisture value |
tollidal | 2:2621304bdec1 | 247 | lcd.locate(0,0); //text location on screen |
tollidal | 2:2621304bdec1 | 248 | lcd.cls(); |
tollidal | 2:2621304bdec1 | 249 | lcd.printf("Moisture reading is: %2.2f\n", moisture_value); //display soil moisture value as a percentage |
tollidal | 2:2621304bdec1 | 250 | wait(1.0f); |
tollidal | 2:2621304bdec1 | 251 | // lcd.printf("Moisture reading is: %2.2f\n", value); |
tollidal | 2:2621304bdec1 | 252 | |
tollidal | 2:2621304bdec1 | 253 | if (moisture_value > 30&& moisture_value <= 50) { //if soil moisture level is between 30 and 60 moisture level okay |
tollidal | 2:2621304bdec1 | 254 | lcd.printf("Moisture level okay"); |
tollidal | 2:2621304bdec1 | 255 | position = 0.0; |
tollidal | 2:2621304bdec1 | 256 | } |
tollidal | 2:2621304bdec1 | 257 | |
tollidal | 2:2621304bdec1 | 258 | else if (moisture_value <= 30) { //if soil moisture level is below 30, soil is dry |
tollidal | 2:2621304bdec1 | 259 | position = 1.0; //move servo to open position to water plant |
tollidal | 2:2621304bdec1 | 260 | |
tollidal | 2:2621304bdec1 | 261 | |
tollidal | 2:2621304bdec1 | 262 | } else { |
tollidal | 2:2621304bdec1 | 263 | position = 0.0; //if soil moisture is above 65 soil is wet, do not water |
tollidal | 2:2621304bdec1 | 264 | } |
tollidal | 2:2621304bdec1 | 265 | |
tollidal | 2:2621304bdec1 | 266 | |
tollidal | 2:2621304bdec1 | 267 | |
tollidal | 2:2621304bdec1 | 268 | lcd.printf("position = %.1f, range = +/-%0.4f\n", position, range); |
tollidal | 2:2621304bdec1 | 269 | myservo.calibrate(range, 45.0); |
tollidal | 2:2621304bdec1 | 270 | myservo = position; |
tollidal | 2:2621304bdec1 | 271 | |
tollidal | 2:2621304bdec1 | 272 | wait (10); |
tollidal | 2:2621304bdec1 | 273 | |
tollidal | 2:2621304bdec1 | 274 | } |
tollidal | 2:2621304bdec1 | 275 | } |
tollidal | 2:2621304bdec1 | 276 | |
tollidal | 2:2621304bdec1 | 277 | |
tollidal | 2:2621304bdec1 | 278 | */ |
tollidal | 2:2621304bdec1 | 279 | |
tollidal | 2:2621304bdec1 | 280 |