Tal Landes / Mbed 2 deprecated IoT

Dependencies:   4DGL-uLCD-SE ECE4180_Touchpad_V2 IoTsecuritySys PinDetect mbed-rtos mbed

Fork of ECE4180_Touchpad_V2 by Jacob Smith

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <mbed.h>
00002 #include <string>
00003 #include <iostream>
00004 #include "rtos.h"
00005 #include <mpr121.h>
00006 #include <stdlib.h>
00007 #include "PinDetect.h"
00008 #include "uLCD_4DGL.h"
00009 #include "SongPlayer.h"
00010 #include "Speaker.h"
00011 #include "EthernetInterface.h"
00012 /* CODE_LENGTH needs to be double the amount of numbers you
00013       want in your authenticator/passcode because of the way interrupt.fall(&fallInterrupt)
00014       works. It is called twice every time an interrupt is detected.
00015       The extra numbers in the array will just be filled with zeros and ignored in checking 
00016       code sequences, so they will not matter either way */
00017       /* i.e, you want a code with 7 numbers, CODE_LENGTH needs to be 14 */
00018 #define CODE_LENGTH 8
00019 
00020 DigitalOut led1(LED1);
00021 DigitalOut led2(LED2);
00022 DigitalOut led3(LED3);
00023 DigitalOut led4(LED4);
00024 
00025 DigitalOut doorlock(p21);
00026 
00027 
00028 //uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
00029 uLCD_4DGL uLCD(p28, p27, p29); 
00030 
00031 // Create the interrupt receiver object on pin 26
00032 InterruptIn interrupt(p30);
00033 
00034 // Setup the i2c bus on pins 9 and 10
00035 I2C i2c(p9, p10);
00036 
00037 // Setup the Mpr121:
00038 // constructor(i2c object, i2c address of the mpr121)
00039 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00040 
00041 // pc serial communication for testing
00042 Serial pc(USBTX, USBRX);
00043 
00044 //Set up IR sensor 
00045 AnalogIn IrSensor(p20);
00046 
00047 //Shiftbright
00048 DigitalOut latch(p15);
00049 DigitalOut enable(p16);
00050 //AnalogOut DACout(p18);
00051 //Cycles through different colors on RGB LED
00052 SPI spi(p11, p12, p13);
00053 
00054 
00055 SongPlayer mySpeaker(p26);
00056 Speaker NotePlayer(p26);
00057 
00058 // ethernet setup    
00059 EthernetInterface eth;
00060 
00061 //Lock timeout 
00062 Timeout flipper;
00063 
00064 // ***** GLOBALS ***** //
00065 // Timer is to seed rand
00066 string ID = "1";
00067 volatile int NumTry = 0; 
00068 Timer t1;
00069 Timer t2;
00070 // code counter is the next position in inputCode array
00071 volatile int codeCounter;
00072 // inputCode array is the sequence of numbers the user will enter
00073 volatile int inputCode[CODE_LENGTH];
00074 //volatile bool code_enabled;
00075 volatile float IrVoltage = 0.0;
00076 int FindStrLocation(string sntsc, string word, string ptr);
00077 volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
00078 Statetype state = Armed;
00079 char charCode[5];
00080 
00081 float note[18]= {1568.0,1396.9};
00082 float duration[18]= {0.48,0.24};
00083 
00084 Mutex LCD_Access; 
00085 Mutex PC_Access;
00086 Mutex Eth_Lock;
00087 Semaphore Consul_Access(5);
00088 
00089 void Shiftbright_thread(void const *args);
00090 void IR_thread(void const *args);
00091 void LCD_Code_Enter_Thread(void const *args);
00092 void uLCD_thread(void const *args);
00093 void RGB_LED(int red, int green, int blue);
00094 void Speaker_thread(void const *args);
00095 void Ethernet_thread(void const *args);
00096 bool Ethernet_massage_Send(string args);
00097 void Activate_Lock();
00098 void init_LCD();
00099 // Key hit/release interrupt routine
00100 void fallInterrupt() {
00101     int key_code=0;
00102     int i=0;
00103     int value=mpr121.read(0x00);
00104     value +=mpr121.read(0x01)<<8;
00105     // LED demo mod
00106     i=0;
00107     // puts key number out to LEDs for demo
00108     for (i=0; i<12; i++) {
00109     if (((value>>i)&0x01)==1) key_code=i+1;
00110     }
00111     led4=key_code & 0x01;
00112     led3=(key_code>>1) & 0x01;
00113     led2=(key_code>>2) & 0x01;
00114     led1=(key_code>>3) & 0x01;
00115   
00116     // save the keypress to inputCode array
00117     switch (state) {
00118         case Armed:
00119         break;
00120         case IR_sensed:
00121         case Second_Step:
00122             if(codeCounter < CODE_LENGTH){
00123             // ignore odd numbers
00124             if(codeCounter % 2 != 0){
00125                 inputCode[codeCounter] = 0;
00126             }
00127             // only save the even numbers (see lines 6-10)
00128             else{
00129                 inputCode[codeCounter] = key_code - 1;
00130                 //pc.printf("codeCounter: %d  --  code:  %d\n\r", codeCounter, key_code - 1);
00131             }      
00132             codeCounter++;
00133         }
00134         break;
00135         case Alarm_ON:
00136             if(key_code == 12 ){
00137                 state = Armed;
00138                 Ethernet_massage_Send("UpdateStatus");
00139             }
00140         break; 
00141         case Cleared:
00142             if(key_code == 12 ){
00143                 state = Armed;
00144                 Ethernet_massage_Send("UpdateStatus");
00145                 }
00146             else if (key_code == 11 ){
00147                 doorlock = 1;  
00148                 flipper.attach(&Activate_Lock, 5.0);
00149                 }
00150         break;
00151         }
00152     
00153 }
00154 
00155 // generate randomized code
00156 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
00157     //int i = 0;
00158     // only care about the even numbers (see lines 6-10)
00159     //PC_Access.lock();
00160     //pc.printf("NEW CODE: ");
00161     //PC_Access.unlock();
00162     for(int i = 0; i < CODE_LENGTH; i+=2){
00163         srand(t1.read_us());
00164         codeArray[i] = (rand() % 9)+1; //nake code only 1-9
00165         PC_Access.lock();
00166         pc.printf("%d,  ", codeArray[i]);
00167         PC_Access.unlock();
00168     }
00169 
00170     snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]);
00171     //PC_Access.lock();
00172     //pc.printf("\n\r");
00173     //pc.printf("%s\n\r",charCode,codeArray[6]);
00174     //PC_Access.unlock();
00175     //Ethernet_massage_Send("TextCode");
00176 }
00177 
00178 // check if the code entered is the correct code
00179 bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){
00180     //int i = 0;
00181     int j = 0;
00182     // only care about the even numbers (see lines 6-10)
00183 
00184     if (NumTry < 3) {
00185         NumTry++;
00186     for(int i = 0; i < CODE_LENGTH; i+=2){
00187         
00188         if(inputCode[i] == codeArray[i]){
00189         j++; // count the number of right numbers
00190         }
00191          
00192     }
00193     if(j == CODE_LENGTH/2){
00194         /*for(int i = 0; i < CODE_LENGTH; i+=2){
00195             //inputCode[i] =0;
00196         }*/
00197         return(true);
00198     }
00199     else if  (Ethernet_massage_Send("GetTempCode")){
00200         /*for(int i = 0; i < CODE_LENGTH; i+=2){
00201              //inputCode[i] =0;
00202         }*/
00203         //pc.printf("return true");
00204         return(true);
00205     }
00206     else {
00207         /*for(int i = 0; i < CODE_LENGTH; i+=2){
00208             //inputCode[i] =0;
00209         }*/
00210        return(false);
00211     }
00212     }
00213     else {
00214         //PC_Access.lock();
00215         //pc.printf("3 times ");
00216         //PC_Access.unlock();
00217         state = Alarm_ON;
00218         Ethernet_massage_Send("UpdateStatus");
00219         return(false);
00220     }
00221 }
00222  
00223 int main() {
00224     interrupt.fall(&fallInterrupt);
00225     interrupt.mode(PullUp);
00226     pc.baud(921600);
00227     
00228     
00229     // authenticator is the randomly generated sequence of numbers by the machine
00230     // the user has to match this sequence to gain access, used for phase 2
00231     int authenticator[CODE_LENGTH];
00232     // passcode is the user's personal passcode, used for phase 1
00233     int passcode[CODE_LENGTH] = {1,0,2,0,3,0,4,0};//,4,0,5,0,6,0};
00234     codeCounter = 0;
00235     bool pass = false;
00236     
00237     // these 2 variables tell the machine when to generate a new random authentication code
00238     int new_code_timer = 0;
00239     int new_code_counter = 0;
00240     // this tells the state machine with phase of authentication we are in
00241     //code_enabled = false;
00242     
00243     for(int i = 0; i < CODE_LENGTH; i++){
00244       authenticator[i] = 0;
00245       inputCode[i] = 0;
00246     }
00247     
00248     // go ahead and start the timer so that when a random code is generated,
00249     // the seed will always be random, unlike the predecessor version
00250     t1.start();
00251     init_LCD();
00252     
00253     Timer t;
00254     t.start(); 
00255     //start threads:
00256     PC_Access.lock();
00257     pc.printf("\n\n\nSetting up Ethernet\n\r");
00258     PC_Access.unlock();
00259     Thread Ethernetthread(Ethernet_thread);
00260     wait(5); //Give the Ethernet connection some time to set up 
00261     //Thread IRthread(IR_thread);
00262     Thread Shiftbright(Shiftbright_thread);
00263     Thread LCDthread(uLCD_thread);
00264     //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); 
00265     //Thread Speakerthread(Speaker_thread);
00266     
00267     // while loop constantly checks if the entered code sequence is right or wrong,
00268     // given that the correct amount of numbers were entered
00269     PC_Access.lock();
00270     pc.printf("Ready\n\r");
00271     PC_Access.unlock();
00272     doorlock = 0; // make sure locked 
00273     while (1){
00274         switch(state){
00275             case Armed:
00276             
00277                 
00278                 
00279                 
00280                 
00281                 
00282                 
00283                 
00284                 
00285 
00286     
00287    // while(1) {
00288         
00289         if (state == Armed) {
00290             IrVoltage=IrSensor.read();
00291             if (IrVoltage <= 0.1) { //if value just nois reset timer
00292                 t.reset();
00293                 state = Armed;
00294                 //Ethernet_massage_Send("UpdateStatus");
00295                 }
00296             if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone 
00297                 state = IR_sensed; 
00298                 Ethernet_massage_Send("UpdateStatus");
00299                 }
00300         //    Thread::wait(2000);
00301         }
00302         else {
00303             //nothing to do for this thread make space for others
00304       //      Thread::wait(2000);
00305         }
00306     //}
00307                 
00308                 
00309                 
00310                 
00311                 
00312                 
00313                 
00314                 
00315                 
00316                 
00317                 
00318                 
00319                 
00320                 break;
00321             case Cleared:
00322                 break;
00323             case IR_sensed:
00324                 if(codeCounter >= CODE_LENGTH){
00325                     pass = check_code_sequence(passcode);
00326                     if(pass == true){
00327                         //PC_Access.lock();
00328                         //pc.printf("SENDING AUTHENTICATION CODE...\n\r");
00329                         //PC_Access.unlock();
00330                         generate_random_code(authenticator);
00331                         t1.stop(); // reset the time
00332                         t1.reset(); // so that it is an even
00333                         t1.start(); // 30 seconds before 1st new code is generated
00334                         codeCounter = 0;
00335                         //code_enabled = true;
00336                         state = Second_Step;
00337                         Ethernet_massage_Send("UpdateStatus");
00338                         Ethernet_massage_Send("TextCode");
00339                     }
00340                     else{
00341                         //PC_Access.lock();
00342                         //pc.printf("WRONG passcode\n\r");
00343                         //PC_Access.unlock();
00344                         codeCounter = 0;
00345                     }
00346                 }
00347                 break;
00348             case Second_Step:
00349                 if(codeCounter >= CODE_LENGTH){
00350                     NumTry = 0;
00351                     pass = check_code_sequence(authenticator);
00352                     if(pass == true){
00353                         //PC_Access.lock();
00354                         //pc.printf("ACCESS GRANTED\n\r");
00355                         //PC_Access.unlock();
00356 
00357                         //wait(5);
00358                         //doorlock = 0;*/
00359                         //pc.printf("Resetting....\n\r");
00360                         //pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
00361                         codeCounter = 0;
00362                         //code_enabled = false;
00363                         state = Cleared;
00364                         Ethernet_massage_Send("TextCode");
00365                         Ethernet_massage_Send("UpdateStatus");
00366                         doorlock = 1;  
00367                         flipper.attach(&Activate_Lock, 5.0);
00368                     }
00369                     else{
00370                         //PC_Access.lock();
00371                         //pc.printf("ACCESS DENIED\n\r");
00372                         //PC_Access.unlock();
00373                         codeCounter = 0;
00374                     }
00375                 }
00376                 // this code generates a new authentication code every 30 seconds (30000 ms)
00377                 new_code_timer = (int)(t1.read_ms()/300000000);
00378                 if(new_code_timer > new_code_counter){
00379                     new_code_counter++;
00380                     generate_random_code(authenticator);
00381                     codeCounter = 0;
00382                 }
00383                 break;
00384         }
00385         // reset the timer when the number gets too high, should last about 35 minutes
00386         //  We do this because int can only hold a number up to 2^32 - 1, preventing errors
00387         if(t1.read_us() > 2147400000){
00388             t1.stop();
00389             t1.reset();
00390             new_code_timer = 0;
00391             new_code_counter = 0;
00392             t1.start();
00393         }
00394     }
00395 }
00396 
00397 
00398 void Shiftbright_thread(void const *args){
00399     spi.format(16,0);
00400     spi.frequency(500000);
00401     enable=0;
00402     latch=0;
00403     
00404     while(1) {
00405     switch (state) {
00406         case Armed:
00407             
00408             for (int i = 0; i <= 50; i++) {
00409                 RGB_LED( i, 0, 0);
00410                 Thread::wait(10);
00411             }
00412             for (int i = 50; i >= 0; i--) {
00413                 RGB_LED( i, 0, 0);
00414                 Thread::wait(10);
00415             }
00416         break;
00417         case IR_sensed:
00418             RGB_LED( 100, 0, 0);
00419             Thread::wait(500);
00420             RGB_LED( 0, 0, 0);
00421         break;
00422         case Alarm_ON:
00423             mySpeaker.PlaySong(note,duration);
00424         //Thread::wait(2000); 
00425             for (int i = 0; i <= 100; i++) {
00426                 RGB_LED( i, i/2, 0);
00427                 Thread::wait(10);
00428             }
00429             for (int i = 100; i >= 0; i--) {
00430                 RGB_LED( i, i/3, 0);
00431                 Thread::wait(10);
00432             }
00433         break; 
00434         case Cleared:
00435             RGB_LED( 0, 100, 0);
00436             break;
00437         }
00438         Thread::wait(1000);
00439    }
00440 }
00441 
00442 
00443 void IR_thread(void const *args) {
00444     
00445     Timer t;
00446     t.start(); 
00447     
00448     while(1) {
00449         
00450         if (state == Armed) {
00451             IrVoltage=IrSensor.read();
00452             if (IrVoltage <= 0.1) { //if value just nois reset timer
00453                 t.reset();
00454                 state = Armed;
00455                 Ethernet_massage_Send("UpdateStatus");
00456                 }
00457             if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone 
00458                 state = IR_sensed; 
00459                 Ethernet_massage_Send("UpdateStatus");
00460                 }
00461             Thread::wait(2000);
00462         }
00463         else {
00464             //nothing to do for this thread make space for others
00465             Thread::wait(2000);
00466         }
00467     }
00468 }
00469 
00470 void RGB_LED(int red, int green, int blue) {
00471     
00472     unsigned int low_color=0;
00473     unsigned int high_color=0;
00474     high_color=(blue<<4)|((red&0x3C0)>>6);
00475     low_color=(((red&0x3F)<<10)|(green));
00476     spi.write(high_color);
00477     spi.write(low_color);
00478     latch=1;
00479     latch=0;
00480 }
00481 
00482 void init_LCD() {
00483     uLCD.baudrate(3000000);
00484     uLCD.background_color(BLACK);
00485     
00486 }
00487     
00488 
00489 void uLCD_thread(void const *args) {
00490     int Change = 99; 
00491     string temp;
00492     temp = "abc";
00493     while(1) {
00494         
00495         if (Change != state) {
00496             Change = state;
00497             switch (state) {
00498                 case Armed:
00499                     LCD_Access.lock();
00500                     uLCD.cls();
00501                     uLCD.color(WHITE);
00502                     uLCD.text_width(2);
00503                     uLCD.text_height(2);
00504                     uLCD.printf("  ARMED\r\n");
00505                     uLCD.text_width(1); 
00506                     uLCD.text_height(1);
00507                    
00508                     if (eth.getIPAddress() == "\0") {
00509                         uLCD.printf("\n\n No Internet connection");
00510                         }
00511                     else {
00512                         uLCD.printf("\n\nConnected to the  Internet\n");
00513                         uLCD.printf("IP Address: \n%s ", eth.getIPAddress());
00514                         }
00515                     LCD_Access.unlock();
00516                 break;
00517                 case IR_sensed:
00518                     LCD_Access.lock();
00519                     uLCD.cls();
00520                     uLCD.printf("\nSensor triggred \n"); 
00521                     uLCD.printf("\n  Enter the code ...");
00522                     LCD_Access.unlock();
00523                     for (int i=30; i>=0; --i) {
00524                         if (state == IR_sensed) {
00525                             LCD_Access.lock();
00526                             uLCD.text_width(4);
00527                             uLCD.text_height(4);
00528                             LCD_Access.unlock();
00529                             LCD_Access.lock();
00530                             uLCD.color(RED);
00531                             uLCD.locate(1,2);
00532                             uLCD.printf("%2D",i);                    
00533                             LCD_Access.unlock();                    
00534                             Thread::wait(1000);
00535                             }
00536                         }
00537                         if (state == IR_sensed) {
00538                             LCD_Access.lock();
00539                             uLCD.cls();
00540                             LCD_Access.unlock();
00541                             state = Alarm_ON;
00542                             Ethernet_massage_Send("UpdateStatus");
00543                             Ethernet_massage_Send("TextAlarm");
00544                             }
00545 
00546                 break;
00547                 case Second_Step: 
00548                     LCD_Access.lock();
00549                     uLCD.cls();
00550                     uLCD.color(BLUE);
00551                     uLCD.printf("\nPleas enter code from text massage \n");
00552                     LCD_Access.unlock();
00553                     break;
00554                 case Alarm_ON:
00555                     LCD_Access.lock();
00556                     uLCD.cls();
00557                     uLCD.color(RED);
00558                     uLCD.text_width(1.5); //4X size text
00559                     uLCD.text_height(1.5);
00560                     uLCD.printf("\nALARM IS ON \nText message sent \n"); 
00561                     LCD_Access.unlock();
00562                     break;
00563                 case Cleared: 
00564                     LCD_Access.lock();
00565                     uLCD.cls();
00566                     uLCD.color(GREEN);
00567                     uLCD.printf("\n\nAccess Granted. \n\n"); 
00568                     LCD_Access.unlock();  
00569                 break; 
00570                 }
00571             }
00572             Thread::wait(500);
00573         }
00574 }
00575 
00576 void LCD_Code_Enter_Thread(void const *args) {
00577     int LineHight = 120;
00578     int LineWidth = 10; 
00579     int SpaceWidth = 5;
00580     int MidPoint = 127/2;
00581     while(1) {
00582         switch (state) {
00583                 case Armed:
00584                 break;
00585                 case IR_sensed:
00586                 case Cleared:
00587                 case Second_Step:
00588                   
00589                     Thread::wait(500);
00590                     while((state == IR_sensed)||(state == Cleared)||(state == Second_Step)) {
00591                         LCD_Access.lock();
00592                         //dusplay four lines 
00593                         uLCD.line(MidPoint-2*(LineWidth+SpaceWidth), LineHight, MidPoint- 2*SpaceWidth-LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
00594                         uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
00595                         uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
00596                         uLCD.line(MidPoint+2*SpaceWidth+LineWidth, LineHight, MidPoint+2*(SpaceWidth+LineWidth), LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
00597                         uLCD.locate(5,14);
00598                         uLCD.text_width(1); //4X size text
00599                         uLCD.text_height(1);
00600                         
00601                         // add black numbers
00602                         /*if (inputCode[0] == inputCode[2] ==inputCode[4] ==inputCode[6] == 0) {                        
00603                             uLCD.color(BLACK);                        
00604                         }
00605                         else {*/
00606                         //uLCD.color(WHITE);
00607                         uLCD.printf("%d %d  %d %d",inputCode[0],inputCode[2],inputCode[4],inputCode[6]);
00608                         //}
00609                         LCD_Access.unlock();
00610                     }
00611                 
00612                 case Alarm_ON:
00613                 break;
00614                 }
00615         }
00616 }
00617 
00618 void Speaker_thread(void const *args) {
00619     while (1) {
00620     if (state == Alarm_ON) {
00621         mySpeaker.PlaySong(note,duration);
00622         Thread::wait(2000); 
00623         }
00624     }
00625 }
00626 
00627 void Ethernet_thread(void const *args) { 
00628  
00629     char buffer[300];
00630    
00631     int ret,found;
00632     eth.init(); //Use DHCP
00633     eth.connect();
00634 //pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
00635     while (1) {
00636                         Thread::wait(3000);
00637         int timeout;
00638 
00639         //t2.start();                 
00640         TCPSocketConnection sock;
00641         sock.connect("dreamphysix.com", 80);
00642         char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n";
00643     //PC_Access.lock();
00644     //pc.printf("%s",http_cmd);
00645     //PC_Access.unlock();
00646     Eth_Lock.lock();
00647     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00648     Eth_Lock.unlock();
00649                         while (true) {
00650                         Eth_Lock.lock();
00651                         ret = sock.receive(buffer, sizeof(buffer)-1);
00652                         Eth_Lock.unlock();
00653                         if (ret <= 0)
00654                             break;
00655                         buffer[ret] = '\0';
00656                         Consul_Access.wait();
00657                         //PC_Access.lock();
00658                         //pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00659                         //PC_Access.unlock();
00660                         Consul_Access.release();
00661                         }
00662                     sock.close();
00663                     //t2.stop();
00664     string str(buffer);
00665     found = str.find("Status=");
00666     //pc.printf("location: %d string: %s" , found, str);              
00667     //pc.printf("\n\rhttp_cmd: %c\n\r",buffer[found+7]);
00668     //pc.printf("\n\state: %i\n\r",state);
00669     int dummy = (buffer[found+7])-48;
00670     //int dummy = ((int)buffer[found+7]);
00671     //dummy = atoi(dummy);
00672     state = (Statetype)dummy;
00673     //state = (Statetype)buffer[found+7];
00674     PC_Access.lock();
00675     pc.printf("\n\state: %i\n\r",dummy);
00676     PC_Access.unlock();
00677     //Thread::wait(3000); 
00678     
00679 
00680     }
00681 }
00682 
00683 bool Ethernet_massage_Send(string buff) { 
00684 
00685     char buffer[300];
00686     int ret;
00687     //pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
00688 
00689                     TCPSocketConnection sock;
00690                     sock.connect("dreamphysix.com", 80);
00691                     
00692                     if (buff == "TextCode") { 
00693                             char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode=";
00694                             strcat(http_cmd, charCode);
00695                             strcat(http_cmd, " HTTP/1.0\n\n");
00696                             PC_Access.lock();
00697                             pc.printf("%s",http_cmd);
00698                             PC_Access.unlock();
00699                             Eth_Lock.lock();
00700                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
00701                             Eth_Lock.unlock();
00702                     }
00703                     else if (buff == "TextAlarm") {
00704                             char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
00705                             Eth_Lock.lock();
00706                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
00707                             Eth_Lock.unlock();
00708                             PC_Access.lock();
00709                             pc.printf("%s",http_cmd);
00710                             PC_Access.unlock();
00711                    }
00712                     else if (buff == "GetTempCode") {
00713                             snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]);
00714                             char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code=";
00715                             strcat(http_cmd, charCode);
00716                             strcat(http_cmd, " HTTP/1.0\n\n");
00717                             PC_Access.lock();
00718                             pc.printf("%s",http_cmd);
00719                             PC_Access.unlock();
00720                             Eth_Lock.lock();
00721                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
00722                             Eth_Lock.unlock();
00723                     }
00724                     else if (buff == "UpdateStatus") {
00725                             char tempStatus[2];
00726                             snprintf(tempStatus, sizeof(tempStatus), "%i", state);
00727                             char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=1";
00728                             //strcat(http_cmd, ID);
00729                             strcat(http_cmd, "&status=");
00730                             strcat(http_cmd, tempStatus);
00731                             strcat(http_cmd, " HTTP/1.0\n\n");
00732                             PC_Access.lock();
00733                             pc.printf("%s",http_cmd);
00734                             PC_Access.unlock();
00735                             Eth_Lock.lock();
00736                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
00737                             Eth_Lock.unlock();
00738                     }
00739                     else {
00740                         
00741                     }
00742 
00743 
00744                     while (true) {
00745                         Eth_Lock.lock();
00746                         ret = sock.receive(buffer, sizeof(buffer)-1);
00747                         Eth_Lock.unlock();
00748                         if (ret <= 0)
00749                             break;
00750                         buffer[ret] = '\0';
00751                         Consul_Access.wait();
00752                         //PC_Access.lock();
00753                        //pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00754                         //PC_Access.unlock();
00755                         Consul_Access.release();
00756                         }
00757                     sock.close();
00758                     
00759                     snprintf(buffer, ret, "%c",buffer);
00760                     //pc.printf("buffer: %s", buffer);
00761                     if (strstr(buffer,"True") != NULL) {
00762                          //pc.printf("true fron eth check");
00763                          return true;
00764                     }
00765                     else if (strstr(buffer,"False") != NULL) {
00766                          //pc.printf("false fron eth check");
00767                          return false;
00768                     }
00769                     else {
00770                         //pc.printf("default ");
00771                         return true;
00772                     }
00773     Thread::wait(1000); 
00774 }
00775 
00776 void Activate_Lock(){
00777     doorlock =! doorlock;
00778 }