Smart barn work with sensor DHT22 measure temp. & Hum.

Dependencies:   DHT mbed

Revision:
0:f45f2181e4f5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 09 08:36:36 2015 +0000
@@ -0,0 +1,415 @@
+#include "mbed.h"
+#include "DHT.h"
+
+Serial hm_tp (PA_11, PA_12);  // PA_11 - tx, PA_12 - rx via bluetooth
+
+DigitalOut lamp(D11); // Hydrogyn lamp for heat
+
+DigitalOut waterIn(D9); // Fake springer
+DigitalOut waterOut(D10); // Fake springer
+
+DigitalOut led1(D7); // Status 0f DHT22-1
+DigitalOut led2(D8); // Status 0f DHT22-2
+
+DigitalOut fanL(D5); // motor Fan for control way
+DigitalOut fanR(D6);
+
+DHT sensorOut(D3,DHT22); // Use the AM2302 sensor
+DHT sensorIn(D4,DHT22); // sensorOut is outside, sensorIn is inside
+
+int main()
+{
+    //int err1, err2;
+    //float refTemp = 0, refHum = 0;
+    uint8_t hedSelect = 0, state_show = 0, state_exit = 0, HedJa;
+    int stop = 0;
+
+    hm_tp.printf("Hello Sir / Madam!!\nPls choose your mushroom\n");
+    while(1) {
+        if(state_show == 0) {
+            hm_tp.printf("Menu\n");
+            hm_tp.printf("1.Hed Ya na ngi\n"); //25-32 78-80
+            hm_tp.printf("2.Hed Fang\n"); //30-38 70-80
+            state_show = 1;
+        }
+        if(hm_tp.readable()) {
+            HedJa = hm_tp.getc();
+            hm_tp.printf("\n");
+            state_show = 0;
+            state_exit = 0;
+
+            switch(HedJa) {
+                case '1':
+                    do {
+                        if(hedSelect == 0) {
+                            hm_tp.printf("Hed Ya na ngi\nTemp. 25-32'c\nHum. 78-80% \n");
+                            hm_tp.printf("a : Start Process\nb : Change Type/Stop Process\n");
+                            hedSelect = 1;
+                        }
+                        if(hm_tp.readable()) {
+                            HedJa = hm_tp.getc();
+                            hm_tp.printf("\n");
+                            hedSelect = 0;
+
+                            switch(HedJa) {
+                                case 'a' :
+                                    hm_tp.printf("*When you enough to see value pls press '1'*\n");
+
+                                    led1 = 1;
+                                    led2 = 1;
+
+                                    if(sensorOut.ReadTemperature(CELCIUS) > sensorIn.ReadTemperature(CELCIUS)) 
+                                    {
+                                                if(sensorIn.ReadTemperature(CELCIUS) < 25 && sensorOut.ReadTemperature(CELCIUS) > 32) {
+                                                    fanL = 1;
+                                                    fanR = 0; //fan run - wind come in
+                                                    wait(15);
+                                                    fanL = 0;
+                                                    fanR = 0; // off fan
+                                                    
+                                                } else if(sensorIn.ReadTemperature(CELCIUS) < 25 && sensorOut.ReadTemperature(CELCIUS) > 25 && sensorOut.ReadTemperature(CELCIUS) < 32) {
+                                                    lamp = 1; //lamp run
+                                                    wait(30);
+                                                    lamp = 0; //lamp stop
+                                                  
+                                                } else if(sensorIn.ReadTemperature(CELCIUS) >= 25 && sensorIn.ReadTemperature(CELCIUS) <= 32 && sensorOut.ReadTemperature(CELCIUS) > 32) {
+                                                    //spinger out run estimate 30 mins. in real but in show just run 10 sec.
+                                                    waterOut = 1;
+                                                    wait(10);
+                                                    waterOut = 0;
+        
+                                                                if(sensorIn.ReadHumidity() < 78) {
+                                                                    waterIn = 1;
+                                                                    wait(1);
+                                                                    waterIn = 0;
+                                                                } else if(sensorIn.ReadHumidity() >= 78 && sensorIn.ReadHumidity() <= 80) {
+                                                                    break;
+                                                                } else { // sensorIn.ReadHumidity() > 80
+                                                                    //fan run - wind come in
+                                                                    fanL = 1;
+                                                                    fanR = 0; // fan run
+                                                                    wait(15);
+                                                                    fanL = 0;
+                                                                    fanR = 0; //fan stop
+                                                                   
+                                                                }
+                                                  
+                                                } else { // sensorIn.ReadTemperature(CELCIUS) > 32 && sensorOut.ReadTemperature(CELCIUS) > 32
+                                                    fanL = 0;
+                                                    fanR = 1; //wind get out
+                                                    wait(20);
+                                                    fanL = 0; //fan stop
+                                                    fanR = 0;
+                                                    waterOut = 1; //springer out run
+                                                    wait(10);
+                                                    waterOut = 0;
+                                                    
+                                                }
+                                    } else if(sensorOut.ReadTemperature(CELCIUS) < sensorIn.ReadTemperature(CELCIUS)) {
+                                                if(sensorIn.ReadTemperature(CELCIUS) > 32 && sensorOut.ReadTemperature(CELCIUS) > 25 && sensorOut.ReadTemperature(CELCIUS) < 32) {
+                                                    fanL = 1;
+                                                    fanR = 0; //fan run - wind come in
+                                                    wait(15);
+                                                    fanL = 0;
+                                                    fanR = 0; // off fan
+                                                } else if(sensorIn.ReadTemperature(CELCIUS) >= 25 && sensorIn.ReadTemperature(CELCIUS) <= 32 && sensorOut.ReadTemperature(CELCIUS) < 25) {
+        
+                                                            if(sensorIn.ReadHumidity() < 78) {
+                                                                waterIn = 1;
+                                                                wait(1);
+                                                                waterIn = 0;
+                                                            } else if(sensorIn.ReadHumidity() >= 78 && sensorIn.ReadHumidity() <= 80) {
+                                                                break;
+                                                            } else { // sensorIn.ReadHumidity() > 80
+                                                                //fan run - wind come in
+                                                                fanL = 1;
+                                                                fanR = 0; // fan run
+                                                                wait(15);
+                                                                fanL = 0;
+                                                                fanR = 0; //fan stop
+                                                               
+                                                            }
+                                            
+                                                } else { // sensorIn.ReadTemperature(CELCIUS) > 32 && sensorOut.ReadTemperature(CELCIUS) > 32
+                                                    fanL = 0;
+                                                    fanR = 1; //wind get out
+                                                    wait(20);
+                                                    fanL = 0; //fan stop
+                                                    fanR = 0;
+                                                    waterOut = 1; //springer out run
+                                                    wait(10);
+                                                    waterOut = 0;
+                                                    break;
+                                                }
+                                    } else { //sensorOut.ReadTemperature(CELCIUS) == sensorIn.ReadTemperature(CELCIUS)
+                                                if(sensorIn.ReadTemperature(CELCIUS) < 25 && sensorOut.ReadTemperature(CELCIUS) > 32) {
+                                                    fanL = 1;
+                                                    fanR = 0; //fan run - wind come in
+                                                    wait(15);
+                                                    fanL = 0;
+                                                    fanR = 0; // off fan
+                                                    break;
+                                                } else if(sensorIn.ReadTemperature(CELCIUS) < 25 && sensorOut.ReadTemperature(CELCIUS) > 25 && sensorOut.ReadTemperature(CELCIUS) < 32) {
+                                                    lamp = 1; //lamp run
+                                                    wait(30);
+                                                    lamp = 0; //lamp stop
+                                                    break;
+                                                } else if(sensorIn.ReadTemperature(CELCIUS) >= 25 && sensorIn.ReadTemperature(CELCIUS) <= 32 && sensorOut.ReadTemperature(CELCIUS) > 32) {
+                                                    //spinger out run estimate 30 mins. in real but in show just run 10 sec.
+                                                    waterOut = 1;
+                                                    wait(10);
+                                                    waterOut = 0;
+        
+                                                                if(sensorIn.ReadHumidity() < 78) {
+                                                                    waterIn = 1;
+                                                                    wait(5);
+                                                                    waterIn = 0;
+                                                                } else if(sensorIn.ReadHumidity() >= 78 && sensorIn.ReadHumidity() <= 80) {
+                                                                    break;
+                                                                } else { // sensorIn.ReadHumidity() > 80
+                                                                    //fan run - wind come in
+                                                                    fanL = 1;
+                                                                    fanR = 0; // fan run
+                                                                    wait(15);
+                                                                    fanL = 0;
+                                                                    fanR = 0; //fan stop
+                                                                   
+                                                                }
+                                                    
+                                                } else { // sensorIn.ReadTemperature(CELCIUS) > 32 && sensorOut.ReadTemperature(CELCIUS) > 32
+                                                    fanL = 0;
+                                                    fanR = 1; //wind get out
+                                                    wait(20);
+                                                    fanL = 0; //fan stop
+                                                    fanR = 0;
+                                                    waterOut = 1; //springer out run
+                                                    wait(10);
+                                                    waterOut = 0;
+                                                   
+                                                }
+                                    }
+
+                                    led1 = 0;
+                                    led2 = 0;
+                                    wait(5);
+
+                                    scanf("%d",&stop);
+                                    if(stop == '1') {
+                                        hedSelect = 0;
+                                        break;
+                                    }
+
+                                case 'b' :
+                                    state_exit = 0;
+                                    state_show = 0;
+
+                                default :
+                                    hm_tp.printf("please choose a or b only\n");
+                                    break;
+                            }
+
+                        }
+                    }
+
+                    while(state_exit == 0);
+                    hm_tp.printf("\n\n");
+                    state_exit = 1;
+                    state_show = 0;
+                    break;
+                
+                case '2':
+                    do {if(hedSelect == 0) {
+                            hm_tp.printf("Hed Fang\nTemp. 30-38'c\nHum. 70-80% \n");
+                            hm_tp.printf("a : Start Process\nb : Change Type/Stop Process\n");
+                            //hedSelect = 1;
+                        }
+                        if(hm_tp.readable()) {
+                            HedJa = hm_tp.getc();
+                            hm_tp.printf("\n");
+                            //hedSelect = 0;
+
+                            switch(HedJa) {
+                                case 'a' :
+                                    hm_tp.printf("*When you enough to see value pls press '1'*\n");
+
+                                    led1 = 1;
+                                    led2 = 1;
+
+                                    if(sensorOut.ReadTemperature(CELCIUS) > sensorIn.ReadTemperature(CELCIUS)) {
+                                        if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            fanL = 1;
+                                            fanR = 0; //fan run - wind come in
+                                            wait(15);
+                                            fanL = 0;
+                                            fanR = 0; // off fan
+                                           
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 30 && sensorOut.ReadTemperature(CELCIUS) < 38) {
+                                            lamp = 1; //lamp run
+                                            wait(30);
+                                            lamp = 0; //lamp stop
+                                            
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) >= 30 && sensorIn.ReadTemperature(CELCIUS) <= 38 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            //spinger out run estimate 30 mins. in real but in show just run 10 sec.
+                                            waterOut = 1;
+                                            wait(10);
+                                            waterOut = 0;
+
+                                            if(sensorIn.ReadHumidity() < 70) {
+                                                waterIn = 1;
+                                                wait(1);
+                                                waterIn = 0;
+                                            } else if(sensorIn.ReadHumidity() >= 70 && sensorIn.ReadHumidity() <= 80) {
+                                                break;
+                                            } else { // sensorIn.ReadHumidity() > 80
+                                                //fan run - wind come in
+                                                fanL = 1;
+                                                fanR = 0; // fan run
+                                                wait(15);
+                                                fanL = 0;
+                                                fanR = 0; //fan stop
+                                                
+                                            }
+                                           
+                                        } else { // sensorIn.ReadTemperature(CELCIUS) > 38 && sensorOut.ReadTemperature(CELCIUS) > 38
+                                            fanL = 0;
+                                            fanR = 1; //wind get out
+                                            wait(20);
+                                            fanL = 0; //fan stop
+                                            fanR = 0;
+                                            waterOut = 1; //springer out run
+                                            wait(10);
+                                            waterOut = 0;
+                                            
+                                        }
+                                    } else if(sensorOut.ReadTemperature(CELCIUS) < sensorIn.ReadTemperature(CELCIUS)) {
+                                        if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            fanL = 1;
+                                            fanR = 0; //fan run - wind come in
+                                            wait(15);
+                                            fanL = 0;
+                                            fanR = 0; // off fan
+                                            
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 38 && sensorOut.ReadTemperature(CELCIUS) < 38) {
+                                            lamp = 1; //lamp run
+                                            wait(30);
+                                            lamp = 0; //lamp stop
+                                           
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) >= 30 && sensorIn.ReadTemperature(CELCIUS) <= 38 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            //spinger out run estimate 30 mins. in real but in show just run 10 sec.
+                                            waterOut = 1;
+                                            wait(10);
+                                            waterOut = 0;
+
+                                            if(sensorIn.ReadHumidity() < 70) {
+                                                waterIn = 1;
+                                                wait(1);
+                                                waterIn = 0;
+                                            } else if(sensorIn.ReadHumidity() >= 70 && sensorIn.ReadHumidity() <= 80) {
+                                               
+                                            } else { // sensorIn.ReadHumidity() > 80
+                                                //fan run - wind come in
+                                                fanL = 1;
+                                                fanR = 0; // fan run
+                                                wait(15);
+                                                fanL = 0;
+                                                fanR = 0; //fan stop
+                                             
+                                            }
+                                           
+                                        } else { // sensorIn.ReadTemperature(CELCIUS) > 38 && sensorOut.ReadTemperature(CELCIUS) > 38
+                                            fanL = 0;
+                                            fanR = 1; //wind get out
+                                            wait(20);
+                                            fanL = 0; //fan stop
+                                            fanR = 0;
+                                            waterOut = 1; //springer out run
+                                            wait(10);
+                                            waterOut = 0;
+                                            
+                                        }
+                                    } else { //sensorOut.ReadTemperature(CELCIUS) == sensorIn.ReadTemperature(CELCIUS)
+                                        if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            fanL = 1;
+                                            fanR = 0; //fan run - wind come in
+                                            wait(15);
+                                            fanL = 0;
+                                            fanR = 0; // off fan
+                                            
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) < 30 && sensorOut.ReadTemperature(CELCIUS) > 30 && sensorOut.ReadTemperature(CELCIUS) < 38) {
+                                            lamp = 1; //lamp run
+                                            wait(30);
+                                            lamp = 0; //lamp stop
+                                           
+                                        } else if(sensorIn.ReadTemperature(CELCIUS) >= 30 && sensorIn.ReadTemperature(CELCIUS) <= 38 && sensorOut.ReadTemperature(CELCIUS) > 38) {
+                                            //spinger out run estimate 30 mins. in real but in show just run 10 sec.
+                                            waterOut = 1;
+                                            wait(10);
+                                            waterOut = 0;
+
+                                            if(sensorIn.ReadHumidity() < 70) {
+                                                waterIn = 1;
+                                                wait(5);
+                                                waterIn = 0;
+                                            } else if(sensorIn.ReadHumidity() >= 70 && sensorIn.ReadHumidity() <= 80) {
+                                                break;
+                                            } else { // sensorIn.ReadHumidity() > 80
+                                                //fan run - wind come in
+                                                fanL = 1;
+                                                fanR = 0; // fan run
+                                                wait(15);
+                                                fanL = 0;
+                                                fanR = 0; //fan stop
+                                                
+                                            }
+                                            
+                                        } else { // sensorIn.ReadTemperature(CELCIUS) > 38 && sensorOut.ReadTemperature(CELCIUS) > 38
+                                            fanL = 0;
+                                            fanR = 1; //wind get out
+                                            wait(20);
+                                            fanL = 0; //fan stop
+                                            fanR = 0;
+                                            waterOut = 1; //springer out run
+                                            wait(10);
+                                            waterOut = 0;
+                                           
+                                        }
+                                    }
+
+                                    led1 = 0;
+                                    led2 = 0;
+                                    wait(5);
+
+                                    hm_tp.scanf("%d",&stop);
+                                    if(stop == '1') {
+                                        hedSelect = 0;
+                                        break;
+                                    }
+
+                                case 'b' :
+                                    state_exit = 0;
+
+                                default :
+                                    hm_tp.printf("\nplease choose a or b only\n");
+                                    break;
+                            }
+
+                        }
+                    }
+
+                    while(state_exit == 0);
+                    hm_tp.printf("\n\n");
+                    state_exit = 1;
+                    state_show = 0;
+                    break;
+
+
+                case 0x00:
+                    break;
+                default:
+                    hm_tp.printf("\nplease choose 1 or 2 only\n");
+                    hm_tp.printf("\n\n");
+                    break;
+            }
+        }
+    }
+} 
\ No newline at end of file