IoT for mbed1
Dependencies: 4DGL-uLCD-SE IoTsecuritySys PinDetect mbed-rtos mbed
Fork of IoT by
Diff: main.cpp
- Revision:
- 9:413c094dd1e2
- Parent:
- 8:429fa05b4952
diff -r 429fa05b4952 -r 413c094dd1e2 main.cpp --- a/main.cpp Tue Dec 08 21:32:24 2015 +0000 +++ b/main.cpp Tue Dec 08 23:28:24 2015 +0000 @@ -74,7 +74,7 @@ //volatile bool code_enabled; volatile float IrVoltage = 0.0; int FindStrLocation(string sntsc, string word, string ptr); -volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4}; +enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4}; Statetype state = Armed; char charCode[5]; @@ -87,11 +87,9 @@ Semaphore Consul_Access(5); void Shiftbright_thread(void const *args); -void IR_thread(void const *args); void LCD_Code_Enter_Thread(void const *args); void uLCD_thread(void const *args); void RGB_LED(int red, int green, int blue); -void Speaker_thread(void const *args); void Ethernet_thread(void const *args); bool Ethernet_massage_Send(string args); void Activate_Lock(); @@ -135,17 +133,17 @@ case Alarm_ON: if(key_code == 12 ){ state = Armed; - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("UpdateStatus"); } break; case Cleared: if(key_code == 12 ){ state = Armed; - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("UpdateStatus"); } else if (key_code == 11 ){ doorlock = 1; - flipper.attach(&Activate_Lock, 5.0); + flipper.attach(&Activate_Lock, 3.0); } break; } @@ -215,7 +213,7 @@ //pc.printf("3 times "); //PC_Access.unlock(); state = Alarm_ON; - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("UpdateStatus"); return(false); } } @@ -258,11 +256,9 @@ PC_Access.unlock(); Thread Ethernetthread(Ethernet_thread); wait(5); //Give the Ethernet connection some time to set up - //Thread IRthread(IR_thread); Thread Shiftbright(Shiftbright_thread); Thread LCDthread(uLCD_thread); - //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); - //Thread Speakerthread(Speaker_thread); + //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); // while loop constantly checks if the entered code sequence is right or wrong, // given that the correct amount of numbers were entered @@ -273,50 +269,15 @@ while (1){ switch(state){ case Armed: - - - - - - - - - - - - // while(1) { - - if (state == Armed) { - IrVoltage=IrSensor.read(); - if (IrVoltage <= 0.1) { //if value just nois reset timer - t.reset(); - state = Armed; - //Ethernet_massage_Send("UpdateStatus"); + IrVoltage=IrSensor.read(); + if (IrVoltage <= 0.1) { //if value just nois reset timer + t.reset(); + state = Armed; } - if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone - state = IR_sensed; - Ethernet_massage_Send("UpdateStatus"); - } - // Thread::wait(2000); - } - else { - //nothing to do for this thread make space for others - // Thread::wait(2000); - } - //} - - - - - - - - - - - - - + if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone + state = IR_sensed; + //Ethernet_massage_Send("UpdateStatus"); + } break; case Cleared: break; @@ -334,7 +295,7 @@ codeCounter = 0; //code_enabled = true; state = Second_Step; - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("UpdateStatus"); Ethernet_massage_Send("TextCode"); } else{ @@ -355,16 +316,16 @@ //PC_Access.unlock(); //wait(5); - //doorlock = 0;*/ + //pc.printf("Resetting....\n\r"); //pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r"); codeCounter = 0; //code_enabled = false; state = Cleared; - Ethernet_massage_Send("TextCode"); - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("TextCode"); + //Ethernet_massage_Send("UpdateStatus"); doorlock = 1; - flipper.attach(&Activate_Lock, 5.0); + flipper.attach(&Activate_Lock, 3.0); } else{ //PC_Access.lock(); @@ -439,34 +400,6 @@ } } - -void IR_thread(void const *args) { - - Timer t; - t.start(); - - while(1) { - - if (state == Armed) { - IrVoltage=IrSensor.read(); - if (IrVoltage <= 0.1) { //if value just nois reset timer - t.reset(); - state = Armed; - Ethernet_massage_Send("UpdateStatus"); - } - if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone - state = IR_sensed; - Ethernet_massage_Send("UpdateStatus"); - } - Thread::wait(2000); - } - else { - //nothing to do for this thread make space for others - Thread::wait(2000); - } - } -} - void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; @@ -525,8 +458,8 @@ LCD_Access.lock(); uLCD.text_width(4); uLCD.text_height(4); - LCD_Access.unlock(); - LCD_Access.lock(); + //LCD_Access.unlock(); + //LCD_Access.lock(); uLCD.color(RED); uLCD.locate(1,2); uLCD.printf("%2D",i); @@ -539,7 +472,7 @@ uLCD.cls(); LCD_Access.unlock(); state = Alarm_ON; - Ethernet_massage_Send("UpdateStatus"); + //Ethernet_massage_Send("UpdateStatus"); Ethernet_massage_Send("TextAlarm"); } @@ -569,7 +502,7 @@ break; } } - Thread::wait(500); + Thread::wait(100); } } @@ -615,27 +548,21 @@ } } -void Speaker_thread(void const *args) { - while (1) { - if (state == Alarm_ON) { - mySpeaker.PlaySong(note,duration); - Thread::wait(2000); - } - } -} - void Ethernet_thread(void const *args) { char buffer[300]; - + int tempstatus = state; int ret,found; eth.init(); //Use DHCP eth.connect(); //pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); while (1) { - Thread::wait(3000); - int timeout; - + Thread::wait(1000); + if (tempstatus !=state) { + Ethernet_massage_Send("UpdateStatus"); + Thread::wait(1500); + tempstatus = state; + } //t2.start(); TCPSocketConnection sock; sock.connect("dreamphysix.com", 80); @@ -667,16 +594,12 @@ //pc.printf("\n\rhttp_cmd: %c\n\r",buffer[found+7]); //pc.printf("\n\state: %i\n\r",state); int dummy = (buffer[found+7])-48; - //int dummy = ((int)buffer[found+7]); - //dummy = atoi(dummy); state = (Statetype)dummy; //state = (Statetype)buffer[found+7]; PC_Access.lock(); - pc.printf("\n\state: %i\n\r",dummy); + pc.printf("\nstate: %i\n\r",dummy); PC_Access.unlock(); //Thread::wait(3000); - - } } @@ -759,20 +682,21 @@ snprintf(buffer, ret, "%c",buffer); //pc.printf("buffer: %s", buffer); if (strstr(buffer,"True") != NULL) { - //pc.printf("true fron eth check"); + pc.printf("true fron eth check"); return true; } else if (strstr(buffer,"False") != NULL) { - //pc.printf("false fron eth check"); + pc.printf("false fron eth check"); return false; } else { - //pc.printf("default "); + pc.printf("default "); return true; } - Thread::wait(1000); } void Activate_Lock(){ doorlock =! doorlock; } + +