IoT for mbed1

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

Fork of IoT by Tal Landes

Revision:
2:922d5b43bee3
Parent:
1:7f873efe5b11
Child:
4:d8de964b3d2c
diff -r 7f873efe5b11 -r 922d5b43bee3 main.cpp
--- a/main.cpp	Sat Dec 05 15:23:55 2015 +0000
+++ b/main.cpp	Sun Dec 06 20:18:37 2015 +0000
@@ -1,4 +1,5 @@
 #include <mbed.h>
+#include <string>
 #include "rtos.h"
 #include <mpr121.h>
 #include <stdlib.h>
@@ -71,6 +72,7 @@
 volatile float IrVoltage = 0.0;
 volatile enum Statetype { Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4};
 Statetype state = Armed;
+char charCode[5];
 
 float note[18]= {1568.0,1396.9};
 float duration[18]= {0.48,0.24};
@@ -85,6 +87,7 @@
 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();
 void init_LCD();
 // Key hit/release interrupt routine
@@ -105,18 +108,32 @@
     led1=(key_code>>3) & 0x01;
   
     // save the keypress to inputCode array
-    if(codeCounter < CODE_LENGTH){
-        // ignore odd numbers
-        if(codeCounter % 2 != 0){
-            inputCode[codeCounter] = 0;
+    switch (state) {
+        case Armed:
+        break;
+        case IR_sensed:
+        case Second_Step:
+            if(codeCounter < CODE_LENGTH){
+            // ignore odd numbers
+            if(codeCounter % 2 != 0){
+                inputCode[codeCounter] = 0;
+            }
+            // only save the even numbers (see lines 6-10)
+            else{
+                inputCode[codeCounter] = key_code - 1;
+                //pc.printf("codeCounter: %d  --  code:  %d\n\r", codeCounter, key_code - 1);
+            }      
+            codeCounter++;
         }
-        // only save the even numbers (see lines 6-10)
-        else{
-            inputCode[codeCounter] = key_code - 1;
-            //pc.printf("codeCounter: %d  --  code:  %d\n\r", codeCounter, key_code - 1);
-        }      
-        codeCounter++;
-    }
+        break;
+        case Alarm_ON:
+        break; 
+        case Cleared:
+            if(codeCounter % 2 == 11)
+                state = Armed;
+        break;
+        }
+    
 }
 
 // generate randomized code
@@ -130,6 +147,9 @@
         pc.printf("%d,  ", codeArray[i]);
     }
     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");
 }
 
 // check if the code entered is the correct code
@@ -143,8 +163,12 @@
     }
     if(j == CODE_LENGTH/2)
         return(true);
-    else
-        return(false);
+    else if  (Ethernet_massage_Send("GetTempCode")){
+        return(true);
+    }
+    else {
+       return(false);
+    }
 }
  
 int main() {
@@ -178,8 +202,8 @@
     init_LCD();
     //start threads:
     pc.printf("\n\n\nSetting up Ethernet\n\r");
-    //Thread Ethernetthread(Ethernet_thread);
-    //wait(5); //Give the Ethernet connection some time to set up 
+    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);
@@ -233,7 +257,7 @@
                     }
                 }
                 // this code generates a new authentication code every 30 seconds (30000 ms)
-                new_code_timer = (int)(t1.read_ms()/30000);
+                new_code_timer = (int)(t1.read_ms()/300000000);
                 if(new_code_timer > new_code_counter){
                     new_code_counter++;
                     generate_random_code(authenticator);
@@ -342,6 +366,8 @@
 
 void uLCD_thread(void const *args) {
     int Change = 99; 
+    string temp;
+    temp = "abc";
     while(1) {
         
         if (Change != state) {
@@ -386,6 +412,7 @@
                         }
                         if (state == IR_sensed) {
                             state = Alarm_ON;
+                            Ethernet_massage_Send("TextAlarm");
                             }
 
                 break;
@@ -466,12 +493,14 @@
 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());
     
-    while(1) {
+    // add check for connection
+    /*while(1) {
         if (Change != state) {
              
             Change = state;
@@ -485,12 +514,11 @@
                 break;
                 case Alarm_ON: {
                 // send alert
-                    char buffer[300];
-                    int ret;
+
                     TCPSocketConnection sock;
                     sock.connect("dreamphysix.com", 80);
 
-                    char http_cmd[] = "GET http://dreamphysix.com/alert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
+                    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);
     
 
@@ -516,10 +544,112 @@
                 break;
             }
         }
-    }   
+    } */  
+}
+
+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);
+                    
+                    if (buff == "TextCode") { 
+                            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.printf("%s",http_cmd);
+                            sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                    }
+                    else if (buff == "TextAlarm") {
+                            char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
+                            sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                   }
+                    else if (buff == "GetTempCode") {
+                            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.printf("%s",http_cmd);
+                            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";
+                    }
+                    else {
+                        
+                    }
+
+
+                    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();
+    /*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();
+                    }
+                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;
 }
 
 void Activate_Lock(){
-    
+    pc.printf("lock\n\r");
     doorlock =! doorlock;
 }