Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed HBridge MQ7 Ton SimpleScheduler Pir_sensor
main.cpp
00001 #include "mbed.h" 00002 #include "math.h" 00003 #include "Pir_sensor.h" 00004 #include "MQ7.h" 00005 #include <string> 00006 #include "SimpleScheduler.h" 00007 #include "ESP8266.h" 00008 #include "Thingspeak.h" 00009 #include "HBridge.h" 00010 #include "Ton.h" 00011 00012 #define TIMER_PERIODICITY (1) /* 1 second */ 00013 #define SCHEDULER_TASK_PERIODICITY (500) /* 500ms */ 00014 #define GARAGE_LIGHT_TURN_OFF_DELAY (10) 00015 00016 Ton t1(15000); // Timer ON delay of 15 seconds. 00017 Ton t2(15000); // Timer ON delay of 15 seconds. 00018 00019 Serial PC(USBTX, USBRX); 00020 /* pir senor input */ 00021 Pir_sensor PIR(D2); 00022 /* Garage lamp output */ 00023 DigitalOut Garage_Lamp_Status(D7); 00024 /* Garage CO sensor output */ 00025 MQ7 CO_Sensor(A0,D3); 00026 DigitalOut PIR_DetectionLight(LED2); 00027 HBridge Motor(D5,D6,D4); 00028 00029 Ticker ticker; 00030 SimpleScheduler *scheduler; 00031 00032 typedef enum { 00033 00034 DOOR_IS_OPEN = 1, 00035 DOOR_IS_CLOSED = 2, 00036 DOOR_IS_SAFE_OPEN = 3, 00037 } eDoor_Status_t; 00038 00039 00040 typedef enum { 00041 00042 MOVEMENT_DETECTED = 1, 00043 NO_MOVEMENT_DETECTED = 2, 00044 } ePIR_Status_t; 00045 00046 typedef enum { 00047 00048 OPEN_DOOR = 1, 00049 CLOSE_DOOR = 2, 00050 SAFE_OPEN_DOOR = 3, 00051 } eDoor_Command_t; 00052 00053 eDoor_Status_t doorStatus = DOOR_IS_CLOSED; 00054 eDoor_Command_t doorControl = CLOSE_DOOR; 00055 eDoor_Command_t prevDoorControl = CLOSE_DOOR; 00056 char garage_light_delay_counter = 0; 00057 ePIR_Status_t PIR_Status = NO_MOVEMENT_DETECTED; 00058 int CO_Level = 1; 00059 /*! 00060 * Periodic Timer Interrupt controlling garage lights 00061 * 00062 * ============================================================================= 00063 */ 00064 void Periodic_Interrupt ( void ) 00065 { 00066 if ( garage_light_delay_counter > 0 ) 00067 { 00068 garage_light_delay_counter--; 00069 } 00070 else 00071 { 00072 if ( DOOR_IS_CLOSED == doorStatus ) 00073 { 00074 Garage_Lamp_Status = 0; 00075 } 00076 00077 PIR_Status = NO_MOVEMENT_DETECTED; 00078 } 00079 } 00080 00081 /*! 00082 * Function used monitor and control door status, CO level and garage lights 00083 * 00084 * ============================================================================= 00085 */ 00086 void Garage_Monitor_Runnable ( void ) 00087 { 00088 /* Development In Progress */ 00089 if( PIR.read() ) 00090 { 00091 PIR_DetectionLight = 0; 00092 Garage_Lamp_Status = 1; 00093 PC.printf("\n\r*** MOTION DETECTED *** \n\r"); 00094 PIR_Status = MOVEMENT_DETECTED; 00095 PC.printf("\r\n*** GARAGE LIGHT IS ON *** \r\n"); 00096 garage_light_delay_counter = GARAGE_LIGHT_TURN_OFF_DELAY; 00097 } 00098 else 00099 { 00100 if ( PIR_DetectionLight == 0 ) 00101 { 00102 PC.printf("\n\r*** NO MOTION DETECTED *** \n\r"); 00103 } 00104 PIR_DetectionLight = 1; 00105 } 00106 00107 CO_Level = CO_Sensor.get_CO_value (); 00108 if ( CO_Level == 1 ) 00109 { 00110 PC.printf("\r\n*** GARAGE CO LEVEL IS SAFE/LOW *** \r\n"); 00111 } 00112 else if ( CO_Level == 2 ) 00113 { 00114 PC.printf("\r\n*** WARNING!! GARAGE CO LEVEL IS MODERATE *** \r\n"); 00115 } 00116 else 00117 { 00118 PC.printf("\r\n*** DANGER!!! GARAGE CO LEVEL IS HIGH *** \r\n"); 00119 } 00120 PC.printf("\n\r=========================================================================\n\r"); 00121 PC.printf("\n\rGarage Door Status = %d \n\rGarage CO Level = %d\n\rPIR Status = %d \n\r", (int)doorStatus, CO_Level, (int)PIR_Status); 00122 PC.printf("\n\r=========================================================================\n\r"); 00123 00124 if ( t2 ) 00125 { 00126 /* Send garage status to Thingspeak channel */ 00127 Send_to_Thingspeak ( (int)doorStatus, CO_Level, (int)PIR_Status ) ; 00128 t2 = Ton::Off; 00129 t2.reset(); 00130 t1 = Ton::On; 00131 } 00132 } 00133 00134 void Garage_Door_Control_Runnable( void ) 00135 { 00136 00137 if ( t1 ) 00138 { 00139 // This code will be executed once the timer times out. 00140 doorControl = ( eDoor_Command_t ) Read_from_Thingspeak(); 00141 PC.printf("\r\n Door Command received from Thingspeak = %d \r\n", doorControl); 00142 t1 = Ton::Off; 00143 t1.reset(); 00144 t2 = Ton::On; 00145 } 00146 if ( ( CO_Level > 1 ) && ( prevDoorControl != OPEN_DOOR ) ) 00147 { 00148 doorControl = SAFE_OPEN_DOOR; 00149 } 00150 if ( ( doorControl == OPEN_DOOR ) && ( prevDoorControl != OPEN_DOOR ) ) 00151 { 00152 Garage_Lamp_Status = 1; 00153 garage_light_delay_counter = GARAGE_LIGHT_TURN_OFF_DELAY; 00154 PC.printf("\r\n*** GARAGE LIGHT IS TURNED ON *** \r\n"); 00155 Motor.forward (); 00156 if ( doorStatus == DOOR_IS_CLOSED ) 00157 { 00158 wait(8); 00159 } 00160 else if ( doorStatus == DOOR_IS_SAFE_OPEN ) 00161 { 00162 wait(6); 00163 } 00164 Motor.stop (); 00165 doorStatus = DOOR_IS_OPEN; 00166 PC.printf("\r\n*** DOOR IS OPEN ***\r\n"); 00167 } 00168 else if ( ( doorControl == SAFE_OPEN_DOOR ) && ( prevDoorControl == CLOSE_DOOR ) ) 00169 { 00170 Garage_Lamp_Status = 1; 00171 garage_light_delay_counter = GARAGE_LIGHT_TURN_OFF_DELAY; 00172 PC.printf("\r\n*** GARAGE LIGHT IS TURNED ON *** \r\n"); 00173 Motor.forward (); 00174 wait(2); 00175 Motor.stop (); 00176 doorStatus = DOOR_IS_SAFE_OPEN; 00177 PC.printf("\r\n*** DOOR IS SAFETY OPEN ***\r\n"); 00178 } 00179 else if ( ( doorControl == CLOSE_DOOR ) && ( prevDoorControl != CLOSE_DOOR ) ) 00180 { 00181 Garage_Lamp_Status = 1; 00182 garage_light_delay_counter = GARAGE_LIGHT_TURN_OFF_DELAY; 00183 PC.printf("\r\n*** GARAGE LIGHT IS TURNED ON *** \r\n"); 00184 Motor.backward (); 00185 if ( doorStatus == DOOR_IS_OPEN ) 00186 { 00187 wait(9); 00188 } 00189 else if ( doorStatus == DOOR_IS_SAFE_OPEN ) 00190 { 00191 wait(2); 00192 } 00193 Motor.stop (); 00194 PC.printf("\r\n*** DOOR IS CLOSED *** \r\n"); 00195 doorStatus = DOOR_IS_CLOSED; 00196 } 00197 else 00198 { 00199 00200 } 00201 prevDoorControl = doorControl; 00202 } 00203 00204 void Task_500ms_Runnable(SimpleTask *task) { 00205 00206 Garage_Door_Control_Runnable (); 00207 00208 Garage_Monitor_Runnable (); 00209 } 00210 00211 /*! 00212 * Main function, user application starts from here. 00213 * 00214 * ============================================================================= 00215 */ 00216 int main ( void ) 00217 { 00218 /* Baud rate used for communicating with Tera-term on PC */ 00219 PC.baud(115200); 00220 PC.printf("\n\r\n\r"); 00221 PC.printf(" ###### ## ## ###### ##### ######## #### ###### ##### ###### #### ######## ###### ## ## ###### ######## ######## ## ##\n\r"); 00222 PC.printf("## ## ### ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ###\n\r"); 00223 PC.printf("## #### #### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #### ## ## ## #### ####\n\r"); 00224 PC.printf(" ###### ## ### ## ######## ## ### ## ## ### ######## ## ### ######## ## ### ###### ###### ## ###### ## ###### ## ### ##\n\r"); 00225 PC.printf(" ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##\n\r"); 00226 PC.printf("## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##\n\r"); 00227 PC.printf(" ###### ## ## ## ## ## ## ## #### ## ## ## ## ## ## #### ######## ###### ## ###### ## ######## ## ##\n\r"); 00228 PC.printf("-----------------------------------------------------------------------------------------\n\r"); 00229 PC.printf("Developed By: Priyanka Kunnal Siddalingappa\n\r"); 00230 PC.printf("-----------------------------------------------------------------------------------------\n\r\n\r"); 00231 00232 PC.printf("\n\n\rInitial ESP...\n\n\r"); 00233 /* Initialize ESP wifi module */ 00234 Esp8266_Init(); 00235 00236 doorStatus = DOOR_IS_CLOSED; 00237 doorControl = CLOSE_DOOR; 00238 prevDoorControl = CLOSE_DOOR; 00239 PIR_Status = NO_MOVEMENT_DETECTED; 00240 garage_light_delay_counter = 0; 00241 if (!t1.isRunning()) 00242 { 00243 t1 = Ton::On; 00244 doorControl = ( eDoor_Command_t ) Read_from_Thingspeak(); 00245 } 00246 00247 PC.printf("\n\n\rStarting Periodic Interrupt...\n\n\r"); 00248 /* Trigger the interrupt periodically */ 00249 ticker.attach(Periodic_Interrupt, TIMER_PERIODICITY); 00250 00251 PC.printf("\n\n\rStarting Scheduler...\n\n\r"); 00252 scheduler = new SimpleScheduler; 00253 scheduler 00254 ->addTask( new SimpleTask(SCHEDULER_TASK_PERIODICITY, Task_500ms_Runnable) ) 00255 ; 00256 scheduler->run(); 00257 00258 /*! 00259 * Wait forever 00260 * ======================== 00261 */ 00262 while (1) 00263 { 00264 ; 00265 } 00266 }
Generated on Fri Jul 29 2022 09:34:43 by
1.7.2