Updated Sensors Module. Added person counter.

Dependencies:   EthernetInterface HC_SR04_Ultrasonic_Library Middleware mbed-rtos mbed

Fork of SmartLabProyect by Jorge Martin Beltran Avila

Committer:
RoHe
Date:
Wed Dec 09 04:45:01 2015 +0000
Revision:
1:106e93f9c7c8
Parent:
0:5db5c980e717
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RoHe 0:5db5c980e717 1 /*
RoHe 0:5db5c980e717 2
RoHe 0:5db5c980e717 3 TODO:
RoHe 0:5db5c980e717 4
RoHe 0:5db5c980e717 5 1.- Conectar a ambos servidores
RoHe 0:5db5c980e717 6 1.1 - si no solo conectar a uno y tener present la conección activa
RoHe 0:5db5c980e717 7 2.-hacer funciones para dvidir las cadenas de los mensajes
RoHe 0:5db5c980e717 8 3.-hace funcion para formar cadenas para enviar mensajes
RoHe 0:5db5c980e717 9 4. Formato de mensajes para que los pueda leer el server
RoHe 0:5db5c980e717 10
RoHe 0:5db5c980e717 11 */
RoHe 0:5db5c980e717 12
RoHe 0:5db5c980e717 13
RoHe 0:5db5c980e717 14 /*
RoHe 0:5db5c980e717 15
RoHe 1:106e93f9c7c8 16 // Connect to Server1
RoHe 1:106e93f9c7c8 17 //TODO: try to connect to both servers
RoHe 1:106e93f9c7c8 18 //TODO: only try 5 times after ti server1 goes down
RoHe 1:106e93f9c7c8 19 //TODO: cual es la diferencia entre recieved_all y recieved
RoHe 1:106e93f9c7c8 20 //TOTRY: que valor regresa cuando hace el connect???
RoHe 1:106e93f9c7c8 21
RoHe 1:106e93f9c7c8 22 //QUESTION: que son los numero que aparecen despues de los dos puntos,
RoHe 1:106e93f9c7c8 23 //parece ser uqe es el numero que noceciones que lleva acabo 192.168.1.81:49160
RoHe 1:106e93f9c7c8 24 //NOSIRVE: si se conecta los dos micros con el programa de TCP Server4 y se les pide un reply
RoHe 1:106e93f9c7c8 25 //los micros no contestan y para terminarla si pides dos veces un reply se queda colgado ..
RoHe 1:106e93f9c7c8 26 //ACCOMPLISH: los colores de los leds ya responen de acuerdo a la conexion
RoHe 1:106e93f9c7c8 27
RoHe 1:106e93f9c7c8 28 //GetPersonStatus(1);
RoHe 1:106e93f9c7c8 29
RoHe 1:106e93f9c7c8 30 for(int i=0,isConServer1=0; i<2 && isConServer1==0; i++) {
RoHe 1:106e93f9c7c8 31 if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) {
RoHe 1:106e93f9c7c8 32 _isConnectedServer1=false;
RoHe 1:106e93f9c7c8 33 pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1);
RoHe 1:106e93f9c7c8 34 pc.printf("TRY SERVER 1 number (%d)\n\r",(i+1));
RoHe 1:106e93f9c7c8 35 isConServer1=0;
RoHe 1:106e93f9c7c8 36 } else {
RoHe 1:106e93f9c7c8 37 _isConnectedServer1=socketTCP1.is_connected();
RoHe 1:106e93f9c7c8 38 pc.printf("Ya se conecto con el servidor 1\n\r");
RoHe 1:106e93f9c7c8 39 isConServer1=1;
RoHe 1:106e93f9c7c8 40 wait(1);
RoHe 0:5db5c980e717 41
RoHe 1:106e93f9c7c8 42 char hello[] = "Hello World\n\r";
RoHe 1:106e93f9c7c8 43 printf("Sending message to Server : '%s' \n",hello);
RoHe 1:106e93f9c7c8 44 socketTCP1.send_all(hello, sizeof(hello) - 1);
RoHe 1:106e93f9c7c8 45
RoHe 1:106e93f9c7c8 46
RoHe 1:106e93f9c7c8 47
RoHe 1:106e93f9c7c8 48
RoHe 1:106e93f9c7c8 49 }
RoHe 1:106e93f9c7c8 50 }//END FOR1
RoHe 1:106e93f9c7c8 51
RoHe 1:106e93f9c7c8 52 for(int i=0,isConServer2=0; i<2 && isConServer2==0; i++) {
RoHe 1:106e93f9c7c8 53 if(socketTCP2.connect(ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2) == -1) {
RoHe 1:106e93f9c7c8 54 _isConnectedServer2=false;
RoHe 1:106e93f9c7c8 55 pc.printf("Unable to connect2 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2);
RoHe 1:106e93f9c7c8 56 pc.printf("TRY SERVER 2 number (%d)\n\r",(i+1));
RoHe 1:106e93f9c7c8 57 isConServer2=0;
RoHe 1:106e93f9c7c8 58 //wait(1);
RoHe 1:106e93f9c7c8 59 } else {
RoHe 1:106e93f9c7c8 60 _isConnectedServer2=socketTCP2.is_connected();
RoHe 1:106e93f9c7c8 61 pc.printf("Ya se conecto con el servidor 2\n\r");
RoHe 1:106e93f9c7c8 62 isConServer2=1;
RoHe 1:106e93f9c7c8 63 }
RoHe 1:106e93f9c7c8 64 }//END FOR2
RoHe 1:106e93f9c7c8 65
RoHe 1:106e93f9c7c8 66
RoHe 1:106e93f9c7c8 67 //RECEIVED from TCP SERVER1
RoHe 1:106e93f9c7c8 68 n1 = socketTCP1.receive_all(buf1, 256);
RoHe 0:5db5c980e717 69 buf1[n1] = '\0';
RoHe 0:5db5c980e717 70 if(n1>0) {
RoHe 0:5db5c980e717 71 pc.printf("Received message from server1 IF: '%s'\n\r", buf1);
RoHe 1:106e93f9c7c8 72 socketTCP1.close();
RoHe 1:106e93f9c7c8 73
RoHe 0:5db5c980e717 74 } else {
RoHe 0:5db5c980e717 75 //_isConnectedServer1==false; //quiere decir que se desconecto del servidor
RoHe 1:106e93f9c7c8 76 pc.printf("Not received1 ELSE value of lenght: '%u'\n\r",strlen(buf1));
RoHe 0:5db5c980e717 77 socketTCP1.close();
RoHe 0:5db5c980e717 78 //isConnectedServer1=false;
RoHe 0:5db5c980e717 79 }
RoHe 1:106e93f9c7c8 80 //socketTCP1.close();
RoHe 1:106e93f9c7c8 81 //socketTCP2.close();
RoHe 1:106e93f9c7c8 82
RoHe 1:106e93f9c7c8 83 if(strcmp(buf1,"reply")==0) {
RoHe 1:106e93f9c7c8 84 // SEND to server1
RoHe 1:106e93f9c7c8 85 //Entra pero no puede mandar los datos
RoHe 1:106e93f9c7c8 86 // pc.printf("Entro a la funcion strcmp: \n\r");
RoHe 1:106e93f9c7c8 87 if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) {
RoHe 1:106e93f9c7c8 88 pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1);
RoHe 1:106e93f9c7c8 89 }
RoHe 1:106e93f9c7c8 90
RoHe 1:106e93f9c7c8 91 char c_myIP[] = "192.168.1.77\n\r";
RoHe 1:106e93f9c7c8 92 pc.printf("Sending message to Server : %s \n\r",c_myIP);
RoHe 1:106e93f9c7c8 93 socketTCP1.send_all(c_myIP, sizeof(c_myIP) - 1);
RoHe 1:106e93f9c7c8 94 socketTCP1.close();
RoHe 1:106e93f9c7c8 95 }
RoHe 1:106e93f9c7c8 96 //socketTCP1.close();
RoHe 1:106e93f9c7c8 97
RoHe 1:106e93f9c7c8 98
RoHe 1:106e93f9c7c8 99
RoHe 1:106e93f9c7c8 100 //RECEIVED from TCP SERVER2
RoHe 1:106e93f9c7c8 101 n2 = socketTCP2.receive(buf2, 256);
RoHe 1:106e93f9c7c8 102 buf2[n2] = '\0';
RoHe 1:106e93f9c7c8 103 if(n2>0) {
RoHe 1:106e93f9c7c8 104 pc.printf("Received message from server2 IF: '%s'\n\r", buf2);
RoHe 1:106e93f9c7c8 105 socketTCP2.close();
RoHe 1:106e93f9c7c8 106 //isConnectedServer1=false;
RoHe 1:106e93f9c7c8 107 } else {
RoHe 1:106e93f9c7c8 108 //_isConnectedServer1==false; //quiere decir que se desconecto del servidor
RoHe 1:106e93f9c7c8 109 pc.printf("Not received2 ELSE value of lenght: '%u'\n\r",strlen(buf2));
RoHe 1:106e93f9c7c8 110 socketTCP2.close();
RoHe 1:106e93f9c7c8 111 //isConnectedServer1=false;
RoHe 1:106e93f9c7c8 112 }
RoHe 1:106e93f9c7c8 113 //
RoHe 1:106e93f9c7c8 114
RoHe 1:106e93f9c7c8 115
RoHe 1:106e93f9c7c8 116 */
RoHe 1:106e93f9c7c8 117
RoHe 1:106e93f9c7c8 118
RoHe 1:106e93f9c7c8 119
RoHe 1:106e93f9c7c8 120 /*
RoHe 1:106e93f9c7c8 121
RoHe 1:106e93f9c7c8 122 // Receive message from server
RoHe 1:106e93f9c7c8 123 char buf1[256];
RoHe 1:106e93f9c7c8 124 int n1 = socketTCP1.receive(buf1, 256);
RoHe 1:106e93f9c7c8 125 buf1[n1] = '\0';
RoHe 1:106e93f9c7c8 126
RoHe 1:106e93f9c7c8 127 if(n1>0) {
RoHe 1:106e93f9c7c8 128 pc.printf("Received message from server1 IF: '%s'\n\r", buf1);
RoHe 1:106e93f9c7c8 129 socketTCP1.close();
RoHe 1:106e93f9c7c8 130 //isConnectedServer1=false;
RoHe 1:106e93f9c7c8 131 } else {
RoHe 1:106e93f9c7c8 132 //_isConnectedServer1==false; //quiere decir que se desconecto del servidor
RoHe 1:106e93f9c7c8 133 pc.printf("Not received ELSE value of lenght: '%u'\n\r",strlen(buf1));
RoHe 1:106e93f9c7c8 134 socketTCP1.close();
RoHe 1:106e93f9c7c8 135 //isConnectedServer1=false;
RoHe 1:106e93f9c7c8 136 }
RoHe 1:106e93f9c7c8 137
RoHe 1:106e93f9c7c8 138 if(bo_somethingWrong==true) {
RoHe 1:106e93f9c7c8 139 return 0;
RoHe 1:106e93f9c7c8 140 }
RoHe 1:106e93f9c7c8 141
RoHe 1:106e93f9c7c8 142 //if(socketTCP.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1 && _isConnectedServer1==false ) {
RoHe 1:106e93f9c7c8 143 if(socketTCP.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) {
RoHe 1:106e93f9c7c8 144 _isConnectedServer1=socketTCP.is_connected();
RoHe 1:106e93f9c7c8 145 //_isConnectedServer1=false;
RoHe 1:106e93f9c7c8 146 pc.printf("Unable to connect to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1);
RoHe 1:106e93f9c7c8 147 wait(1);
RoHe 1:106e93f9c7c8 148 } else {
RoHe 1:106e93f9c7c8 149 _isConnectedServer1=socketTCP.is_connected();
RoHe 1:106e93f9c7c8 150 //_isConnectedServer1=true;
RoHe 1:106e93f9c7c8 151 //pc.printf("Connected to Server at %s\n\r",ECHO_SERVER_ADDRESS1);
RoHe 1:106e93f9c7c8 152 pc.printf("Connected to Server at %s\n\r",socketTCP.get_address());
RoHe 1:106e93f9c7c8 153 pc.printf("Connected to PORT at %u\n\r",socketTCP.get_port());
RoHe 1:106e93f9c7c8 154
RoHe 1:106e93f9c7c8 155
RoHe 1:106e93f9c7c8 156
RoHe 1:106e93f9c7c8 157 //TODO: split strings
RoHe 1:106e93f9c7c8 158
RoHe 1:106e93f9c7c8 159 char hello[] = "reply:get:status:192.168.1.77:door:1\n\r";
RoHe 1:106e93f9c7c8 160
RoHe 1:106e93f9c7c8 161 pc.printf("Sending message to Server : %s \n\r",hello);
RoHe 1:106e93f9c7c8 162 socketTCP.send_all(hello, sizeof(hello)- 1);
RoHe 1:106e93f9c7c8 163
RoHe 1:106e93f9c7c8 164
RoHe 1:106e93f9c7c8 165
RoHe 1:106e93f9c7c8 166 // Receive message from server
RoHe 1:106e93f9c7c8 167 char buf[256];
RoHe 1:106e93f9c7c8 168 int n = socketTCP.receive(buf, 256);
RoHe 1:106e93f9c7c8 169 buf[n] = '\0';
RoHe 1:106e93f9c7c8 170 pc.printf("Received message from server: '%s'\n\r", buf);
RoHe 0:5db5c980e717 171
RoHe 0:5db5c980e717 172 if(bo_somethingWrong==true) {
RoHe 0:5db5c980e717 173 return 0;
RoHe 0:5db5c980e717 174 }
RoHe 0:5db5c980e717 175
RoHe 1:106e93f9c7c8 176
RoHe 1:106e93f9c7c8 177 }
RoHe 0:5db5c980e717 178
RoHe 0:5db5c980e717 179
RoHe 0:5db5c980e717 180
RoHe 1:106e93f9c7c8 181 }
RoHe 0:5db5c980e717 182
RoHe 1:106e93f9c7c8 183 // Clean up
RoHe 1:106e93f9c7c8 184 socketTCP1.close();
RoHe 1:106e93f9c7c8 185 socketTCP2.close();
RoHe 1:106e93f9c7c8 186 */
RoHe 0:5db5c980e717 187
RoHe 0:5db5c980e717 188
RoHe 0:5db5c980e717 189
RoHe 0:5db5c980e717 190 // Send message to server
RoHe 0:5db5c980e717 191 /*
RoHe 0:5db5c980e717 192 strcat(s_myIP,"\n\r");
RoHe 0:5db5c980e717 193 pc.printf("Sending message to Server : %s \n\r",s_myIP);
RoHe 0:5db5c980e717 194 socketTCP.send_all(s_myIP, sizeof(s_myIP) - 1);
RoHe 0:5db5c980e717 195 */
RoHe 0:5db5c980e717 196 /*
RoHe 0:5db5c980e717 197 c_myIP = "192.168.1.77\n\r";
RoHe 0:5db5c980e717 198 //strcat(c_myIP,"\n\r");
RoHe 0:5db5c980e717 199 pc.printf("Sending message to Server : %s \n\r",c_myIP);
RoHe 0:5db5c980e717 200 socketTCP.send_all(c_myIP, sizeof(c_myIP) - 1);
RoHe 0:5db5c980e717 201 */
RoHe 0:5db5c980e717 202
RoHe 0:5db5c980e717 203 //char hello[] = "reply:get:";
RoHe 0:5db5c980e717 204 //char hello2[] = "status:192.168.1.77:door:1\n\r" ;
RoHe 0:5db5c980e717 205 //strcat(hello,hello2);
RoHe 0:5db5c980e717 206
RoHe 0:5db5c980e717 207
RoHe 0:5db5c980e717 208 //THREADS CSDECLARATION
RoHe 0:5db5c980e717 209 //Thread thread2(conect_Server);
RoHe 0:5db5c980e717 210 //Thread *thread2;
RoHe 0:5db5c980e717 211 //thread2 = new Thread(conect_Server);
RoHe 0:5db5c980e717 212
RoHe 1:106e93f9c7c8 213 /*
RoHe 1:106e93f9c7c8 214 // Echo received message back to client
RoHe 1:106e93f9c7c8 215 pc.printf("bufferReply: '%s'\n\r",bufferReply);
RoHe 1:106e93f9c7c8 216 //Todas las cadenas que son enviadas tienen el final de \r\n
RoHe 1:106e93f9c7c8 217 int lengBuffer = stringManager.stringLenght(bufferReply);
RoHe 1:106e93f9c7c8 218 pc.printf("longhtBuffer: '%u'\n\r",strlen(buffer));
RoHe 1:106e93f9c7c8 219 pc.printf("longhtBuffer n : '%u'\n\r",n);
RoHe 1:106e93f9c7c8 220 pc.printf("longhtBufferReply: '%u'\n\r",lengBuffer);
RoHe 1:106e93f9c7c8 221 pc.printf("strcmp: '%u'\n\r",strcmp(buffer,"set\r\n"));
RoHe 1:106e93f9c7c8 222 */
RoHe 0:5db5c980e717 223
RoHe 0:5db5c980e717 224
RoHe 0:5db5c980e717 225 //IMPORT FROM LIBRARY
RoHe 0:5db5c980e717 226 /*
RoHe 0:5db5c980e717 227 cad = driver.echo();
RoHe 0:5db5c980e717 228 pc.printf("libraryECHO: %s\n\r",cad);
RoHe 0:5db5c980e717 229 int var = driver.is_good();
RoHe 0:5db5c980e717 230 pc.printf("libraryVAR: %u\n\r",var);
RoHe 0:5db5c980e717 231 */
RoHe 0:5db5c980e717 232
RoHe 0:5db5c980e717 233