The code reads various sensors, populates the packet and transmits it over to another TCP Client on receiving a request

Dependencies:   Ping WiflyInterface mbed

Fork of Wifly_TCPEchoServer by Samuel Mokrani

Files at this revision

API Documentation at this revision

Comitter:
Neel
Date:
Sun Apr 21 22:32:45 2013 +0000
Parent:
2:b15847b47f78
Commit message:
Final code on 04/21

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r b15847b47f78 -r 13b54c5c407c main.cpp
--- a/main.cpp	Fri Apr 05 01:47:23 2013 +0000
+++ b/main.cpp	Sun Apr 21 22:32:45 2013 +0000
@@ -51,7 +51,8 @@
 
 int main (void)
 {
-    wifly.init(); // use DHCP
+    wifly.init("192.168.1.101","255.255.255.0","192.168.1.1"); // use Static
+    //wifly.init();
     while (!wifly.connect()); // join the network
     printf("IP Address is %s\n\r", wifly.getIPAddress());
     
@@ -59,8 +60,6 @@
     server.set_blocking(false, 1500);
     server.bind(ECHO_SERVER_PORT);
     server.listen();
-    
-     
         
     printf("\nWait for new connection...\n");
     TCPSocketConnection client;
@@ -70,66 +69,90 @@
     
     char buffer[256];
     char b='0';
+    char dim = '0';
     char data[256];
     float temp_c;
     char fan = '0';
     char camera = '0';
-    int count=0;
-    float range;
+    long count=0;
+    float range = 0;
     int packet_no=1;
     char motion = '0';
     int n = 9;
     
-    while (true) {
-            
-            
-            if(!PIR){
-                led3 = 1;
-                motion = '1';
-            }
-            else 
+    float personCounter = 0;
+    bool personFlag = false;
+        
+    float tempAvg = 0;
+        
+    int lightIter=0;
+    
+    while (true) 
+    { 
+
+            if(lightIter == 0)
             {
                 led3=0;
                 motion = '0';
             }
+            lightIter++;
+            
+            if(lightIter == 10)
+                lightIter = 0;
+                
+            if(!PIR)
+            {
+                led3 = 1;
+                motion = '1';
+                lightIter = 1;
+            }
+          
             if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
-            temp_c=(t.read()*330.0)-50;
+            
+            temp_c=((t.read()*330.0)-50)+7;
+            
+            tempAvg = temp_c +tempAvg;
+            
             printf("\rtemp is%d\n",(int)temp_c);
             if(!PIR){
                 led3 = 1;
                 motion = '1';
+                lightIter = 1;
             }
-            else 
-            {
-                led3=0;
-                motion = '0';
-            }
+            
             if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
+            
             float light = l.read_u16();
+            
             printf("\rlight value is %f\n",light);
             if(!PIR){
                 led3 = 1;
                 motion = '1';
+                lightIter = 1;
             }
-            else 
-            {
-                led3=0;
-                motion = '0';
-            }
+           
             if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
             Pinger.Send();    
             wait_ms(30);
             range = Pinger.Read_cm();
             
+            if(range < 60.0 && range >= 0.0 && personFlag == false)
+            {
+                personFlag = true;
+                personCounter = personCounter + 1.0;
+            }
+            if(range >= 60.0 && personFlag == true)
+            {
+                personFlag = false;
+            }
+            printf("\rPerson counter is %f\n",range);
+            
             if(!PIR){
                 led3 = 1;
                 motion = '1';
+                lightIter = 1;
             }
-            else 
-            {
-                led3=0;
-                motion = '0';
-            }
+            
             if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
             if (temp_c >25 || (b==49))
             {   
@@ -138,59 +161,66 @@
                 servo.pulsewidth(0.002);
                 wait(1);
             }
-            else
+            if (temp_c <=25 && (b==48))
             {
                fan = '0';
-                    servo.pulsewidth(0);
-                    wait(1);
-              
+               servo.pulsewidth(0);
+               wait(1);
             }
             if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
-            if(light<6000)
+            
+           // 
+            
+            if(((dim-48) == 0 || (dim-48) == 1) && motion == '1')
             {
-                dimmer = 0.02;
+                dimmer = 0.07;
             }
-            else dimmer = 0;
-           if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
+            else
+            {
+                dimmer = 0.01286 * (dim-48) - .015714;
+            }
+            
+            if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
+            
             n = client.receive(buffer, sizeof(buffer));
             printf("\r N = %d\n",n);
            
-            /*if (n <= 0) count++;
+            count++;
+            
             
-            if (count>10) { mbed_reset(); count=0;} 
-            */
+            
+            //if (count>10) { mbed_reset(); count=0;} 
+            
             if (n>1)
             {
-                //for (int i=5;i<n;i++)    
-                printf("5%c 6%c 7%c 8%c 9%c 10%c",buffer[5],buffer[6],buffer[7],buffer[8], buffer[9], buffer[10]);
-                if (buffer[7] == 49 || buffer[7]==48) b=buffer[7]; 
+                printf("Received bytes %d",n);
+                for (int i=5;i<n;i++)    
+                //printf("5%c 6%c 7%c 8%c 9%c 10%c",buffer[5],buffer[6],buffer[7],buffer[8], buffer[9], buffer[10]);
+                printf("%d, %c\t",i,buffer[i]);
+                printf("\n");
+                if (buffer[7] == 49 || buffer[7]==48) 
+                    b=buffer[7];
+                    
+                if(buffer[18]>=48 && buffer[18]<=57) 
+                    dim = buffer[18];
+                 
                 data[3]=(int)motion;
-                data[5]=MY_NODE_ID;
+                data[5]=personCounter;
                 data[4]=packet_no;
                 data[0]=(int)temp_c;
                 data[1]=(int)(light/100);
                 data[2]=range;
                 data[6]=(int)fan;
                 data[7]=(int)camera;
-                data[8]='\0';
+                data[8] =(int)(tempAvg/count);
+                data[9]='\0';
             
                 n=sprintf(buffer,"%s",data);
                 printf("\n\n");
                 client.send_all(buffer, n);
                 packet_no++;
                 
-                /*if((buffer[6]-48)==1)
-                {
-                    servo.pulsewidth(0.02);
-                }else servo.pulsewidth(0);
-                */
-                /*if ((buffer[8]-48)>0)
-                {
-                led2=1;
-                dimmer = ((buffer[8]-48)/100);
-                }*/
                 
-                //n = client.receive(buffer, sizeof(buffer));
                 for (int i=5; i<n; i++)
                 {
                     buffer[i]='0';