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
main.cpp
- Committer:
- Neel
- Date:
- 2013-03-27
- Revision:
- 1:9ae2041887fa
- Parent:
- 0:0710a5e21ef9
- Child:
- 2:b15847b47f78
File content as of revision 1:9ae2041887fa:
#include "mbed.h" #include "WiflyInterface.h" #include "Ping.h" #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 * - p25 is for the reset pin * - p26 is for the connection status * - "mbed" is the ssid of the network * - "password" is the password * - WPA is the security */ 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(); printf("\nWait for new connection...\n"); TCPSocketConnection client; 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) { 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 }