IoT for mbed1

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

Fork of IoT by Tal Landes

Revision:
4:d8de964b3d2c
Parent:
2:922d5b43bee3
Child:
5:ebc70efa2e86
--- a/main.cpp	Sun Dec 06 20:24:13 2015 +0000
+++ b/main.cpp	Sun Dec 06 23:15:34 2015 +0000
@@ -59,10 +59,11 @@
 EthernetInterface eth;
 
 //Lock timeout 
-Ticker flipper;
+Timeout flipper;
 
 // ***** GLOBALS ***** //
 // Timer is to seed rand
+string ID = "1";
 Timer t1;
 // code counter is the next position in inputCode array
 volatile int codeCounter;
@@ -70,7 +71,7 @@
 volatile int inputCode[CODE_LENGTH];
 volatile bool code_enabled;
 volatile float IrVoltage = 0.0;
-volatile enum Statetype { Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4};
+volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
 Statetype state = Armed;
 char charCode[5];
 
@@ -131,6 +132,7 @@
         case Cleared:
             if(codeCounter % 2 == 11)
                 state = Armed;
+                //Ethernet_massage_Send("UpdateStatus");
         break;
         }
     
@@ -157,6 +159,7 @@
     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])
         j++; // count the number of right numbers
@@ -229,6 +232,7 @@
                         codeCounter = 0;
                         //code_enabled = true;
                         state = Second_Step;
+                        //Ethernet_massage_Send("UpdateStatus");
                     }
                     else{
                         pc.printf("WRONG passcode\n\r");
@@ -250,6 +254,7 @@
                         codeCounter = 0;
                         code_enabled = false;
                         state = Cleared;
+                        Ethernet_massage_Send("UpdateStatus");
                     }
                     else{
                         pc.printf("ACCESS DENIED\n\r");
@@ -332,9 +337,11 @@
             if (IrVoltage <= 0.1) { //if value just nois reset timer
                 t.reset();
                 state = Armed;
+                //Ethernet_massage_Send("UpdateStatus");
                 }
             if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone 
                 state = IR_sensed; 
+                //Ethernet_massage_Send("UpdateStatus");
                 }
             Thread::wait(1000);
         }
@@ -412,6 +419,7 @@
                         }
                         if (state == IR_sensed) {
                             state = Alarm_ON;
+                            //Ethernet_massage_Send("UpdateStatus");
                             Ethernet_massage_Send("TextAlarm");
                             }
 
@@ -491,38 +499,20 @@
 }
 
 void Ethernet_thread(void const *args) { 
-    
-    int Change = 99; 
+ 
     char buffer[300];
     int ret;
     eth.init(); //Use DHCP
     eth.connect();
-    pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
+pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
+   /* while (1) {
+                        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);
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
     
-    // add check for connection
-    /*while(1) {
-        if (Change != state) {
-             
-            Change = state;
-            switch (state) {
-                case Armed:
-                    //add code that verifies connection every so often 
-                    Thread::wait(1000);
-                break;
-                case Second_Step: 
-                    Thread::wait(1000);
-                break;
-                case Alarm_ON: {
-                // send alert
-
-                    TCPSocketConnection sock;
-                    sock.connect("dreamphysix.com", 80);
-
-                    char http_cmd[] = "GET http://dreamphysix.com/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
-                    sock.send_all(http_cmd, sizeof(http_cmd)-1);
-    
-
-                    while (true) {
+                        while (true) {
                         ret = sock.receive(buffer, sizeof(buffer)-1);
                         if (ret <= 0)
                             break;
@@ -532,35 +522,20 @@
                         Consul_Access.release();
                         }
                     sock.close();
-                    }
-                break;
-                case Cleared:
-                    //nothing to do for this thread make space for others 
-                    Thread::wait(1000);
-                break;
-                case IR_sensed:
-                    //nothing to do for this thread make space for others
-                    Thread::wait(1000);
-                break;
-            }
-        }
-    } */  
+        find(buffer, size_t pos = 0);
+     //size_t found = buffer.find("Status=");
+     pc.printf("location: %i string: %s",found, buffer[found]);              
+    
+    Thread::wait(2000); 
+    }*/
+
 }
 
 bool Ethernet_massage_Send(string buff) { 
-    
-    
-    
-    
-    int Change = 99; 
+
     char buffer[300];
     int ret;
-    //eth.init(); //Use DHCP
-    //eth.connect();
     pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
-    pc.printf("buff: %s\n\r", buff);
-    
-                    // send alert
 
                     TCPSocketConnection sock;
                     sock.connect("dreamphysix.com", 80);
@@ -577,6 +552,7 @@
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
                    }
                     else if (buff == "GetTempCode") {
+                            snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]);
                             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");
@@ -584,7 +560,15 @@
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
                     }
                     else if (buff == "UpdateStatus") {
-                            //char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
+                            char tempStatus[2];
+                            snprintf(tempStatus, sizeof(tempStatus), "%i", state);
+                            char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=1";
+                            //strcat(http_cmd, ID);
+                            strcat(http_cmd, "&status=");
+                            strcat(http_cmd, tempStatus);
+                            strcat(http_cmd, " HTTP/1.0\n\n");
+                            pc.printf("%s",http_cmd);
+                            sock.send_all(http_cmd, sizeof(http_cmd)-1);
                     }
                     else {
                         
@@ -601,55 +585,20 @@
                         Consul_Access.release();
                         }
                     sock.close();
-    /*while(1) {
-        if (Change != state) {
-             
-            Change = state;
-            switch (state) {
-                case Armed:
-                    //add code that verifies connection every so often 
-                    Thread::wait(1000);
-                break;
-                case Second_Step: 
-                    Thread::wait(1000);
-                break;
-                case Alarm_ON: {
-                // send alert
-
-                    TCPSocketConnection sock;
-                    sock.connect("dreamphysix.com", 80);
-
-                    char http_cmd[] = "GET http://dreamphysix.com/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
-                    sock.send_all(http_cmd, sizeof(http_cmd)-1);
-    
-
-                    while (true) {
-                        ret = sock.receive(buffer, sizeof(buffer)-1);
-                        if (ret <= 0)
-                            break;
-                        buffer[ret] = '\0';
-                        Consul_Access.wait();
-                       pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
-                        Consul_Access.release();
-                        }
-                    sock.close();
+                    
+                    snprintf(buffer, sizeof(buffer), "%i",ret);
+                    //pc.printf("buffer: %s"
+                    if (strstr(buffer,"True") != NULL) {
+                         return false;
                     }
-                break;
-                case Cleared:
-                    //nothing to do for this thread make space for others 
-                    Thread::wait(1000);
-                break;
-                case IR_sensed:
-                    //nothing to do for this thread make space for others
-                    Thread::wait(1000);
-                break;
-            }
-        }
-    }  */ 
-    return false;
+                    else if (strstr(buffer,"False") != NULL) {
+                         return false;
+                    }
+                    else if (strstr(buffer,"Invalid") != NULL) {
+                         return false;
+                    }
 }
 
 void Activate_Lock(){
-    pc.printf("lock\n\r");
     doorlock =! doorlock;
 }