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

Committer:
Neel
Date:
Sun Apr 21 22:32:45 2013 +0000
Revision:
3:13b54c5c407c
Parent:
2:b15847b47f78
Final code on 04/21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:0710a5e21ef9 1 #include "mbed.h"
samux 0:0710a5e21ef9 2 #include "WiflyInterface.h"
Neel 1:9ae2041887fa 3 #include "Ping.h"
samux 0:0710a5e21ef9 4
Neel 1:9ae2041887fa 5 #define ECHO_SERVER_PORT 12345
Neel 1:9ae2041887fa 6 #define MY_NODE_ID 1
Neel 1:9ae2041887fa 7
Neel 1:9ae2041887fa 8 Ping Pinger(p21);
Neel 1:9ae2041887fa 9 AnalogIn t(p19); //TMP36 is connected here
Neel 1:9ae2041887fa 10 AnalogIn l(p18);
Neel 1:9ae2041887fa 11 DigitalIn PIR(p17);
Neel 2:b15847b47f78 12 PwmOut dimmer(p22);
Neel 2:b15847b47f78 13 PwmOut servo(p24);
Neel 2:b15847b47f78 14 DigitalOut led1(LED1);
Neel 2:b15847b47f78 15 DigitalOut led2(LED2);
Neel 2:b15847b47f78 16 DigitalOut led3(LED3);
Neel 2:b15847b47f78 17 DigitalOut myled4(LED4);
Neel 2:b15847b47f78 18 DigitalIn cam(p16);
Neel 2:b15847b47f78 19
Neel 2:b15847b47f78 20 extern "C" void mbed_reset();
samux 0:0710a5e21ef9 21
samux 0:0710a5e21ef9 22 /* wifly object where:
samux 0:0710a5e21ef9 23 * - p9 and p10 are for the serial communication
samux 0:0710a5e21ef9 24 * - p25 is for the reset pin
samux 0:0710a5e21ef9 25 * - p26 is for the connection status
samux 0:0710a5e21ef9 26 * - "mbed" is the ssid of the network
samux 0:0710a5e21ef9 27 * - "password" is the password
samux 0:0710a5e21ef9 28 * - WPA is the security
samux 0:0710a5e21ef9 29 */
Neel 1:9ae2041887fa 30 WiflyInterface wifly(p9, p10, p25, p26, "solarskin", "solarskin", WPA);
samux 0:0710a5e21ef9 31
Neel 2:b15847b47f78 32 class Watchdog {
Neel 2:b15847b47f78 33 public:
Neel 2:b15847b47f78 34 // Load timeout value in watchdog timer and enable
Neel 2:b15847b47f78 35 void kick(float s) {
Neel 2:b15847b47f78 36 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
Neel 2:b15847b47f78 37 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
Neel 2:b15847b47f78 38 LPC_WDT->WDTC = s * (float)clk;
Neel 2:b15847b47f78 39 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
Neel 2:b15847b47f78 40 kick();
Neel 2:b15847b47f78 41 }
Neel 2:b15847b47f78 42 // "kick" or "feed" the dog - reset the watchdog timer
Neel 2:b15847b47f78 43 // by writing this required bit pattern
Neel 2:b15847b47f78 44 void kick() {
Neel 2:b15847b47f78 45 LPC_WDT->WDFEED = 0xAA;
Neel 2:b15847b47f78 46 LPC_WDT->WDFEED = 0x55;
Neel 2:b15847b47f78 47 }
Neel 2:b15847b47f78 48 };
Neel 2:b15847b47f78 49
Neel 2:b15847b47f78 50 Watchdog wdt;
Neel 2:b15847b47f78 51
samux 0:0710a5e21ef9 52 int main (void)
samux 0:0710a5e21ef9 53 {
Neel 3:13b54c5c407c 54 wifly.init("192.168.1.101","255.255.255.0","192.168.1.1"); // use Static
Neel 3:13b54c5c407c 55 //wifly.init();
samux 0:0710a5e21ef9 56 while (!wifly.connect()); // join the network
samux 0:0710a5e21ef9 57 printf("IP Address is %s\n\r", wifly.getIPAddress());
Neel 1:9ae2041887fa 58
samux 0:0710a5e21ef9 59 TCPSocketServer server;
Neel 2:b15847b47f78 60 server.set_blocking(false, 1500);
samux 0:0710a5e21ef9 61 server.bind(ECHO_SERVER_PORT);
samux 0:0710a5e21ef9 62 server.listen();
Neel 2:b15847b47f78 63
samux 0:0710a5e21ef9 64 printf("\nWait for new connection...\n");
samux 0:0710a5e21ef9 65 TCPSocketConnection client;
samux 0:0710a5e21ef9 66 server.accept(client);
Neel 2:b15847b47f78 67 client.set_blocking(false,1500);
Neel 2:b15847b47f78 68 wdt.kick(10.0);
Neel 2:b15847b47f78 69
samux 0:0710a5e21ef9 70 char buffer[256];
Neel 2:b15847b47f78 71 char b='0';
Neel 3:13b54c5c407c 72 char dim = '0';
Neel 1:9ae2041887fa 73 char data[256];
Neel 1:9ae2041887fa 74 float temp_c;
Neel 2:b15847b47f78 75 char fan = '0';
Neel 2:b15847b47f78 76 char camera = '0';
Neel 3:13b54c5c407c 77 long count=0;
Neel 3:13b54c5c407c 78 float range = 0;
Neel 1:9ae2041887fa 79 int packet_no=1;
Neel 2:b15847b47f78 80 char motion = '0';
Neel 2:b15847b47f78 81 int n = 9;
Neel 1:9ae2041887fa 82
Neel 3:13b54c5c407c 83 float personCounter = 0;
Neel 3:13b54c5c407c 84 bool personFlag = false;
Neel 3:13b54c5c407c 85
Neel 3:13b54c5c407c 86 float tempAvg = 0;
Neel 3:13b54c5c407c 87
Neel 3:13b54c5c407c 88 int lightIter=0;
Neel 3:13b54c5c407c 89
Neel 3:13b54c5c407c 90 while (true)
Neel 3:13b54c5c407c 91 {
Neel 3:13b54c5c407c 92
Neel 3:13b54c5c407c 93 if(lightIter == 0)
Neel 2:b15847b47f78 94 {
Neel 2:b15847b47f78 95 led3=0;
Neel 2:b15847b47f78 96 motion = '0';
Neel 2:b15847b47f78 97 }
Neel 3:13b54c5c407c 98 lightIter++;
Neel 3:13b54c5c407c 99
Neel 3:13b54c5c407c 100 if(lightIter == 10)
Neel 3:13b54c5c407c 101 lightIter = 0;
Neel 3:13b54c5c407c 102
Neel 3:13b54c5c407c 103 if(!PIR)
Neel 3:13b54c5c407c 104 {
Neel 3:13b54c5c407c 105 led3 = 1;
Neel 3:13b54c5c407c 106 motion = '1';
Neel 3:13b54c5c407c 107 lightIter = 1;
Neel 3:13b54c5c407c 108 }
Neel 3:13b54c5c407c 109
Neel 2:b15847b47f78 110 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 3:13b54c5c407c 111
Neel 3:13b54c5c407c 112 temp_c=((t.read()*330.0)-50)+7;
Neel 3:13b54c5c407c 113
Neel 3:13b54c5c407c 114 tempAvg = temp_c +tempAvg;
Neel 3:13b54c5c407c 115
Neel 1:9ae2041887fa 116 printf("\rtemp is%d\n",(int)temp_c);
Neel 2:b15847b47f78 117 if(!PIR){
Neel 2:b15847b47f78 118 led3 = 1;
Neel 2:b15847b47f78 119 motion = '1';
Neel 3:13b54c5c407c 120 lightIter = 1;
Neel 2:b15847b47f78 121 }
Neel 3:13b54c5c407c 122
Neel 2:b15847b47f78 123 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 3:13b54c5c407c 124
Neel 1:9ae2041887fa 125 float light = l.read_u16();
Neel 3:13b54c5c407c 126
Neel 1:9ae2041887fa 127 printf("\rlight value is %f\n",light);
Neel 2:b15847b47f78 128 if(!PIR){
Neel 2:b15847b47f78 129 led3 = 1;
Neel 2:b15847b47f78 130 motion = '1';
Neel 3:13b54c5c407c 131 lightIter = 1;
Neel 2:b15847b47f78 132 }
Neel 3:13b54c5c407c 133
Neel 2:b15847b47f78 134 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 1:9ae2041887fa 135 Pinger.Send();
Neel 1:9ae2041887fa 136 wait_ms(30);
Neel 1:9ae2041887fa 137 range = Pinger.Read_cm();
Neel 1:9ae2041887fa 138
Neel 3:13b54c5c407c 139 if(range < 60.0 && range >= 0.0 && personFlag == false)
Neel 3:13b54c5c407c 140 {
Neel 3:13b54c5c407c 141 personFlag = true;
Neel 3:13b54c5c407c 142 personCounter = personCounter + 1.0;
Neel 3:13b54c5c407c 143 }
Neel 3:13b54c5c407c 144 if(range >= 60.0 && personFlag == true)
Neel 3:13b54c5c407c 145 {
Neel 3:13b54c5c407c 146 personFlag = false;
Neel 3:13b54c5c407c 147 }
Neel 3:13b54c5c407c 148 printf("\rPerson counter is %f\n",range);
Neel 3:13b54c5c407c 149
Neel 1:9ae2041887fa 150 if(!PIR){
Neel 2:b15847b47f78 151 led3 = 1;
Neel 2:b15847b47f78 152 motion = '1';
Neel 3:13b54c5c407c 153 lightIter = 1;
Neel 2:b15847b47f78 154 }
Neel 3:13b54c5c407c 155
Neel 2:b15847b47f78 156 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 2:b15847b47f78 157 if (temp_c >25 || (b==49))
Neel 2:b15847b47f78 158 {
Neel 2:b15847b47f78 159 fan = '1';
Neel 2:b15847b47f78 160 led1 = 1;
Neel 2:b15847b47f78 161 servo.pulsewidth(0.002);
Neel 2:b15847b47f78 162 wait(1);
Neel 2:b15847b47f78 163 }
Neel 3:13b54c5c407c 164 if (temp_c <=25 && (b==48))
Neel 2:b15847b47f78 165 {
Neel 2:b15847b47f78 166 fan = '0';
Neel 3:13b54c5c407c 167 servo.pulsewidth(0);
Neel 3:13b54c5c407c 168 wait(1);
Neel 2:b15847b47f78 169 }
Neel 2:b15847b47f78 170 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 3:13b54c5c407c 171
Neel 3:13b54c5c407c 172 //
Neel 3:13b54c5c407c 173
Neel 3:13b54c5c407c 174 if(((dim-48) == 0 || (dim-48) == 1) && motion == '1')
Neel 1:9ae2041887fa 175 {
Neel 3:13b54c5c407c 176 dimmer = 0.07;
Neel 2:b15847b47f78 177 }
Neel 3:13b54c5c407c 178 else
Neel 3:13b54c5c407c 179 {
Neel 3:13b54c5c407c 180 dimmer = 0.01286 * (dim-48) - .015714;
Neel 3:13b54c5c407c 181 }
Neel 3:13b54c5c407c 182
Neel 3:13b54c5c407c 183 if(!cam){camera = '1'; myled4=1;}else{camera='0';myled4=0;}
Neel 3:13b54c5c407c 184
Neel 2:b15847b47f78 185 n = client.receive(buffer, sizeof(buffer));
Neel 2:b15847b47f78 186 printf("\r N = %d\n",n);
Neel 2:b15847b47f78 187
Neel 3:13b54c5c407c 188 count++;
Neel 3:13b54c5c407c 189
Neel 2:b15847b47f78 190
Neel 3:13b54c5c407c 191
Neel 3:13b54c5c407c 192 //if (count>10) { mbed_reset(); count=0;}
Neel 3:13b54c5c407c 193
Neel 2:b15847b47f78 194 if (n>1)
Neel 2:b15847b47f78 195 {
Neel 3:13b54c5c407c 196 printf("Received bytes %d",n);
Neel 3:13b54c5c407c 197 for (int i=5;i<n;i++)
Neel 3:13b54c5c407c 198 //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]);
Neel 3:13b54c5c407c 199 printf("%d, %c\t",i,buffer[i]);
Neel 3:13b54c5c407c 200 printf("\n");
Neel 3:13b54c5c407c 201 if (buffer[7] == 49 || buffer[7]==48)
Neel 3:13b54c5c407c 202 b=buffer[7];
Neel 3:13b54c5c407c 203
Neel 3:13b54c5c407c 204 if(buffer[18]>=48 && buffer[18]<=57)
Neel 3:13b54c5c407c 205 dim = buffer[18];
Neel 3:13b54c5c407c 206
Neel 2:b15847b47f78 207 data[3]=(int)motion;
Neel 3:13b54c5c407c 208 data[5]=personCounter;
Neel 2:b15847b47f78 209 data[4]=packet_no;
Neel 2:b15847b47f78 210 data[0]=(int)temp_c;
Neel 2:b15847b47f78 211 data[1]=(int)(light/100);
Neel 2:b15847b47f78 212 data[2]=range;
Neel 2:b15847b47f78 213 data[6]=(int)fan;
Neel 2:b15847b47f78 214 data[7]=(int)camera;
Neel 3:13b54c5c407c 215 data[8] =(int)(tempAvg/count);
Neel 3:13b54c5c407c 216 data[9]='\0';
Neel 1:9ae2041887fa 217
Neel 1:9ae2041887fa 218 n=sprintf(buffer,"%s",data);
Neel 1:9ae2041887fa 219 printf("\n\n");
Neel 1:9ae2041887fa 220 client.send_all(buffer, n);
Neel 1:9ae2041887fa 221 packet_no++;
Neel 2:b15847b47f78 222
Neel 2:b15847b47f78 223
Neel 2:b15847b47f78 224 for (int i=5; i<n; i++)
Neel 2:b15847b47f78 225 {
Neel 2:b15847b47f78 226 buffer[i]='0';
Neel 2:b15847b47f78 227 }
Neel 1:9ae2041887fa 228 }
Neel 2:b15847b47f78 229
Neel 1:9ae2041887fa 230
Neel 2:b15847b47f78 231
Neel 2:b15847b47f78 232 wdt.kick();
Neel 1:9ae2041887fa 233 }
Neel 1:9ae2041887fa 234 client.close(); //Closes the connection with the client
samux 0:0710a5e21ef9 235 }