This is the client side of our motion detection security scanner system.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Ethernet_UDP_client by
Need to use this program too.
https://developer.mbed.org/users/dereklstinson/code/Motions_Secure_Server_IUPUI/
main.cpp@1:2f91eff1bb80, 2016-12-16 (annotated)
- Committer:
- dereklstinson
- Date:
- Fri Dec 16 01:02:08 2016 +0000
- Revision:
- 1:2f91eff1bb80
- Parent:
- 0:29858159c001
version .01 alpha
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mareikeFSL | 0:29858159c001 | 1 | /*------------------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 2 | /* Ethernet UDP Client (to be used with Ethernet_UDP_server) */ |
mareikeFSL | 0:29858159c001 | 3 | /*------------------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 4 | |
mareikeFSL | 0:29858159c001 | 5 | /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/ |
mareikeFSL | 0:29858159c001 | 6 | /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */ |
mareikeFSL | 0:29858159c001 | 7 | /* */ |
mareikeFSL | 0:29858159c001 | 8 | /*------------------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 9 | /* This "Hello World" program is used in conjunction with the Ethernet_UDP_server */ |
mareikeFSL | 0:29858159c001 | 10 | /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */ |
mareikeFSL | 0:29858159c001 | 11 | /* To use this program, you need to do the following: */ |
mareikeFSL | 0:29858159c001 | 12 | /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */ |
mareikeFSL | 0:29858159c001 | 13 | /* is not required). */ |
mareikeFSL | 0:29858159c001 | 14 | /* - Flash one board with Ethernet_UDP_client and the other with */ |
mareikeFSL | 0:29858159c001 | 15 | /* Ethernet_UDP_server */ |
mareikeFSL | 0:29858159c001 | 16 | /* - [optional] If you would like to see the "Hello World" output on your */ |
mareikeFSL | 0:29858159c001 | 17 | /* monitor, install and open a terminal. Tera Term is used in the Wiki for */ |
mareikeFSL | 0:29858159c001 | 18 | /* this program. */ |
mareikeFSL | 0:29858159c001 | 19 | /*------------------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 20 | |
mareikeFSL | 0:29858159c001 | 21 | |
mareikeFSL | 0:29858159c001 | 22 | /*--INCLUDES----------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 23 | #include "mbed.h" |
mareikeFSL | 0:29858159c001 | 24 | #include "EthernetInterface.h" |
mareikeFSL | 0:29858159c001 | 25 | |
mareikeFSL | 0:29858159c001 | 26 | |
mareikeFSL | 0:29858159c001 | 27 | /*--DEFINES-----------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 28 | |
mareikeFSL | 0:29858159c001 | 29 | |
mareikeFSL | 0:29858159c001 | 30 | |
mareikeFSL | 0:29858159c001 | 31 | /*--CONSTANTS---------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 32 | const int PORT = 7; |
mareikeFSL | 0:29858159c001 | 33 | |
mareikeFSL | 0:29858159c001 | 34 | static const char* SERVER_IP = "192.168.1.101"; //IP of server board |
mareikeFSL | 0:29858159c001 | 35 | static const char* CLIENT_IP = "192.168.1.102"; //IP of client board |
mareikeFSL | 0:29858159c001 | 36 | static const char* MASK = "255.255.255.0"; //mask |
mareikeFSL | 0:29858159c001 | 37 | static const char* GATEWAY = "192.168.1.1"; //gateway |
mareikeFSL | 0:29858159c001 | 38 | |
mareikeFSL | 0:29858159c001 | 39 | |
mareikeFSL | 0:29858159c001 | 40 | /*--INITS-------------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 41 | Serial pc(USBTX, USBRX); //create PC interface |
mareikeFSL | 0:29858159c001 | 42 | EthernetInterface eth; //create ethernet |
mareikeFSL | 0:29858159c001 | 43 | UDPSocket sock; //creat Ethernet socket |
mareikeFSL | 0:29858159c001 | 44 | Endpoint server; //create endpoint |
mareikeFSL | 0:29858159c001 | 45 | |
mareikeFSL | 0:29858159c001 | 46 | DigitalOut red(LED_RED); //debug led |
mareikeFSL | 0:29858159c001 | 47 | DigitalOut green(LED_GREEN); //debug led |
dereklstinson | 1:2f91eff1bb80 | 48 | DigitalIn button(SW3); |
dereklstinson | 1:2f91eff1bb80 | 49 | DigitalIn button2(SW2); |
dereklstinson | 1:2f91eff1bb80 | 50 | DigitalOut led(PTD3); //ARDUINO D12 compareable |
mareikeFSL | 0:29858159c001 | 51 | /*--VARIABLES---------------------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 52 | int n; //size of received message |
mareikeFSL | 0:29858159c001 | 53 | char in_buffer[1]; //create receive buffer |
mareikeFSL | 0:29858159c001 | 54 | char counter[1] = {0}; //sample send buffer |
mareikeFSL | 0:29858159c001 | 55 | |
dereklstinson | 1:2f91eff1bb80 | 56 | Ticker flashes; |
mareikeFSL | 0:29858159c001 | 57 | /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 58 | void init_usb(void); //initializes pc.printf if required |
mareikeFSL | 0:29858159c001 | 59 | void init_eth(void); //initializes Ethernet |
mareikeFSL | 0:29858159c001 | 60 | void end_eth(void); //closes Ethernet socket |
mareikeFSL | 0:29858159c001 | 61 | int main(void); //main |
dereklstinson | 1:2f91eff1bb80 | 62 | void alarmLight(); |
mareikeFSL | 0:29858159c001 | 63 | |
mareikeFSL | 0:29858159c001 | 64 | /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ |
mareikeFSL | 0:29858159c001 | 65 | |
dereklstinson | 1:2f91eff1bb80 | 66 | /***********ALARM************************************************************************/ |
dereklstinson | 1:2f91eff1bb80 | 67 | |
dereklstinson | 1:2f91eff1bb80 | 68 | void alarmLight(){ |
dereklstinson | 1:2f91eff1bb80 | 69 | if (in_buffer[0] =='z' || led ==1){ |
dereklstinson | 1:2f91eff1bb80 | 70 | |
dereklstinson | 1:2f91eff1bb80 | 71 | red=!red; |
dereklstinson | 1:2f91eff1bb80 | 72 | led=!led; |
dereklstinson | 1:2f91eff1bb80 | 73 | |
dereklstinson | 1:2f91eff1bb80 | 74 | |
dereklstinson | 1:2f91eff1bb80 | 75 | |
dereklstinson | 1:2f91eff1bb80 | 76 | } |
dereklstinson | 1:2f91eff1bb80 | 77 | } |
mareikeFSL | 0:29858159c001 | 78 | /*****************************************************************************INIT_USB***/ |
mareikeFSL | 0:29858159c001 | 79 | void init_usb(void) |
mareikeFSL | 0:29858159c001 | 80 | { |
mareikeFSL | 0:29858159c001 | 81 | pc.baud(9600); //baud |
mareikeFSL | 0:29858159c001 | 82 | |
mareikeFSL | 0:29858159c001 | 83 | } //end init_usb() |
mareikeFSL | 0:29858159c001 | 84 | |
mareikeFSL | 0:29858159c001 | 85 | /*****************************************************************************INIT_ETH***/ |
mareikeFSL | 0:29858159c001 | 86 | void init_eth(void) |
mareikeFSL | 0:29858159c001 | 87 | { |
mareikeFSL | 0:29858159c001 | 88 | eth.init(CLIENT_IP, MASK, GATEWAY); //set up IP |
mareikeFSL | 0:29858159c001 | 89 | eth.connect(); //connect ethernet |
mareikeFSL | 0:29858159c001 | 90 | pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress()); //get client IP address |
mareikeFSL | 0:29858159c001 | 91 | |
mareikeFSL | 0:29858159c001 | 92 | sock.init(); //initialize socket |
mareikeFSL | 0:29858159c001 | 93 | |
mareikeFSL | 0:29858159c001 | 94 | server.set_address(SERVER_IP, PORT); //set address of server |
mareikeFSL | 0:29858159c001 | 95 | |
mareikeFSL | 0:29858159c001 | 96 | } //end init_eth() |
mareikeFSL | 0:29858159c001 | 97 | |
mareikeFSL | 0:29858159c001 | 98 | /******************************************************************************END_ETH***/ |
mareikeFSL | 0:29858159c001 | 99 | void end_eth(void) |
mareikeFSL | 0:29858159c001 | 100 | { |
mareikeFSL | 0:29858159c001 | 101 | sock.close(); //close socket |
mareikeFSL | 0:29858159c001 | 102 | eth.disconnect(); //close Ethernet |
mareikeFSL | 0:29858159c001 | 103 | |
mareikeFSL | 0:29858159c001 | 104 | } //end end_eth() |
mareikeFSL | 0:29858159c001 | 105 | |
mareikeFSL | 0:29858159c001 | 106 | /*********************************************************************************MAIN***/ |
mareikeFSL | 0:29858159c001 | 107 | int main(void) |
mareikeFSL | 0:29858159c001 | 108 | { |
mareikeFSL | 0:29858159c001 | 109 | red = 0; //client |
mareikeFSL | 0:29858159c001 | 110 | green = 1; |
dereklstinson | 1:2f91eff1bb80 | 111 | // led = 1; |
mareikeFSL | 0:29858159c001 | 112 | init_usb(); //initialize the PC interface |
mareikeFSL | 0:29858159c001 | 113 | init_eth(); //initialize the Ethernet connection |
dereklstinson | 1:2f91eff1bb80 | 114 | flashes.attach(&alarmLight,.1); |
mareikeFSL | 0:29858159c001 | 115 | while(true) //repeat forever |
dereklstinson | 1:2f91eff1bb80 | 116 | { |
dereklstinson | 1:2f91eff1bb80 | 117 | if(button2==0) counter[0]='x'; |
dereklstinson | 1:2f91eff1bb80 | 118 | if(button==0) counter[0]='b'; |
mareikeFSL | 0:29858159c001 | 119 | |
dereklstinson | 1:2f91eff1bb80 | 120 | pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP); //print message to send |
dereklstinson | 1:2f91eff1bb80 | 121 | sock.sendTo(server, counter, sizeof(counter)); |
dereklstinson | 1:2f91eff1bb80 | 122 | |
mareikeFSL | 0:29858159c001 | 123 | pc.printf("CLIENT - Waiting for UDP packet...\r\n"); //wait for message |
mareikeFSL | 0:29858159c001 | 124 | |
mareikeFSL | 0:29858159c001 | 125 | n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer)); //receive message from server |
mareikeFSL | 0:29858159c001 | 126 | |
mareikeFSL | 0:29858159c001 | 127 | in_buffer[n] = '\0'; //add \0 to end of message |
mareikeFSL | 0:29858159c001 | 128 | pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP); //print message received |
dereklstinson | 1:2f91eff1bb80 | 129 | |
dereklstinson | 1:2f91eff1bb80 | 130 | alarmLight(); |
dereklstinson | 1:2f91eff1bb80 | 131 | |
dereklstinson | 1:2f91eff1bb80 | 132 | |
mareikeFSL | 0:29858159c001 | 133 | counter[0] = (counter[0] + 1)%11; //only count up to 10, then reset to 0 |
mareikeFSL | 0:29858159c001 | 134 | |
dereklstinson | 1:2f91eff1bb80 | 135 | // wait(1); |
dereklstinson | 1:2f91eff1bb80 | 136 | //wait 1 second |
mareikeFSL | 0:29858159c001 | 137 | } |
mareikeFSL | 0:29858159c001 | 138 | |
mareikeFSL | 0:29858159c001 | 139 | } //end main() |