Hw6

Dependencies:   C12832_lcd LM75B MMA7660 NTPClient WiflyInterface HOMEWORK_6 mbed

Committer:
bhakti08
Date:
Mon Jun 09 21:12:22 2014 +0000
Revision:
0:ec9669ff706c
Homework 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhakti08 0:ec9669ff706c 1 /****************************************************************************************/
bhakti08 0:ec9669ff706c 2 /* INTERNET OF THINGS ASSIGNMENT 6 */
bhakti08 0:ec9669ff706c 3 /*This assignment implements a thermostat which turns ON/OFF depending on the set and */
bhakti08 0:ec9669ff706c 4 /*current temperature. The thermostat can be turned ON/OFF by following ways: */
bhakti08 0:ec9669ff706c 5 /* 1. Joustick up button on the application board. */
bhakti08 0:ec9669ff706c 6 /* 2. Depending on if there is movement or not */
bhakti08 0:ec9669ff706c 7 /* 3. Remotely through wifly(Telnet) */
bhakti08 0:ec9669ff706c 8 /****************************************************************************************/
bhakti08 0:ec9669ff706c 9
bhakti08 0:ec9669ff706c 10 #include "mbed.h"
bhakti08 0:ec9669ff706c 11 #include "rtos.h"
bhakti08 0:ec9669ff706c 12 #include "C12832_lcd.h"
bhakti08 0:ec9669ff706c 13 #include "NTPClient.h"
bhakti08 0:ec9669ff706c 14 #include "WiflyInterface.h"
bhakti08 0:ec9669ff706c 15 #include "LM75B.h"
bhakti08 0:ec9669ff706c 16 #include "DebouncedIn.h"
bhakti08 0:ec9669ff706c 17 #include "MMA7660.h"
bhakti08 0:ec9669ff706c 18
bhakti08 0:ec9669ff706c 19 #define SEC 1000 //macro for no. of msec in sec
bhakti08 0:ec9669ff706c 20 #define MIN 60*SEC //macro for no. of msec in min
bhakti08 0:ec9669ff706c 21 #define hys 3
bhakti08 0:ec9669ff706c 22 #define TIME 20 //Time after which system should turn OFF if
bhakti08 0:ec9669ff706c 23 //no movement detected
bhakti08 0:ec9669ff706c 24 #define ECHO_SERVER_PORT 7 //Server port for HTTP client.
bhakti08 0:ec9669ff706c 25
bhakti08 0:ec9669ff706c 26 Mutex LCD;
bhakti08 0:ec9669ff706c 27 C12832_LCD lcd;
bhakti08 0:ec9669ff706c 28 Serial pc (USBTX,USBRX);
bhakti08 0:ec9669ff706c 29 LM75B current(p28,p27);
bhakti08 0:ec9669ff706c 30 MMA7660 MMA(p28,p27);
bhakti08 0:ec9669ff706c 31 BusOut move(p23,p24,p25);
bhakti08 0:ec9669ff706c 32
bhakti08 0:ec9669ff706c 33 //System ON/OFF
bhakti08 0:ec9669ff706c 34 BusIn heat_on (p16,p13);
bhakti08 0:ec9669ff706c 35
bhakti08 0:ec9669ff706c 36 //Increase Temperature
bhakti08 0:ec9669ff706c 37 DebouncedIn temp_up (p15);
bhakti08 0:ec9669ff706c 38
bhakti08 0:ec9669ff706c 39 //Decrease Temperature
bhakti08 0:ec9669ff706c 40 DebouncedIn temp_down (p12);
bhakti08 0:ec9669ff706c 41
bhakti08 0:ec9669ff706c 42 //System ON/OFF LED
bhakti08 0:ec9669ff706c 43 DigitalOut thermostat (LED1);
bhakti08 0:ec9669ff706c 44
bhakti08 0:ec9669ff706c 45 //Heater ON/OFF LED. This can be furthur connected to the relay
bhakti08 0:ec9669ff706c 46 DigitalOut heater (LED2);
bhakti08 0:ec9669ff706c 47
bhakti08 0:ec9669ff706c 48
bhakti08 0:ec9669ff706c 49 int temp = 10;
bhakti08 0:ec9669ff706c 50 bool status = 0;
bhakti08 0:ec9669ff706c 51
bhakti08 0:ec9669ff706c 52 void update_lcd(void const *args); //Thread 1
bhakti08 0:ec9669ff706c 53 void Thermostat_logic (void const *args); //Thread 2
bhakti08 0:ec9669ff706c 54 void check_movement(void const *args); //Thread 3
bhakti08 0:ec9669ff706c 55
bhakti08 0:ec9669ff706c 56 bool no_move;
bhakti08 0:ec9669ff706c 57 float acc_x = MMA.x();
bhakti08 0:ec9669ff706c 58 float acc_y = MMA.y();
bhakti08 0:ec9669ff706c 59 float acc_z = MMA.z();
bhakti08 0:ec9669ff706c 60 float acc_x_old,acc_y_old,acc_z_old;
bhakti08 0:ec9669ff706c 61 bool wifly_on = 0;
bhakti08 0:ec9669ff706c 62
bhakti08 0:ec9669ff706c 63 WiflyInterface wifly(p9, p10, p30, p29, "MY_WIFI", "", NONE);
bhakti08 0:ec9669ff706c 64
bhakti08 0:ec9669ff706c 65 /****************************************************************************************/
bhakti08 0:ec9669ff706c 66 /*Thread update_lcd: This thread is used to update the lcd. The LCD will dispaly current*/
bhakti08 0:ec9669ff706c 67 /*time, Set temperature and the actual temperature. If the system is OFF lcd will show */
bhakti08 0:ec9669ff706c 68 /*current time and message 'System OFF' */
bhakti08 0:ec9669ff706c 69 /****************************************************************************************/
bhakti08 0:ec9669ff706c 70 void update_lcd(void const *args)
bhakti08 0:ec9669ff706c 71 {
bhakti08 0:ec9669ff706c 72 set_time(1391739990);
bhakti08 0:ec9669ff706c 73 while (true) {
bhakti08 0:ec9669ff706c 74 time_t seconds = time(NULL);
bhakti08 0:ec9669ff706c 75 LCD.lock();
bhakti08 0:ec9669ff706c 76 lcd.locate(0,0);
bhakti08 0:ec9669ff706c 77 lcd.printf("%s",ctime(&seconds));
bhakti08 0:ec9669ff706c 78 lcd.locate(0,10);
bhakti08 0:ec9669ff706c 79 if (status) {
bhakti08 0:ec9669ff706c 80 lcd.printf("Current: %.2f",current.read());
bhakti08 0:ec9669ff706c 81 lcd.locate(1,20);
bhakti08 0:ec9669ff706c 82 lcd.printf("Set: %d",temp);
bhakti08 0:ec9669ff706c 83 } else {
bhakti08 0:ec9669ff706c 84 lcd.printf("System OFF");
bhakti08 0:ec9669ff706c 85 }
bhakti08 0:ec9669ff706c 86 LCD.unlock();
bhakti08 0:ec9669ff706c 87 Thread::wait(200); //LCD will update after every 200msec.
bhakti08 0:ec9669ff706c 88 }
bhakti08 0:ec9669ff706c 89 }
bhakti08 0:ec9669ff706c 90 /****************************************************************************************/
bhakti08 0:ec9669ff706c 91
bhakti08 0:ec9669ff706c 92 /****************************************************************************************/
bhakti08 0:ec9669ff706c 93 /*Thread: thermostat logic: This thread implements the logic of the thermostat. It turns*/
bhakti08 0:ec9669ff706c 94 /*ON/OFF the thermostat depending on temperature, movement and the web control. It also */
bhakti08 0:ec9669ff706c 95 /*allows the user to set the temperature from the push button switches. The setting of */
bhakti08 0:ec9669ff706c 96 /*temperature can also be done remotely through xively. Currently the program just */
bhakti08 0:ec9669ff706c 97 /*controls turning ON/OFF the system remotely. */
bhakti08 0:ec9669ff706c 98 /****************************************************************************************/
bhakti08 0:ec9669ff706c 99 void Thermostat_logic(void const *args)
bhakti08 0:ec9669ff706c 100 {
bhakti08 0:ec9669ff706c 101 while (true) {
bhakti08 0:ec9669ff706c 102 if (heat_on == 0x2 || wifly_on || !no_move)
bhakti08 0:ec9669ff706c 103 {
bhakti08 0:ec9669ff706c 104 thermostat = 1;
bhakti08 0:ec9669ff706c 105 status = 1;
bhakti08 0:ec9669ff706c 106 wifly_on = 1;
bhakti08 0:ec9669ff706c 107 } else if ( wifly_on == 0 ){
bhakti08 0:ec9669ff706c 108 thermostat = 0;
bhakti08 0:ec9669ff706c 109 heater = 0;
bhakti08 0:ec9669ff706c 110 status = 0;
bhakti08 0:ec9669ff706c 111 wifly_on = 0;
bhakti08 0:ec9669ff706c 112 }
bhakti08 0:ec9669ff706c 113 else if (heat_on == 0x1 || no_move ) {
bhakti08 0:ec9669ff706c 114 thermostat = 0;
bhakti08 0:ec9669ff706c 115 heater = 0;
bhakti08 0:ec9669ff706c 116 status = 0;
bhakti08 0:ec9669ff706c 117 wifly_on = 0;
bhakti08 0:ec9669ff706c 118 }
bhakti08 0:ec9669ff706c 119
bhakti08 0:ec9669ff706c 120 /*If the joystick is pushed upwards increase set temperature by 2
bhakti08 0:ec9669ff706c 121 And print the set temperature on LCD.*/
bhakti08 0:ec9669ff706c 122 if (temp_up.rising()) {
bhakti08 0:ec9669ff706c 123 temp = temp + 0x2;
bhakti08 0:ec9669ff706c 124 }
bhakti08 0:ec9669ff706c 125
bhakti08 0:ec9669ff706c 126 /*else if the joystick is pushed downwards decrease set temperature by 2
bhakti08 0:ec9669ff706c 127 And print the set temperature on LCD.*/
bhakti08 0:ec9669ff706c 128
bhakti08 0:ec9669ff706c 129 else if (temp_down.rising()) {
bhakti08 0:ec9669ff706c 130 temp = temp - 0x2;
bhakti08 0:ec9669ff706c 131 }
bhakti08 0:ec9669ff706c 132
bhakti08 0:ec9669ff706c 133 //Comparison logic and turn Heater ON/OFF
bhakti08 0:ec9669ff706c 134 if ((temp > (current.read()+ hys)) && thermostat == 1)
bhakti08 0:ec9669ff706c 135 heater = 1;
bhakti08 0:ec9669ff706c 136 else if ((temp < (current.read()- hys)) || thermostat == 0)
bhakti08 0:ec9669ff706c 137 heater = 0;
bhakti08 0:ec9669ff706c 138
bhakti08 0:ec9669ff706c 139 if (acc_x_old != MMA.x() || acc_y_old != MMA.y() || acc_z_old != MMA.x())
bhakti08 0:ec9669ff706c 140 {
bhakti08 0:ec9669ff706c 141 no_move = 0;
bhakti08 0:ec9669ff706c 142 }
bhakti08 0:ec9669ff706c 143
bhakti08 0:ec9669ff706c 144 Thread::wait(100); //Temperature comparison will take place after
bhakti08 0:ec9669ff706c 145 //every 100msec.
bhakti08 0:ec9669ff706c 146 }
bhakti08 0:ec9669ff706c 147 }
bhakti08 0:ec9669ff706c 148 /****************************************************************************************/
bhakti08 0:ec9669ff706c 149
bhakti08 0:ec9669ff706c 150
bhakti08 0:ec9669ff706c 151 /****************************************************************************************/
bhakti08 0:ec9669ff706c 152 /*Thread check movement: This thread detects if there is movement nearby the thermostat.*/
bhakti08 0:ec9669ff706c 153 /*If there is no movement this thread sets a variable called no_move which is furthur */
bhakti08 0:ec9669ff706c 154 /*used to turn ON/OFF the system. Logic implemented for movement detection is as follows*/
bhakti08 0:ec9669ff706c 155 /*This thread is executed once every minute. Every time this thread is executed it */
bhakti08 0:ec9669ff706c 156 /*compares the accelerometer reading with its previous value. If the reading is same */
bhakti08 0:ec9669ff706c 157 /*(no movement detected) it increments a counter. When this counter reaches 20 (which */
bhakti08 0:ec9669ff706c 158 /*means there is no movement for 20 mins) it sets the variable no_move to turn OFF the */
bhakti08 0:ec9669ff706c 159 /*system. When a different accelerometer value is detected(movement present) it resets */
bhakti08 0:ec9669ff706c 160 /*the variable which will in turn turn the system ON. */
bhakti08 0:ec9669ff706c 161 /****************************************************************************************/
bhakti08 0:ec9669ff706c 162 void check_movement(void const *args)
bhakti08 0:ec9669ff706c 163 {
bhakti08 0:ec9669ff706c 164 static int move_cntr = 0;
bhakti08 0:ec9669ff706c 165 while (true) {
bhakti08 0:ec9669ff706c 166 acc_x_old = acc_x;
bhakti08 0:ec9669ff706c 167 acc_y_old = acc_y;
bhakti08 0:ec9669ff706c 168 acc_z_old = acc_z;
bhakti08 0:ec9669ff706c 169 acc_x = MMA.x();
bhakti08 0:ec9669ff706c 170 acc_y = MMA.y();
bhakti08 0:ec9669ff706c 171 acc_z = MMA.z();
bhakti08 0:ec9669ff706c 172 if (acc_x_old == acc_x && acc_y_old == acc_y && acc_z_old == acc_z)
bhakti08 0:ec9669ff706c 173 {
bhakti08 0:ec9669ff706c 174 move_cntr++;
bhakti08 0:ec9669ff706c 175 pc.printf("Value of move_cntr = %d\r\n",move_cntr);
bhakti08 0:ec9669ff706c 176 move = 011;
bhakti08 0:ec9669ff706c 177 }
bhakti08 0:ec9669ff706c 178 else {
bhakti08 0:ec9669ff706c 179 move_cntr = 0;
bhakti08 0:ec9669ff706c 180 pc.printf("Move_cntr reset\r\n");}
bhakti08 0:ec9669ff706c 181 if (move_cntr >= TIME) //If the Accelerometer value remains constant for 20 mins no movement detected
bhakti08 0:ec9669ff706c 182 no_move = 1;
bhakti08 0:ec9669ff706c 183 else
bhakti08 0:ec9669ff706c 184 no_move = 0;
bhakti08 0:ec9669ff706c 185 Thread::wait(1*MIN);
bhakti08 0:ec9669ff706c 186 }
bhakti08 0:ec9669ff706c 187
bhakti08 0:ec9669ff706c 188 }
bhakti08 0:ec9669ff706c 189 /****************************************************************************************/
bhakti08 0:ec9669ff706c 190
bhakti08 0:ec9669ff706c 191
bhakti08 0:ec9669ff706c 192 /****************************************************************************************/
bhakti08 0:ec9669ff706c 193 /*Thread main: This is the main thread which instantiates all other threads. The main */
bhakti08 0:ec9669ff706c 194 /*thread also initializes the wifly module and communicates with the mbed via wifly. */
bhakti08 0:ec9669ff706c 195 /*If the HTTP client is not connected, the main thread continues its operation through */
bhakti08 0:ec9669ff706c 196 /*the on board joystick. Turning ON/OFF the thermostat remotely is not possible in such */
bhakti08 0:ec9669ff706c 197 /*a case. The other controls like the joystick and the accelorometer operate normally. */
bhakti08 0:ec9669ff706c 198 /****************************************************************************************/
bhakti08 0:ec9669ff706c 199 int main() {
bhakti08 0:ec9669ff706c 200
bhakti08 0:ec9669ff706c 201 Thread lcd_display(update_lcd,NULL, osPriorityAboveNormal);
bhakti08 0:ec9669ff706c 202 Thread thermostat_thread(Thermostat_logic,NULL, osPriorityAboveNormal);
bhakti08 0:ec9669ff706c 203 Thread accel_thread(check_movement,NULL,osPriorityAboveNormal);
bhakti08 0:ec9669ff706c 204
bhakti08 0:ec9669ff706c 205 wifly.init(); //Use DHCP
bhakti08 0:ec9669ff706c 206 while (!wifly.connect());
bhakti08 0:ec9669ff706c 207 pc.printf("IP Address is %s\n\r", wifly.getIPAddress());
bhakti08 0:ec9669ff706c 208
bhakti08 0:ec9669ff706c 209 TCPSocketServer server;
bhakti08 0:ec9669ff706c 210
bhakti08 0:ec9669ff706c 211 server.bind(ECHO_SERVER_PORT);
bhakti08 0:ec9669ff706c 212 server.listen();
bhakti08 0:ec9669ff706c 213
bhakti08 0:ec9669ff706c 214 printf("\nWait for new connection...\r\n");
bhakti08 0:ec9669ff706c 215 TCPSocketConnection client;
bhakti08 0:ec9669ff706c 216 server.accept(client);
bhakti08 0:ec9669ff706c 217 char buffer[3];
bhakti08 0:ec9669ff706c 218 // NTPClient ntp;
bhakti08 0:ec9669ff706c 219 //ntp.setTime("nist1.symmetricom.com");
bhakti08 0:ec9669ff706c 220
bhakti08 0:ec9669ff706c 221 while (true) {
bhakti08 0:ec9669ff706c 222 if (client.is_connected()){
bhakti08 0:ec9669ff706c 223 int n = client.receive(buffer, sizeof(buffer));
bhakti08 0:ec9669ff706c 224 if (n <= 0)continue;
bhakti08 0:ec9669ff706c 225 buffer[n] = 0;
bhakti08 0:ec9669ff706c 226 pc.printf("Buffer is %s\r\n",buffer);
bhakti08 0:ec9669ff706c 227 if (!(strcmp(buffer,"o")))
bhakti08 0:ec9669ff706c 228 wifly_on = 1;
bhakti08 0:ec9669ff706c 229 else if (!(strcmp(buffer,"f"))){
bhakti08 0:ec9669ff706c 230 wifly_on = 0;
bhakti08 0:ec9669ff706c 231 pc.printf("wifi off\r\n");
bhakti08 0:ec9669ff706c 232 }
bhakti08 0:ec9669ff706c 233 Thread::wait(100);
bhakti08 0:ec9669ff706c 234 client.send_all(buffer,n);
bhakti08 0:ec9669ff706c 235 pc.printf("Sent data is %s\r\n",buffer);
bhakti08 0:ec9669ff706c 236 }
bhakti08 0:ec9669ff706c 237 else
bhakti08 0:ec9669ff706c 238 Thread::wait(1*SEC);
bhakti08 0:ec9669ff706c 239 Thread::wait(100); //This wait is necessary for the condition in which http
bhakti08 0:ec9669ff706c 240 //client is connected but it does not receive anything.
bhakti08 0:ec9669ff706c 241 //if n<= 0
bhakti08 0:ec9669ff706c 242 }
bhakti08 0:ec9669ff706c 243 }
bhakti08 0:ec9669ff706c 244 /****************************************************************************************/