Hw6
Dependencies: C12832_lcd LM75B MMA7660 NTPClient WiflyInterface HOMEWORK_6 mbed
main.cpp
00001 /****************************************************************************************/ 00002 /* INTERNET OF THINGS ASSIGNMENT 6 */ 00003 /*This assignment implements a thermostat which turns ON/OFF depending on the set and */ 00004 /*current temperature. The thermostat can be turned ON/OFF by following ways: */ 00005 /* 1. Joustick up button on the application board. */ 00006 /* 2. Depending on if there is movement or not */ 00007 /* 3. Remotely through wifly(Telnet) */ 00008 /****************************************************************************************/ 00009 00010 #include "mbed.h" 00011 #include "rtos.h" 00012 #include "C12832_lcd.h" 00013 #include "NTPClient.h" 00014 #include "WiflyInterface.h" 00015 #include "LM75B.h" 00016 #include "DebouncedIn.h" 00017 #include "MMA7660.h" 00018 00019 #define SEC 1000 //macro for no. of msec in sec 00020 #define MIN 60*SEC //macro for no. of msec in min 00021 #define hys 3 00022 #define TIME 20 //Time after which system should turn OFF if 00023 //no movement detected 00024 #define ECHO_SERVER_PORT 7 //Server port for HTTP client. 00025 00026 Mutex LCD; 00027 C12832_LCD lcd; 00028 Serial pc (USBTX,USBRX); 00029 LM75B current(p28,p27); 00030 MMA7660 MMA(p28,p27); 00031 BusOut move(p23,p24,p25); 00032 00033 //System ON/OFF 00034 BusIn heat_on (p16,p13); 00035 00036 //Increase Temperature 00037 DebouncedIn temp_up (p15); 00038 00039 //Decrease Temperature 00040 DebouncedIn temp_down (p12); 00041 00042 //System ON/OFF LED 00043 DigitalOut thermostat (LED1); 00044 00045 //Heater ON/OFF LED. This can be furthur connected to the relay 00046 DigitalOut heater (LED2); 00047 00048 00049 int temp = 10; 00050 bool status = 0; 00051 00052 void update_lcd(void const *args); //Thread 1 00053 void Thermostat_logic (void const *args); //Thread 2 00054 void check_movement(void const *args); //Thread 3 00055 00056 bool no_move; 00057 float acc_x = MMA.x(); 00058 float acc_y = MMA.y(); 00059 float acc_z = MMA.z(); 00060 float acc_x_old,acc_y_old,acc_z_old; 00061 bool wifly_on = 0; 00062 00063 WiflyInterface wifly(p9, p10, p30, p29, "MY_WIFI", "", NONE); 00064 00065 /****************************************************************************************/ 00066 /*Thread update_lcd: This thread is used to update the lcd. The LCD will dispaly current*/ 00067 /*time, Set temperature and the actual temperature. If the system is OFF lcd will show */ 00068 /*current time and message 'System OFF' */ 00069 /****************************************************************************************/ 00070 void update_lcd(void const *args) 00071 { 00072 set_time(1391739990); 00073 while (true) { 00074 time_t seconds = time(NULL); 00075 LCD.lock(); 00076 lcd.locate(0,0); 00077 lcd.printf("%s",ctime(&seconds)); 00078 lcd.locate(0,10); 00079 if (status) { 00080 lcd.printf("Current: %.2f",current.read()); 00081 lcd.locate(1,20); 00082 lcd.printf("Set: %d",temp); 00083 } else { 00084 lcd.printf("System OFF"); 00085 } 00086 LCD.unlock(); 00087 Thread::wait(200); //LCD will update after every 200msec. 00088 } 00089 } 00090 /****************************************************************************************/ 00091 00092 /****************************************************************************************/ 00093 /*Thread: thermostat logic: This thread implements the logic of the thermostat. It turns*/ 00094 /*ON/OFF the thermostat depending on temperature, movement and the web control. It also */ 00095 /*allows the user to set the temperature from the push button switches. The setting of */ 00096 /*temperature can also be done remotely through xively. Currently the program just */ 00097 /*controls turning ON/OFF the system remotely. */ 00098 /****************************************************************************************/ 00099 void Thermostat_logic(void const *args) 00100 { 00101 while (true) { 00102 if (heat_on == 0x2 || wifly_on || !no_move) 00103 { 00104 thermostat = 1; 00105 status = 1; 00106 wifly_on = 1; 00107 } else if ( wifly_on == 0 ){ 00108 thermostat = 0; 00109 heater = 0; 00110 status = 0; 00111 wifly_on = 0; 00112 } 00113 else if (heat_on == 0x1 || no_move ) { 00114 thermostat = 0; 00115 heater = 0; 00116 status = 0; 00117 wifly_on = 0; 00118 } 00119 00120 /*If the joystick is pushed upwards increase set temperature by 2 00121 And print the set temperature on LCD.*/ 00122 if (temp_up.rising()) { 00123 temp = temp + 0x2; 00124 } 00125 00126 /*else if the joystick is pushed downwards decrease set temperature by 2 00127 And print the set temperature on LCD.*/ 00128 00129 else if (temp_down.rising()) { 00130 temp = temp - 0x2; 00131 } 00132 00133 //Comparison logic and turn Heater ON/OFF 00134 if ((temp > (current.read()+ hys)) && thermostat == 1) 00135 heater = 1; 00136 else if ((temp < (current.read()- hys)) || thermostat == 0) 00137 heater = 0; 00138 00139 if (acc_x_old != MMA.x() || acc_y_old != MMA.y() || acc_z_old != MMA.x()) 00140 { 00141 no_move = 0; 00142 } 00143 00144 Thread::wait(100); //Temperature comparison will take place after 00145 //every 100msec. 00146 } 00147 } 00148 /****************************************************************************************/ 00149 00150 00151 /****************************************************************************************/ 00152 /*Thread check movement: This thread detects if there is movement nearby the thermostat.*/ 00153 /*If there is no movement this thread sets a variable called no_move which is furthur */ 00154 /*used to turn ON/OFF the system. Logic implemented for movement detection is as follows*/ 00155 /*This thread is executed once every minute. Every time this thread is executed it */ 00156 /*compares the accelerometer reading with its previous value. If the reading is same */ 00157 /*(no movement detected) it increments a counter. When this counter reaches 20 (which */ 00158 /*means there is no movement for 20 mins) it sets the variable no_move to turn OFF the */ 00159 /*system. When a different accelerometer value is detected(movement present) it resets */ 00160 /*the variable which will in turn turn the system ON. */ 00161 /****************************************************************************************/ 00162 void check_movement(void const *args) 00163 { 00164 static int move_cntr = 0; 00165 while (true) { 00166 acc_x_old = acc_x; 00167 acc_y_old = acc_y; 00168 acc_z_old = acc_z; 00169 acc_x = MMA.x(); 00170 acc_y = MMA.y(); 00171 acc_z = MMA.z(); 00172 if (acc_x_old == acc_x && acc_y_old == acc_y && acc_z_old == acc_z) 00173 { 00174 move_cntr++; 00175 pc.printf("Value of move_cntr = %d\r\n",move_cntr); 00176 move = 011; 00177 } 00178 else { 00179 move_cntr = 0; 00180 pc.printf("Move_cntr reset\r\n");} 00181 if (move_cntr >= TIME) //If the Accelerometer value remains constant for 20 mins no movement detected 00182 no_move = 1; 00183 else 00184 no_move = 0; 00185 Thread::wait(1*MIN); 00186 } 00187 00188 } 00189 /****************************************************************************************/ 00190 00191 00192 /****************************************************************************************/ 00193 /*Thread main: This is the main thread which instantiates all other threads. The main */ 00194 /*thread also initializes the wifly module and communicates with the mbed via wifly. */ 00195 /*If the HTTP client is not connected, the main thread continues its operation through */ 00196 /*the on board joystick. Turning ON/OFF the thermostat remotely is not possible in such */ 00197 /*a case. The other controls like the joystick and the accelorometer operate normally. */ 00198 /****************************************************************************************/ 00199 int main() { 00200 00201 Thread lcd_display(update_lcd,NULL, osPriorityAboveNormal); 00202 Thread thermostat_thread(Thermostat_logic,NULL, osPriorityAboveNormal); 00203 Thread accel_thread(check_movement,NULL,osPriorityAboveNormal); 00204 00205 wifly.init(); //Use DHCP 00206 while (!wifly.connect()); 00207 pc.printf("IP Address is %s\n\r", wifly.getIPAddress()); 00208 00209 TCPSocketServer server; 00210 00211 server.bind(ECHO_SERVER_PORT); 00212 server.listen(); 00213 00214 printf("\nWait for new connection...\r\n"); 00215 TCPSocketConnection client; 00216 server.accept(client); 00217 char buffer[3]; 00218 // NTPClient ntp; 00219 //ntp.setTime("nist1.symmetricom.com"); 00220 00221 while (true) { 00222 if (client.is_connected()){ 00223 int n = client.receive(buffer, sizeof(buffer)); 00224 if (n <= 0)continue; 00225 buffer[n] = 0; 00226 pc.printf("Buffer is %s\r\n",buffer); 00227 if (!(strcmp(buffer,"o"))) 00228 wifly_on = 1; 00229 else if (!(strcmp(buffer,"f"))){ 00230 wifly_on = 0; 00231 pc.printf("wifi off\r\n"); 00232 } 00233 Thread::wait(100); 00234 client.send_all(buffer,n); 00235 pc.printf("Sent data is %s\r\n",buffer); 00236 } 00237 else 00238 Thread::wait(1*SEC); 00239 Thread::wait(100); //This wait is necessary for the condition in which http 00240 //client is connected but it does not receive anything. 00241 //if n<= 0 00242 } 00243 } 00244 /****************************************************************************************/
Generated on Fri Jul 15 2022 09:55:01 by 1.7.2