w5Ethernetserver

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of exe5_3_Ethernet_UDP_server by Jack Occleshaw

Files at this revision

API Documentation at this revision

Comitter:
steveshun
Date:
Thu Dec 01 14:12:26 2016 +0000
Commit message:
Exercise 5: Ethernet

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Dec 01 14:12:26 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#4d7bff17a592
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 01 14:12:26 2016 +0000
@@ -0,0 +1,108 @@
+/*------------------------------------------------------------------------------------*/
+/*  Ethernet UDP Server (to be used with Ethernet_UDP_client)                         */
+/*------------------------------------------------------------------------------------*/
+
+/*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
+/*  NXP         mareikeFSL  2015.12.23      rev 1.0     initial                       */
+/*                                                                                    */
+/*------------------------------------------------------------------------------------*/
+/*  This "Hello World" program is used in conjunction with the Ethernet_UDP_client    */
+/*  program. It communicates between two FRDM-K64F boards via the Ethernet protocol.  */
+/*  To use this program, you need to do the following:                                */
+/*      - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable   */
+/*        is not required).                                                           */
+/*      - Flash one board with Ethernet_UDP_client and the other with                 */
+/*        Ethernet_UDP_server                                                         */
+/*      - [optional] If you would like to see the "Hello World" output on your        */
+/*        monitor, install and open a terminal. Tera Term is used in the Wiki for     */
+/*        this program.                                                               */
+/*------------------------------------------------------------------------------------*/
+
+
+/*--INCLUDES----------------------------------------------------------------------------*/
+#include "mbed.h"
+#include "EthernetInterface.h"
+ 
+
+/*--DEFINES-----------------------------------------------------------------------------*/
+
+
+
+/*--CONSTANTS---------------------------------------------------------------------------*/
+const int PORT = 7;                             //arbitrary port
+
+static const char* SERVER_IP = "192.168.1.101"; //IP of server board
+static const char* MASK = "255.255.255.0";      //mask
+static const char* GATEWAY = "192.168.1.1";     //gateway
+
+
+/*--INITS-------------------------------------------------------------------------------*/
+Serial pc(USBTX, USBRX);        //create PC interface
+EthernetInterface eth;          //create ethernet
+UDPSocket server;               //creat server
+Endpoint client;                //create endpoint
+
+DigitalOut red(LED_RED);        //debug led
+DigitalOut green(LED_GREEN);    //debug led
+
+
+/*--VARIABLES---------------------------------------------------------------------------*/
+int n;                  //size of received message
+char counter[1] = {0};  //sample receive/send buffer
+
+
+/*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
+void init_usb(void);    //initializes pc.printf if required
+void init_eth(void);    //initializes Ethernet
+void receive(void);     //receives packets
+int main(void);         //main
+
+
+/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
+
+/*****************************************************************************INIT_USB***/
+void init_usb(void)
+{
+    pc.baud(9600);    //baud
+    
+}   //end init_usb()
+
+/*****************************************************************************INIT_ETH***/
+void init_eth(void)
+{
+    eth.init(SERVER_IP, MASK, GATEWAY);                                         //set up IP
+    eth.connect();                                                              //connect ethernet
+    pc.printf("\nSERVER - Server IP Address is %s\r\n", eth.getIPAddress());    //get server IP address;
+    
+    server.bind(PORT);                                                          //bind server
+        
+}   //end init_eth()
+
+/******************************************************************************RECEIVE***/
+void receive(void)
+{
+    pc.printf("\nSERVER - Waiting for UDP packet...\r\n");                                      //wait for packet
+    n = server.receiveFrom(client, counter, sizeof(counter));                                   //receive message from client
+    counter[n] = '\0';                                                                          //add \0 to end of message
+    
+    pc.printf("SERVER - Received '%i' from client %s\r\n", counter[0], client.get_address());   //print message and client
+    pc.printf("SERVER - Sending '%i' back to client %s\r\n", counter[0], client.get_address()); //print sending back
+    server.sendTo(client, counter, n);                                                          //send message
+
+}   //end receive()
+
+/*********************************************************************************MAIN***/
+int main(void)
+{
+    red = 1;
+    green = 0;      //server
+    
+    init_usb();     //initialize the PC interface
+    init_eth();     //initialize the Ethernet connection
+
+    while (true)    //repeat forever
+    {
+        receive();  //wait for message
+    }
+
+}   //end main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Dec 01 14:12:26 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Dec 01 14:12:26 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file