ECE 4180 final project- GPS based home automation: code for mbed controlling home devices

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 AnalogIn light_1(p20); //light sensor
00004 AnalogIn proxi_2(p15); // for detecting motion within the house
00005 DigitalIn proxi_1(p5); // for security
00006 DigitalOut relay_1(p8);// power switch tail for controlling the lamp
00007 DigitalOut relay_2(p22);// power switch tail for controlling the fan
00008 DigitalIn P11(p11); // Digital inputs p10,p11,p12,p13,p14,p16,p17 for communication between two mbeds; p18 and p19 for handshake signals;
00009 DigitalIn P12(p12);
00010 DigitalIn P13(p13);
00011 DigitalIn P14(p14);
00012 DigitalIn P10(p10);
00013 DigitalIn P16(p16);
00014 DigitalIn P17(p17);
00015 DigitalOut P18(p18);
00016 DigitalIn P19(p19);
00017 DigitalOut myled(LED1);
00018 DigitalOut led2(LED2); // device 2
00019 DigitalOut led3(LED3); //device 3
00020 DigitalOut led4(LED4); //device 4
00021 Serial pc(USBTX,USBRX);
00022 float light_thresh=0.4; // set threshold value for light sensor
00023 float ir_value=0.4; // set threshold value for IR sensor to detect the presence inside the house
00024 int P111;
00025     int P112;
00026     int P113;
00027     int P114;
00028     int P115;
00029     int P116;
00030     int P117;
00031     Mutex bulb; //for locking and unlocking the lamp
00032     Mutex pc_lock; //for virtual COM port
00033         
00034     
00035 void security_thread(void const *args) // thread for detecting presence near the house
00036 {
00037     while(true)
00038     {
00039    
00040     wait(2);
00041     if(P116==1) // check if security feature is enabled or not
00042             {
00043                 if(proxi_1==0) // check the status of proximity sensor
00044                 {
00045                     bulb.lock();
00046                     led2=1;      //switch on device 2
00047                     relay_2=1;   //switch on the fan
00048                     bulb.unlock();
00049                     wait(3);
00050                     led2=0;       //switch off device 2
00051                     relay_2=0;     //switch off the fan
00052                     wait(3);
00053                    
00054                    
00055                     
00056                     }
00057                     
00058                     
00059                 }
00060            
00061         
00062    } 
00063     
00064     
00065     }
00066 
00067 int main() {
00068   
00069     P19.mode(PullUp);
00070      proxi_1.mode(PullUp);
00071      
00072     P18=1;     // Initialize the hand shake signal
00073     P116=0;     // Make sure the security feature is turned off by default
00074     Thread security(security_thread);
00075     
00076     while(1) {
00077         
00078        label: while(P19==1)   // wait till the mbed receives data from the master mbed
00079         {
00080             
00081             }
00082             
00083         if(P19==0)        //make sure if the read signal is a correct one by waiting for one second and checking again
00084         {       wait(1);
00085         if(P19!=0)
00086           goto label;
00087         }
00088         //store the status of the digital input pins 
00089         P111=(int)P11; // D1
00090         P112=(int)P12; // D2 
00091         P113=(int)P13; // D3 
00092         P114=(int)P14; // D4
00093         P115=(int)P10; // manual=0;automatic=1
00094         P116=(int)P16; //security on=1, off=0
00095         P117=(int)P17; //near=0, far=1
00096 
00097         pc_lock.lock();
00098         pc.printf("\n P11: %d, P12: %d, P13: %d, P14:%d, P15:%d, P16:%d, P17:%d\n Proxi_2 %f\n ",P111,P112,P113,P114,P115,P116,P117,(float)proxi_2);
00099                
00100         pc_lock.unlock();
00101         P18=0; //Indicate the reception of data by pulling p18 low
00102  
00103     // blink LED1 to indicate that data has been received
00104         myled = 1;
00105         wait(2);
00106         myled = 0;
00107         wait(2);
00108         P18=1;
00109        
00110         
00111         if((float)proxi_2<ir_value) // check presence inside house
00112         
00113         {
00114        
00115             if(P115==1) // check if automatic mode is turned on
00116             {
00117                 if(P117==0) //check if the user is near the home location
00118                 {
00119                     if((float)light_1<light_thresh) //check  value of light sensor, turn on light if it is less than the threshold
00120                     relay_1=1; 
00121                     else relay_1=0;                 //turn off light if there is enough ambient light 
00122                     pc.printf("Light sensor: %f \n",(float)light_1);
00123                     bulb.lock();
00124                     led2=1;            //turn on device2
00125                     relay_2=1;                 //turn on the fan
00126                     bulb.unlock();
00127                     led3=1;             //turn on device 3
00128                    led4=1;              //turn on device 4
00129                     
00130                     
00131                     }
00132                 else                    //user is far away from home loation; turn off all devices 
00133                 {
00134                     relay_1=0; 
00135                     bulb.lock();
00136                     led2=0;
00137                       relay_2=0;
00138                     bulb.unlock();
00139                     led3=0;
00140                    led4=0;
00141                     
00142                     
00143                     
00144                     }
00145                 
00146                 
00147                 
00148                 
00149                 }
00150             else                //if the system is in manual mode
00151             {
00152                   relay_1=P111;     // read status of the lamp
00153                     bulb.lock();
00154                     led2=P112;             // read status of the device-2
00155                       relay_2=P112;       //read status of the fan
00156                     bulb.unlock();
00157                     led3=P113;           // read status of the device-3
00158                    led4=P114;            // read status of the device-4
00159                     
00160                     
00161                     }
00162         
00163     }
00164 }
00165 }