DSOS Sensors
Dependencies: EthernetInterface SensorsTointegrate mbed-rtos mbed
Fork of MainTaskGus by
Diff: main.cpp
- Revision:
- 0:5db5c980e717
- Child:
- 1:f3619e6e9e89
diff -r 000000000000 -r 5db5c980e717 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 05 17:46:07 2015 +0000 @@ -0,0 +1,193 @@ +/* + +Main.cpp + +Last update by RoHe 16/11/2015 + +*/ +#include <string.h> +#include "mbed.h" +#include "EthernetInterface.h" +#include "rtos.h" +#include "SensorPIR.h" +#include "Ether.h" +#include "Initial.h" +#include "Threads.cpp" +#include "StringManager.h" + + +int main() +{ + /*//////////////////////////////////// + Initialization of the outports + + Comments: + -Inicialize all the output in zero + -It has Pull - up + */ + ledRED=1; // it has pull-up + ledGREEN=1; + ledBLUE=1; + + + /*//////////////////////////////////// + SET Initial configuration and welcome message + */ + pc.baud(115200); + pc.printf("Hello World from TCP Client-Server RoHE Main TASK GUS integrate YORCH1.\n\r"); + + + /*////////////////////////////////// + Declares all necessary CLASSES + */ + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + + /*/////////////////////////////////// + Declares all necessary THREADS + */ + Thread thread1(led_thread); + //Thread thread2(receivedFromServer1); + + /*//////////////////////////////////// + Declares all necessary INTERRUPTIONS + */ + sw2.fall(&sw2_press); + +//////////////////////////////////////////////////////APPLICATION ///////////////////////////////////////////////////////// + + + s_myIP = eth.getIPAddress(); + pc.printf("\nClient IP Address is %s\r", s_myIP); + pc.printf("\nClient IP MAC address is %s\r", eth.getMACAddress); + pc.printf("\nClient IP Gateway is %s\r", eth.getGateway()); + pc.printf("\nClient IP network mask is %s\n\r", eth.getNetworkMask()); + /* + CONSTRAINS: + -No se puede poner un serial print el los threads .... + -se tiene que abrir y cerrar las conecciones par que pueda leer nuevos mensajes + -se puede leer mensajes si no se cierra el socket pero la conexion se queda abierta + y no puede recibir mas mensajes .. se saco el forpara hacerlo una sola vez y no se cierra la conexion + + */ + + + /* + // Connect to Server2 + (socketTCP.connect(ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2) < 0) { + pc.printf("Unable to connect to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2); + } + _isConnectedServer2=true; + pc.printf("Connected to Server at %s\n\r",ECHO_SERVER_ADDRESS2); + */ + + + while(true) { + + // Connect to Server1 + //TODO: try to connect to both servers + //TODO: only try 5 times after ti server1 goes down + //TODO: cual es la diferencia entre recieved_all y recieved + //TOTRY: que valor regresa cuando hace el connect??? + + //QUESTION: que son los numero que aparecen despues de los dos puntos, + //parece ser uqe es el numero que noceciones que lleva acabo 192.168.1.81:49160 + //NOSIRVE: si se conecta los dos micros con el programa de TCP Server4 y se les pide un reply + //los micros no contestan y para terminarla si pides dos veces un reply se queda colgado .. + //ACCOMPLISH: los colores de los leds ya responen de acuerdo a la conexion + + //GetPersonStatus(1); + + for(int i=0,isConServer1=0; i<2 && isConServer1==0; i++) { + if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) { + _isConnectedServer1=false; + pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1); + pc.printf("TRY SERVER 1 number (%d)\n\r",(i+1)); + isConServer1=0; + } else { + _isConnectedServer1=socketTCP1.is_connected(); + pc.printf("Ya se conecto con el servidor 1\n\r"); + isConServer1=1; + wait(1); + + char hello[] = "Hello World\n\r"; + printf("Sending message to Server : '%s' \n",hello); + socketTCP1.send_all(hello, sizeof(hello) - 1); + + + + + } + }//END FOR1 + + for(int i=0,isConServer2=0; i<2 && isConServer2==0; i++) { + if(socketTCP2.connect(ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2) == -1) { + _isConnectedServer2=false; + pc.printf("Unable to connect2 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS2, ECHO_SERVER_PORT2); + pc.printf("TRY SERVER 2 number (%d)\n\r",(i+1)); + isConServer2=0; + //wait(1); + } else { + _isConnectedServer2=socketTCP2.is_connected(); + pc.printf("Ya se conecto con el servidor 2\n\r"); + isConServer2=1; + } + }//END FOR2 + + + //RECEIVED from TCP SERVER1 + n1 = socketTCP1.receive_all(buf1, 256); + buf1[n1] = '\0'; + if(n1>0) { + pc.printf("Received message from server1 IF: '%s'\n\r", buf1); + socketTCP1.close(); + + } else { + //_isConnectedServer1==false; //quiere decir que se desconecto del servidor + pc.printf("Not received1 ELSE value of lenght: '%u'\n\r",strlen(buf1)); + socketTCP1.close(); + //isConnectedServer1=false; + } + //socketTCP1.close(); + //socketTCP2.close(); + + if(strcmp(buf1,"reply")==0) { + // SEND to server1 + //Entra pero no puede mandar los datos + // pc.printf("Entro a la funcion strcmp: \n\r"); + if(socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) == -1) { + pc.printf("Unable to connect1 to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1); + } + + char c_myIP[] = "192.168.1.77\n\r"; + pc.printf("Sending message to Server : %s \n\r",c_myIP); + socketTCP1.send_all(c_myIP, sizeof(c_myIP) - 1); + socketTCP1.close(); + } + //socketTCP1.close(); + + + + //RECEIVED from TCP SERVER2 + n2 = socketTCP2.receive(buf2, 256); + buf2[n2] = '\0'; + if(n2>0) { + pc.printf("Received message from server2 IF: '%s'\n\r", buf2); + socketTCP2.close(); + //isConnectedServer1=false; + } else { + //_isConnectedServer1==false; //quiere decir que se desconecto del servidor + pc.printf("Not received2 ELSE value of lenght: '%u'\n\r",strlen(buf2)); + socketTCP2.close(); + //isConnectedServer1=false; + } + // + }// END while + eth.disconnect(); + + + +}