DSOS Sensors
Dependencies: EthernetInterface SensorsTointegrate mbed-rtos mbed
Fork of MainTaskGus by
main.cpp@0:5db5c980e717, 2015-12-05 (annotated)
- Committer:
- RoHe
- Date:
- Sat Dec 05 17:46:07 2015 +0000
- Revision:
- 0:5db5c980e717
- Child:
- 1:f3619e6e9e89
MiddleWare GUS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RoHe | 0:5db5c980e717 | 1 | /* |
RoHe | 0:5db5c980e717 | 2 | |
RoHe | 0:5db5c980e717 | 3 | Main.cpp |
RoHe | 0:5db5c980e717 | 4 | |
RoHe | 0:5db5c980e717 | 5 | Last update by RoHe 16/11/2015 |
RoHe | 0:5db5c980e717 | 6 | |
RoHe | 0:5db5c980e717 | 7 | */ |
RoHe | 0:5db5c980e717 | 8 | #include <string.h> |
RoHe | 0:5db5c980e717 | 9 | #include "mbed.h" |
RoHe | 0:5db5c980e717 | 10 | #include "EthernetInterface.h" |
RoHe | 0:5db5c980e717 | 11 | #include "rtos.h" |
RoHe | 0:5db5c980e717 | 12 | #include "SensorPIR.h" |
RoHe | 0:5db5c980e717 | 13 | #include "Ether.h" |
RoHe | 0:5db5c980e717 | 14 | #include "Initial.h" |
RoHe | 0:5db5c980e717 | 15 | #include "Threads.cpp" |
RoHe | 0:5db5c980e717 | 16 | #include "StringManager.h" |
RoHe | 0:5db5c980e717 | 17 | |
RoHe | 0:5db5c980e717 | 18 | |
RoHe | 0:5db5c980e717 | 19 | int main() |
RoHe | 0:5db5c980e717 | 20 | { |
RoHe | 0:5db5c980e717 | 21 | /*//////////////////////////////////// |
RoHe | 0:5db5c980e717 | 22 | Initialization of the outports |
RoHe | 0:5db5c980e717 | 23 | |
RoHe | 0:5db5c980e717 | 24 | Comments: |
RoHe | 0:5db5c980e717 | 25 | -Inicialize all the output in zero |
RoHe | 0:5db5c980e717 | 26 | -It has Pull - up |
RoHe | 0:5db5c980e717 | 27 | */ |
RoHe | 0:5db5c980e717 | 28 | ledRED=1; // it has pull-up |
RoHe | 0:5db5c980e717 | 29 | ledGREEN=1; |
RoHe | 0:5db5c980e717 | 30 | ledBLUE=1; |
RoHe | 0:5db5c980e717 | 31 | |
RoHe | 0:5db5c980e717 | 32 | |
RoHe | 0:5db5c980e717 | 33 | /*//////////////////////////////////// |
RoHe | 0:5db5c980e717 | 34 | SET Initial configuration and welcome message |
RoHe | 0:5db5c980e717 | 35 | */ |
RoHe | 0:5db5c980e717 | 36 | pc.baud(115200); |
RoHe | 0:5db5c980e717 | 37 | pc.printf("Hello World from TCP Client-Server RoHE Main TASK GUS integrate YORCH1.\n\r"); |
RoHe | 0:5db5c980e717 | 38 | |
RoHe | 0:5db5c980e717 | 39 | |
RoHe | 0:5db5c980e717 | 40 | /*////////////////////////////////// |
RoHe | 0:5db5c980e717 | 41 | Declares all necessary CLASSES |
RoHe | 0:5db5c980e717 | 42 | */ |
RoHe | 0:5db5c980e717 | 43 | |
RoHe | 0:5db5c980e717 | 44 | EthernetInterface eth; |
RoHe | 0:5db5c980e717 | 45 | eth.init(); //Use DHCP |
RoHe | 0:5db5c980e717 | 46 | eth.connect(); |
RoHe | 0:5db5c980e717 | 47 | |
RoHe | 0:5db5c980e717 | 48 | |
RoHe | 0:5db5c980e717 | 49 | /*/////////////////////////////////// |
RoHe | 0:5db5c980e717 | 50 | Declares all necessary THREADS |
RoHe | 0:5db5c980e717 | 51 | */ |
RoHe | 0:5db5c980e717 | 52 | Thread thread1(led_thread); |
RoHe | 0:5db5c980e717 | 53 | //Thread thread2(receivedFromServer1); |
RoHe | 0:5db5c980e717 | 54 | |
RoHe | 0:5db5c980e717 | 55 | /*//////////////////////////////////// |
RoHe | 0:5db5c980e717 | 56 | Declares all necessary INTERRUPTIONS |
RoHe | 0:5db5c980e717 | 57 | */ |
RoHe | 0:5db5c980e717 | 58 | sw2.fall(&sw2_press); |
RoHe | 0:5db5c980e717 | 59 | |
RoHe | 0:5db5c980e717 | 60 | //////////////////////////////////////////////////////APPLICATION ///////////////////////////////////////////////////////// |
RoHe | 0:5db5c980e717 | 61 | |
RoHe | 0:5db5c980e717 | 62 | |
RoHe | 0:5db5c980e717 | 63 | s_myIP = eth.getIPAddress(); |
RoHe | 0:5db5c980e717 | 64 | pc.printf("\nClient IP Address is %s\r", s_myIP); |
RoHe | 0:5db5c980e717 | 65 | pc.printf("\nClient IP MAC address is %s\r", eth.getMACAddress); |
RoHe | 0:5db5c980e717 | 66 | pc.printf("\nClient IP Gateway is %s\r", eth.getGateway()); |
RoHe | 0:5db5c980e717 | 67 | pc.printf("\nClient IP network mask is %s\n\r", eth.getNetworkMask()); |
RoHe | 0:5db5c980e717 | 68 | /* |
RoHe | 0:5db5c980e717 | 69 | CONSTRAINS: |
RoHe | 0:5db5c980e717 | 70 | -No se puede poner un serial print el los threads .... |
RoHe | 0:5db5c980e717 | 71 | -se tiene que abrir y cerrar las conecciones par que pueda leer nuevos mensajes |
RoHe | 0:5db5c980e717 | 72 | -se puede leer mensajes si no se cierra el socket pero la conexion se queda abierta |
RoHe | 0:5db5c980e717 | 73 | y no puede recibir mas mensajes .. se saco el forpara hacerlo una sola vez y no se cierra la conexion |
RoHe | 0:5db5c980e717 | 74 | |
RoHe | 0:5db5c980e717 | 75 | */ |
RoHe | 0:5db5c980e717 | 76 | |
RoHe | 0:5db5c980e717 | 77 | |
RoHe | 0:5db5c980e717 | 78 | /* |
RoHe | 0:5db5c980e717 | 79 | // Connect to Server2 |
RoHe | 0:5db5c980e717 | 80 | (socketTCP.connect(ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2) < 0) { |
RoHe | 0:5db5c980e717 | 81 | pc.printf("Unable to connect to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2); |
RoHe | 0:5db5c980e717 | 82 | } |
RoHe | 0:5db5c980e717 | 83 | _isConnectedServer2=true; |
RoHe | 0:5db5c980e717 | 84 | pc.printf("Connected to Server at %s\n\r",ECHO_SERVER_ADDRESS2); |
RoHe | 0:5db5c980e717 | 85 | */ |
RoHe | 0:5db5c980e717 | 86 | |
RoHe | 0:5db5c980e717 | 87 | |
RoHe | 0:5db5c980e717 | 88 | while(true) { |
RoHe | 0:5db5c980e717 | 89 | |
RoHe | 0:5db5c980e717 | 90 | // Connect to Server1 |
RoHe | 0:5db5c980e717 | 91 | //TODO: try to connect to both servers |
RoHe | 0:5db5c980e717 | 92 | //TODO: only try 5 times after ti server1 goes down |
RoHe | 0:5db5c980e717 | 93 | //TODO: cual es la diferencia entre recieved_all y recieved |
RoHe | 0:5db5c980e717 | 94 | //TOTRY: que valor regresa cuando hace el connect??? |
RoHe | 0:5db5c980e717 | 95 | |
RoHe | 0:5db5c980e717 | 96 | //QUESTION: que son los numero que aparecen despues de los dos puntos, |
RoHe | 0:5db5c980e717 | 97 | //parece ser uqe es el numero que noceciones que lleva acabo 192.168.1.81:49160 |
RoHe | 0:5db5c980e717 | 98 | //NOSIRVE: si se conecta los dos micros con el programa de TCP Server4 y se les pide un reply |
RoHe | 0:5db5c980e717 | 99 | //los micros no contestan y para terminarla si pides dos veces un reply se queda colgado .. |
RoHe | 0:5db5c980e717 | 100 | //ACCOMPLISH: los colores de los leds ya responen de acuerdo a la conexion |
RoHe | 0:5db5c980e717 | 101 | |
RoHe | 0:5db5c980e717 | 102 | //GetPersonStatus(1); |
RoHe | 0:5db5c980e717 | 103 | |
RoHe | 0:5db5c980e717 | 104 | for(int i=0,isConServer1=0; i<2 && isConServer1==0; i++) { |
RoHe | 0:5db5c980e717 | 105 | if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) { |
RoHe | 0:5db5c980e717 | 106 | _isConnectedServer1=false; |
RoHe | 0:5db5c980e717 | 107 | pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1); |
RoHe | 0:5db5c980e717 | 108 | pc.printf("TRY SERVER 1 number (%d)\n\r",(i+1)); |
RoHe | 0:5db5c980e717 | 109 | isConServer1=0; |
RoHe | 0:5db5c980e717 | 110 | } else { |
RoHe | 0:5db5c980e717 | 111 | _isConnectedServer1=socketTCP1.is_connected(); |
RoHe | 0:5db5c980e717 | 112 | pc.printf("Ya se conecto con el servidor 1\n\r"); |
RoHe | 0:5db5c980e717 | 113 | isConServer1=1; |
RoHe | 0:5db5c980e717 | 114 | wait(1); |
RoHe | 0:5db5c980e717 | 115 | |
RoHe | 0:5db5c980e717 | 116 | char hello[] = "Hello World\n\r"; |
RoHe | 0:5db5c980e717 | 117 | printf("Sending message to Server : '%s' \n",hello); |
RoHe | 0:5db5c980e717 | 118 | socketTCP1.send_all(hello, sizeof(hello) - 1); |
RoHe | 0:5db5c980e717 | 119 | |
RoHe | 0:5db5c980e717 | 120 | |
RoHe | 0:5db5c980e717 | 121 | |
RoHe | 0:5db5c980e717 | 122 | |
RoHe | 0:5db5c980e717 | 123 | } |
RoHe | 0:5db5c980e717 | 124 | }//END FOR1 |
RoHe | 0:5db5c980e717 | 125 | |
RoHe | 0:5db5c980e717 | 126 | for(int i=0,isConServer2=0; i<2 && isConServer2==0; i++) { |
RoHe | 0:5db5c980e717 | 127 | if(socketTCP2.connect(ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2) == -1) { |
RoHe | 0:5db5c980e717 | 128 | _isConnectedServer2=false; |
RoHe | 0:5db5c980e717 | 129 | pc.printf("Unable to connect2 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2); |
RoHe | 0:5db5c980e717 | 130 | pc.printf("TRY SERVER 2 number (%d)\n\r",(i+1)); |
RoHe | 0:5db5c980e717 | 131 | isConServer2=0; |
RoHe | 0:5db5c980e717 | 132 | //wait(1); |
RoHe | 0:5db5c980e717 | 133 | } else { |
RoHe | 0:5db5c980e717 | 134 | _isConnectedServer2=socketTCP2.is_connected(); |
RoHe | 0:5db5c980e717 | 135 | pc.printf("Ya se conecto con el servidor 2\n\r"); |
RoHe | 0:5db5c980e717 | 136 | isConServer2=1; |
RoHe | 0:5db5c980e717 | 137 | } |
RoHe | 0:5db5c980e717 | 138 | }//END FOR2 |
RoHe | 0:5db5c980e717 | 139 | |
RoHe | 0:5db5c980e717 | 140 | |
RoHe | 0:5db5c980e717 | 141 | //RECEIVED from TCP SERVER1 |
RoHe | 0:5db5c980e717 | 142 | n1 = socketTCP1.receive_all(buf1, 256); |
RoHe | 0:5db5c980e717 | 143 | buf1[n1] = '\0'; |
RoHe | 0:5db5c980e717 | 144 | if(n1>0) { |
RoHe | 0:5db5c980e717 | 145 | pc.printf("Received message from server1 IF: '%s'\n\r", buf1); |
RoHe | 0:5db5c980e717 | 146 | socketTCP1.close(); |
RoHe | 0:5db5c980e717 | 147 | |
RoHe | 0:5db5c980e717 | 148 | } else { |
RoHe | 0:5db5c980e717 | 149 | //_isConnectedServer1==false; //quiere decir que se desconecto del servidor |
RoHe | 0:5db5c980e717 | 150 | pc.printf("Not received1 ELSE value of lenght: '%u'\n\r",strlen(buf1)); |
RoHe | 0:5db5c980e717 | 151 | socketTCP1.close(); |
RoHe | 0:5db5c980e717 | 152 | //isConnectedServer1=false; |
RoHe | 0:5db5c980e717 | 153 | } |
RoHe | 0:5db5c980e717 | 154 | //socketTCP1.close(); |
RoHe | 0:5db5c980e717 | 155 | //socketTCP2.close(); |
RoHe | 0:5db5c980e717 | 156 | |
RoHe | 0:5db5c980e717 | 157 | if(strcmp(buf1,"reply")==0) { |
RoHe | 0:5db5c980e717 | 158 | // SEND to server1 |
RoHe | 0:5db5c980e717 | 159 | //Entra pero no puede mandar los datos |
RoHe | 0:5db5c980e717 | 160 | // pc.printf("Entro a la funcion strcmp: \n\r"); |
RoHe | 0:5db5c980e717 | 161 | if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) { |
RoHe | 0:5db5c980e717 | 162 | pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1); |
RoHe | 0:5db5c980e717 | 163 | } |
RoHe | 0:5db5c980e717 | 164 | |
RoHe | 0:5db5c980e717 | 165 | char c_myIP[] = "192.168.1.77\n\r"; |
RoHe | 0:5db5c980e717 | 166 | pc.printf("Sending message to Server : %s \n\r",c_myIP); |
RoHe | 0:5db5c980e717 | 167 | socketTCP1.send_all(c_myIP, sizeof(c_myIP) - 1); |
RoHe | 0:5db5c980e717 | 168 | socketTCP1.close(); |
RoHe | 0:5db5c980e717 | 169 | } |
RoHe | 0:5db5c980e717 | 170 | //socketTCP1.close(); |
RoHe | 0:5db5c980e717 | 171 | |
RoHe | 0:5db5c980e717 | 172 | |
RoHe | 0:5db5c980e717 | 173 | |
RoHe | 0:5db5c980e717 | 174 | //RECEIVED from TCP SERVER2 |
RoHe | 0:5db5c980e717 | 175 | n2 = socketTCP2.receive(buf2, 256); |
RoHe | 0:5db5c980e717 | 176 | buf2[n2] = '\0'; |
RoHe | 0:5db5c980e717 | 177 | if(n2>0) { |
RoHe | 0:5db5c980e717 | 178 | pc.printf("Received message from server2 IF: '%s'\n\r", buf2); |
RoHe | 0:5db5c980e717 | 179 | socketTCP2.close(); |
RoHe | 0:5db5c980e717 | 180 | //isConnectedServer1=false; |
RoHe | 0:5db5c980e717 | 181 | } else { |
RoHe | 0:5db5c980e717 | 182 | //_isConnectedServer1==false; //quiere decir que se desconecto del servidor |
RoHe | 0:5db5c980e717 | 183 | pc.printf("Not received2 ELSE value of lenght: '%u'\n\r",strlen(buf2)); |
RoHe | 0:5db5c980e717 | 184 | socketTCP2.close(); |
RoHe | 0:5db5c980e717 | 185 | //isConnectedServer1=false; |
RoHe | 0:5db5c980e717 | 186 | } |
RoHe | 0:5db5c980e717 | 187 | // |
RoHe | 0:5db5c980e717 | 188 | }// END while |
RoHe | 0:5db5c980e717 | 189 | eth.disconnect(); |
RoHe | 0:5db5c980e717 | 190 | |
RoHe | 0:5db5c980e717 | 191 | |
RoHe | 0:5db5c980e717 | 192 | |
RoHe | 0:5db5c980e717 | 193 | } |