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

Revision:
1:9ae2041887fa
Parent:
0:0710a5e21ef9
Child:
2:b15847b47f78
--- a/main.cpp	Thu Dec 20 15:31:13 2012 +0000
+++ b/main.cpp	Wed Mar 27 18:16:27 2013 +0000
@@ -1,7 +1,14 @@
 #include "mbed.h"
 #include "WiflyInterface.h"
+#include "Ping.h"
 
-#define ECHO_SERVER_PORT   7
+#define ECHO_SERVER_PORT   12345
+#define MY_NODE_ID 1
+
+Ping Pinger(p21);
+AnalogIn t(p19);        //TMP36 is connected here
+AnalogIn l(p18);
+DigitalIn PIR(p17);
 
 /* wifly object where:
 *     - p9 and p10 are for the serial communication
@@ -11,14 +18,14 @@
 *     - "password" is the password
 *     - WPA is the security
 */
-WiflyInterface wifly(p9, p10, p25, p26, "mbed", "password", WPA);
+WiflyInterface wifly(p9, p10, p25, p26, "solarskin", "solarskin", WPA);
 
 int main (void)
 {
     wifly.init(); // use DHCP
     while (!wifly.connect()); // join the network
     printf("IP Address is %s\n\r", wifly.getIPAddress());
-
+    
     TCPSocketServer server;
     server.bind(ECHO_SERVER_PORT);
     server.listen();
@@ -28,11 +35,62 @@
     server.accept(client);
 
     char buffer[256];
+    char data[256];
+    float temp_c;
+    float range;
+    int packet_no=1;
+    int flag=0;
+    char motion = 'N';
+    
     while (true) {
-        int n = client.receive(buffer, sizeof(buffer));
-        if (n <= 0) continue;
-        buffer[n] = 0;
-
-        client.send_all(buffer, n);
-    }
+            
+            server.set_blocking(false, 100);
+            int n = client.receive(buffer, sizeof(buffer));
+            printf("\r N = %d\n",n);
+            if(n>6)
+             {
+                flag=1;
+             }
+            if (n <= 0) break;
+            
+            temp_c=(t.read()*330.0)-50;
+            printf("\rtemp is%d\n",(int)temp_c);
+            
+            float light = l.read_u16();
+            printf("\rlight value is %f\n",light);
+            
+            Pinger.Send();    
+            wait_ms(30);
+            range = Pinger.Read_cm();
+            
+            if(!PIR){
+                motion = 'Y';
+            }
+            else motion = 'N';
+            
+            if (n <= 0) break;
+            
+            if (flag == 1)
+            {
+                for (int i=5;i<n;i++)    
+                    printf("%c",buffer[i]);
+                data[0]=MY_NODE_ID;
+                data[1]=packet_no;
+                data[2]=(int)temp_c;
+                data[3]=(int)(light/100);
+                data[4]=range;
+                data[5]=motion;
+                data[6]='\0';
+            
+                n=sprintf(buffer,"%s",data);
+            //n=sprintf(buffer,"%s",data);
+            
+                printf("\n\n");
+                client.send_all(buffer, n);
+                packet_no++;
+                flag=0;
+            }
+            
+        }
+        client.close(); //Closes the connection with the client
 }
\ No newline at end of file