Derek Stinson / Mbed 2 deprecated Motion_Secure_Client_IUPUI

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of Ethernet_UDP_client by NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*------------------------------------------------------------------------------------*/
00002 /*  Ethernet UDP Client (to be used with Ethernet_UDP_server)                         */
00003 /*------------------------------------------------------------------------------------*/
00004 
00005 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
00006 /*  NXP         mareikeFSL  2015.12.23      rev 1.0     initial                       */
00007 /*                                                                                    */
00008 /*------------------------------------------------------------------------------------*/
00009 /*  This "Hello World" program is used in conjunction with the Ethernet_UDP_server    */
00010 /*  program. It communicates between two FRDM-K64F boards via the Ethernet protocol.  */
00011 /*  To use this program, you need to do the following:                                */
00012 /*      - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable   */
00013 /*        is not required).                                                           */
00014 /*      - Flash one board with Ethernet_UDP_client and the other with                 */
00015 /*        Ethernet_UDP_server                                                         */
00016 /*      - [optional] If you would like to see the "Hello World" output on your        */
00017 /*        monitor, install and open a terminal. Tera Term is used in the Wiki for     */
00018 /*        this program.                                                               */
00019 /*------------------------------------------------------------------------------------*/
00020 
00021 
00022 /*--INCLUDES----------------------------------------------------------------------------*/
00023 #include "mbed.h"
00024 #include "EthernetInterface.h"
00025  
00026 
00027 /*--DEFINES-----------------------------------------------------------------------------*/
00028 
00029 
00030 
00031 /*--CONSTANTS---------------------------------------------------------------------------*/
00032 const int PORT = 7;
00033 
00034 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
00035 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
00036 static const char* MASK = "255.255.255.0";      //mask
00037 static const char* GATEWAY = "192.168.1.1";     //gateway
00038 
00039 
00040 /*--INITS-------------------------------------------------------------------------------*/
00041 Serial pc(USBTX, USBRX);        //create PC interface
00042 EthernetInterface eth;          //create ethernet
00043 UDPSocket sock;                 //creat Ethernet socket
00044 Endpoint server;                //create endpoint
00045 
00046 DigitalOut red(LED_RED);        //debug led
00047 DigitalOut green(LED_GREEN);    //debug led
00048 DigitalIn button(SW3);
00049 DigitalIn button2(SW2);
00050 DigitalOut led(PTD3); //ARDUINO D12 compareable
00051 /*--VARIABLES---------------------------------------------------------------------------*/
00052 int n;                  //size of received message
00053 char in_buffer[1];      //create receive buffer
00054 char counter[1] = {0};  //sample send buffer
00055 
00056 Ticker flashes;
00057 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
00058 void init_usb(void);    //initializes pc.printf if required
00059 void init_eth(void);    //initializes Ethernet
00060 void end_eth(void);     //closes Ethernet socket
00061 int main(void);         //main
00062 void alarmLight();
00063 
00064 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
00065 
00066 /***********ALARM************************************************************************/
00067 
00068 void alarmLight(){
00069   if (in_buffer[0] =='z' || led ==1){
00070   
00071             red=!red;
00072             led=!led;
00073           
00074         
00075  
00076 }
00077 }
00078 /*****************************************************************************INIT_USB***/
00079 void init_usb(void)
00080 {
00081     pc.baud(9600);    //baud
00082     
00083 }   //end init_usb()
00084 
00085 /*****************************************************************************INIT_ETH***/
00086 void init_eth(void)
00087 {
00088     eth.init(CLIENT_IP, MASK, GATEWAY);                                         //set up IP
00089     eth.connect();                                                              //connect ethernet
00090     pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress());    //get client IP address
00091     
00092     sock.init();                                                                //initialize socket
00093     
00094     server.set_address(SERVER_IP, PORT);                                        //set address of server
00095     
00096 }   //end init_eth()
00097 
00098 /******************************************************************************END_ETH***/
00099 void end_eth(void)
00100 {
00101     sock.close();       //close socket
00102     eth.disconnect();   //close Ethernet
00103     
00104 }   //end end_eth()
00105 
00106 /*********************************************************************************MAIN***/
00107 int main(void)
00108 {
00109     red = 0;                                                                                //client
00110     green = 1;
00111    // led = 1;
00112     init_usb();                                                                             //initialize the PC interface
00113     init_eth();                                                                             //initialize the Ethernet connection
00114     flashes.attach(&alarmLight,.1);
00115     while(true)                                                                             //repeat forever
00116     { 
00117     if(button2==0) counter[0]='x';
00118     if(button==0) counter[0]='b';
00119         
00120     pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP);       //print message to send
00121     sock.sendTo(server, counter, sizeof(counter)); 
00122        
00123         pc.printf("CLIENT - Waiting for UDP packet...\r\n");                                //wait for message
00124         
00125         n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer));                         //receive message from server
00126         
00127         in_buffer[n] = '\0';                                                                //add \0 to end of message
00128         pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP);    //print message received
00129 
00130             alarmLight();
00131          
00132             
00133         counter[0] = (counter[0] + 1)%11;                                                   //only count up to 10, then reset to 0
00134         
00135      //   wait(1);    
00136                                                                                 //wait 1 second
00137     }
00138     
00139 }   //end main()