
IoT for mbed1
Dependencies: 4DGL-uLCD-SE IoTsecuritySys PinDetect mbed-rtos mbed
Fork of IoT by
Diff: main.cpp
- Revision:
- 5:ebc70efa2e86
- Parent:
- 4:d8de964b3d2c
- Child:
- 6:33185f926189
--- a/main.cpp Sun Dec 06 23:15:34 2015 +0000 +++ b/main.cpp Mon Dec 07 19:43:53 2015 +0000 @@ -1,5 +1,6 @@ #include <mbed.h> #include <string> +#include <iostream> #include "rtos.h" #include <mpr121.h> #include <stdlib.h> @@ -64,6 +65,7 @@ // ***** GLOBALS ***** // // Timer is to seed rand string ID = "1"; +volatile int NumTry = 0; Timer t1; // code counter is the next position in inputCode array volatile int codeCounter; @@ -71,6 +73,7 @@ volatile int inputCode[CODE_LENGTH]; 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}; Statetype state = Armed; char charCode[5]; @@ -79,6 +82,7 @@ float duration[18]= {0.48,0.24}; Mutex LCD_Access; +Mutex PC_Access; Semaphore Consul_Access(5); void Shiftbright_thread(void const *args); @@ -130,9 +134,14 @@ case Alarm_ON: break; case Cleared: - if(codeCounter % 2 == 11) + 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); + } break; } @@ -142,36 +151,65 @@ void generate_random_code(int (&codeArray)[CODE_LENGTH]){ //int i = 0; // only care about the even numbers (see lines 6-10) - pc.printf("NEW CODE: "); + //////////PC_Access.lock(); + //pc.printf("NEW CODE: "); + //PC_Access.unlock(); for(int i = 0; i < CODE_LENGTH; i+=2){ srand(t1.read_us()); codeArray[i] = (rand() % 9)+1; //nake code only 1-9 + //////////PC_Access.lock(); pc.printf("%d, ", codeArray[i]); + //PC_Access.unlock(); } - pc.printf("\n\r"); + snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]); - pc.printf("%s\n\r",charCode,codeArray[6]); - Ethernet_massage_Send("TextCode"); + //////////PC_Access.lock(); + //pc.printf("\n\r"); + //pc.printf("%s\n\r",charCode,codeArray[6]); + //PC_Access.unlock(); + //Ethernet_massage_Send("TextCode"); } // check if the code entered is the correct code bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){ - int i = 0; + //int i = 0; int j = 0; // only care about the even numbers (see lines 6-10) - for(i = 0; i < CODE_LENGTH; i+=2){ - if(inputCode[i] == codeArray[i]) + if (NumTry < 3) { + NumTry++; + for(int i = 0; i < CODE_LENGTH; i+=2){ + + if(inputCode[i] == codeArray[i]){ j++; // count the number of right numbers + } + } - if(j == CODE_LENGTH/2) + if(j == CODE_LENGTH/2){ + for(int i = 0; i < CODE_LENGTH; i+=2){ + inputCode[i] =0; + } return(true); + } else if (Ethernet_massage_Send("GetTempCode")){ + for(int i = 0; i < CODE_LENGTH; i+=2){ + inputCode[i] =0; + } + //pc.printf("return true"); return(true); } else { + for(int i = 0; i < CODE_LENGTH; i+=2){ + inputCode[i] =0; + } return(false); } + } + else { + pc.printf("3 times "); + state = Alarm_ON; + return(false); + } } int main() { @@ -204,17 +242,22 @@ t1.start(); init_LCD(); //start threads: + //////////PC_Access.lock(); pc.printf("\n\n\nSetting up Ethernet\n\r"); + //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); + //Thread Speakerthread(Speaker_thread); + // while loop constantly checks if the entered code sequence is right or wrong, // given that the correct amount of numbers were entered - pc.printf("Please Enter Your Personal Security Code\n\r"); + ////////////PC_Access.lock(); + pc.printf("Ready\n\r"); + ////PC_Access.unlock(); doorlock = 0; // make sure locked while (1){ switch(state){ @@ -224,7 +267,9 @@ if(codeCounter >= CODE_LENGTH){ pass = check_code_sequence(passcode); if(pass == true){ - pc.printf("SENDING AUTHENTICATION CODE...\n\r"); + ////////////////////////PC_Access.lock(); + //pc.printf("SENDING AUTHENTICATION CODE...\n\r"); + ////////PC_Access.unlock(); generate_random_code(authenticator); t1.stop(); // reset the time t1.reset(); // so that it is an even @@ -235,29 +280,37 @@ //Ethernet_massage_Send("UpdateStatus"); } else{ - pc.printf("WRONG passcode\n\r"); + ////////////////////////PC_Access.lock(); + //pc.printf("WRONG passcode\n\r"); + ////PC_Access.unlock(); codeCounter = 0; } } break; case Second_Step: if(codeCounter >= CODE_LENGTH){ + NumTry = 0; pass = check_code_sequence(authenticator); if(pass == true){ - pc.printf("ACCESS GRANTED\n\r"); + ////////////////////////PC_Access.lock(); + //pc.printf("ACCESS GRANTED\n\r"); + ////PC_Access.unlock(); doorlock = 1; flipper.attach(&Activate_Lock, 5.0); //wait(5); //doorlock = 0;*/ - pc.printf("Resetting....\n\r"); - pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r"); + //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("UpdateStatus"); + Ethernet_massage_Send("TextCode"); + //Ethernet_massage_Send("UpdateStatus"); } else{ - pc.printf("ACCESS DENIED\n\r"); + ////////////////////////PC_Access.lock(); + //pc.printf("ACCESS DENIED\n\r"); + ////PC_Access.unlock(); codeCounter = 0; } } @@ -477,9 +530,15 @@ uLCD.locate(5,14); uLCD.text_width(1); //4X size text uLCD.text_height(1); - uLCD.color(WHITE); + // add black numbers + /*if (inputCode[0] == inputCode[2] ==inputCode[4] ==inputCode[6] == 0) { + uLCD.color(BLACK); + } + else {*/ + //uLCD.color(WHITE); uLCD.printf("%d %d %d %d",inputCode[0],inputCode[2],inputCode[4],inputCode[6]); + //} LCD_Access.unlock(); } @@ -501,15 +560,19 @@ void Ethernet_thread(void const *args) { char buffer[300]; - int ret; + + int ret,found; eth.init(); //Use DHCP eth.connect(); -pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); - /* while (1) { +//pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); + while (1) { + Thread::wait(10000); TCPSocketConnection sock; sock.connect("dreamphysix.com", 80); char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n"; - pc.printf("%s",http_cmd); + ////////////PC_Access.lock(); + //pc.printf("%s",http_cmd); + ////PC_Access.unlock(); sock.send_all(http_cmd, sizeof(http_cmd)-1); while (true) { @@ -518,24 +581,33 @@ break; buffer[ret] = '\0'; Consul_Access.wait(); - pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + ////////////////////////PC_Access.lock(); + //pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + ////PC_Access.unlock(); Consul_Access.release(); } sock.close(); - find(buffer, size_t pos = 0); - //size_t found = buffer.find("Status="); - pc.printf("location: %i string: %s",found, buffer[found]); - - Thread::wait(2000); - }*/ - + + //string str(buffer); + //found = str.find("Status="); + //pc.printf("location: %d string: %s" , found, str); + //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.printf("\n\state: %i\n\r",dummy); + //Thread::wait(3000); + } } bool Ethernet_massage_Send(string buff) { char buffer[300]; int ret; - pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); + //pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); TCPSocketConnection sock; sock.connect("dreamphysix.com", 80); @@ -544,7 +616,9 @@ char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode="; strcat(http_cmd, charCode); strcat(http_cmd, " HTTP/1.0\n\n"); + //////////////////////////PC_Access.lock(); pc.printf("%s",http_cmd); + ////PC_Access.unlock(); sock.send_all(http_cmd, sizeof(http_cmd)-1); } else if (buff == "TextAlarm") { @@ -556,7 +630,9 @@ char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code="; strcat(http_cmd, charCode); strcat(http_cmd, " HTTP/1.0\n\n"); + //////////////////////////PC_Access.lock(); pc.printf("%s",http_cmd); + ////PC_Access.unlock(); sock.send_all(http_cmd, sizeof(http_cmd)-1); } else if (buff == "UpdateStatus") { @@ -567,7 +643,9 @@ strcat(http_cmd, "&status="); strcat(http_cmd, tempStatus); strcat(http_cmd, " HTTP/1.0\n\n"); + //////////////////////////PC_Access.lock(); pc.printf("%s",http_cmd); + ////PC_Access.unlock(); sock.send_all(http_cmd, sizeof(http_cmd)-1); } else { @@ -581,21 +659,26 @@ break; buffer[ret] = '\0'; Consul_Access.wait(); + ////////////////////////PC_Access.lock(); pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + ////PC_Access.unlock(); Consul_Access.release(); } sock.close(); - snprintf(buffer, sizeof(buffer), "%i",ret); - //pc.printf("buffer: %s" + snprintf(buffer, ret, "%c",buffer); + //pc.printf("buffer: %s", buffer); if (strstr(buffer,"True") != NULL) { + //pc.printf("true fron eth check"); + return true; + } + else if (strstr(buffer,"False") != NULL) { + //pc.printf("false fron eth check"); return false; } - else if (strstr(buffer,"False") != NULL) { - return false; - } - else if (strstr(buffer,"Invalid") != NULL) { - return false; + else { + //pc.printf("default "); + return false; } }