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
Diff: main.cpp
- Revision:
- 2:b15847b47f78
- Parent:
- 1:9ae2041887fa
- Child:
- 3:13b54c5c407c
--- a/main.cpp Wed Mar 27 18:16:27 2013 +0000 +++ b/main.cpp Fri Apr 05 01:47:23 2013 +0000 @@ -9,6 +9,15 @@ AnalogIn t(p19); //TMP36 is connected here AnalogIn l(p18); DigitalIn PIR(p17); +PwmOut dimmer(p22); +PwmOut servo(p24); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut myled4(LED4); +DigitalIn cam(p16); + +extern "C" void mbed_reset(); /* wifly object where: * - p9 and p10 are for the serial communication @@ -20,6 +29,26 @@ */ WiflyInterface wifly(p9, p10, p25, p26, "solarskin", "solarskin", WPA); +class Watchdog { +public: +// Load timeout value in watchdog timer and enable + void kick(float s) { + LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK + uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 + LPC_WDT->WDTC = s * (float)clk; + LPC_WDT->WDMOD = 0x3; // Enabled and Reset + kick(); + } +// "kick" or "feed" the dog - reset the watchdog timer +// by writing this required bit pattern + void kick() { + LPC_WDT->WDFEED = 0xAA; + LPC_WDT->WDFEED = 0x55; + } +}; + +Watchdog wdt; + int main (void) { wifly.init(); // use DHCP @@ -27,70 +56,150 @@ printf("IP Address is %s\n\r", wifly.getIPAddress()); TCPSocketServer server; + server.set_blocking(false, 1500); server.bind(ECHO_SERVER_PORT); server.listen(); - + + + printf("\nWait for new connection...\n"); TCPSocketConnection client; server.accept(client); - + client.set_blocking(false,1500); + wdt.kick(10.0); + char buffer[256]; + char b='0'; char data[256]; float temp_c; + char fan = '0'; + char camera = '0'; + int count=0; float range; int packet_no=1; - int flag=0; - char motion = 'N'; + char motion = '0'; + int n = 9; 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; + if(!PIR){ + led3 = 1; + motion = '1'; + } + else + { + led3=0; + motion = '0'; + } + if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;} temp_c=(t.read()*330.0)-50; printf("\rtemp is%d\n",(int)temp_c); - + if(!PIR){ + led3 = 1; + motion = '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'; + } + 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(!PIR){ - motion = 'Y'; + led3 = 1; + motion = '1'; + } + else + { + led3=0; + motion = '0'; } - else motion = 'N'; - - if (n <= 0) break; - - if (flag == 1) + if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;} + if (temp_c >25 || (b==49)) + { + fan = '1'; + led1 = 1; + servo.pulsewidth(0.002); + wait(1); + } + else + { + fan = '0'; + servo.pulsewidth(0); + wait(1); + + } + if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;} + if(light<6000) { - 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'; + dimmer = 0.02; + } + else dimmer = 0; + 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++; + + 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]; + data[3]=(int)motion; + data[5]=MY_NODE_ID; + 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'; n=sprintf(buffer,"%s",data); - //n=sprintf(buffer,"%s",data); - printf("\n\n"); client.send_all(buffer, n); packet_no++; - flag=0; + + /*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'; + } } + + + wdt.kick(); } client.close(); //Closes the connection with the client } \ No newline at end of file