pro_EthServer

Dependencies:   C12832 EthernetInterface LM75B mbed-rtos mbed

Fork of exe5_3_Ethernet_UDP_server by Zhengguo Sheng

Committer:
occle
Date:
Thu Jan 12 15:49:42 2017 +0000
Revision:
1:e29e7d1b1929
Parent:
0:86b7ebac3e60
pro_EthServer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
steveshun 0:86b7ebac3e60 1 /*------------------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 2 /* Ethernet UDP Server (to be used with Ethernet_UDP_client) */
steveshun 0:86b7ebac3e60 3 /*------------------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 4
steveshun 0:86b7ebac3e60 5 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
steveshun 0:86b7ebac3e60 6 /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */
steveshun 0:86b7ebac3e60 7 /* */
steveshun 0:86b7ebac3e60 8 /*------------------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 9 /* This "Hello World" program is used in conjunction with the Ethernet_UDP_client */
steveshun 0:86b7ebac3e60 10 /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */
steveshun 0:86b7ebac3e60 11 /* To use this program, you need to do the following: */
steveshun 0:86b7ebac3e60 12 /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */
steveshun 0:86b7ebac3e60 13 /* is not required). */
steveshun 0:86b7ebac3e60 14 /* - Flash one board with Ethernet_UDP_client and the other with */
steveshun 0:86b7ebac3e60 15 /* Ethernet_UDP_server */
steveshun 0:86b7ebac3e60 16 /* - [optional] If you would like to see the "Hello World" output on your */
steveshun 0:86b7ebac3e60 17 /* monitor, install and open a terminal. Tera Term is used in the Wiki for */
steveshun 0:86b7ebac3e60 18 /* this program. */
steveshun 0:86b7ebac3e60 19 /*------------------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 20
steveshun 0:86b7ebac3e60 21
steveshun 0:86b7ebac3e60 22 /*--INCLUDES----------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 23 #include "mbed.h"
steveshun 0:86b7ebac3e60 24 #include "EthernetInterface.h"
occle 1:e29e7d1b1929 25 #include "LM75B.h"
occle 1:e29e7d1b1929 26 #include "C12832.h"
steveshun 0:86b7ebac3e60 27 /*--DEFINES-----------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 28
steveshun 0:86b7ebac3e60 29
steveshun 0:86b7ebac3e60 30
steveshun 0:86b7ebac3e60 31 /*--CONSTANTS---------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 32 const int PORT = 7; //arbitrary port
steveshun 0:86b7ebac3e60 33
steveshun 0:86b7ebac3e60 34 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
steveshun 0:86b7ebac3e60 35 static const char* MASK = "255.255.255.0"; //mask
steveshun 0:86b7ebac3e60 36 static const char* GATEWAY = "192.168.1.1"; //gateway
occle 1:e29e7d1b1929 37 static const char* BOILER_IP = "192.168.1.102"; //Boiler device
occle 1:e29e7d1b1929 38 static const char* MASTER_IP = "192.168.1.110"; //Master device
steveshun 0:86b7ebac3e60 39
steveshun 0:86b7ebac3e60 40
steveshun 0:86b7ebac3e60 41 /*--INITS-------------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 42 Serial pc(USBTX, USBRX); //create PC interface
steveshun 0:86b7ebac3e60 43 EthernetInterface eth; //create ethernet
occle 1:e29e7d1b1929 44 UDPSocket server; //create server
steveshun 0:86b7ebac3e60 45 Endpoint client; //create endpoint
steveshun 0:86b7ebac3e60 46
steveshun 0:86b7ebac3e60 47 DigitalOut red(LED_RED); //debug led
steveshun 0:86b7ebac3e60 48 DigitalOut green(LED_GREEN); //debug led
steveshun 0:86b7ebac3e60 49
occle 1:e29e7d1b1929 50 LM75B sensor(D14,D15); //temp sensor
occle 1:e29e7d1b1929 51 C12832 lcd(D11, D13, D12, D7, D10);//LCD screen arduino notation
steveshun 0:86b7ebac3e60 52
steveshun 0:86b7ebac3e60 53 /*--VARIABLES---------------------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 54 int n; //size of received message
occle 1:e29e7d1b1929 55 int idealtemp; //ideal room temp
steveshun 0:86b7ebac3e60 56 char counter[1] = {0}; //sample receive/send buffer
steveshun 0:86b7ebac3e60 57
steveshun 0:86b7ebac3e60 58
steveshun 0:86b7ebac3e60 59 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 60 void init_usb(void); //initializes pc.printf if required
steveshun 0:86b7ebac3e60 61 void init_eth(void); //initializes Ethernet
steveshun 0:86b7ebac3e60 62 void receive(void); //receives packets
steveshun 0:86b7ebac3e60 63 int main(void); //main
steveshun 0:86b7ebac3e60 64
steveshun 0:86b7ebac3e60 65
steveshun 0:86b7ebac3e60 66 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
steveshun 0:86b7ebac3e60 67
steveshun 0:86b7ebac3e60 68 /*****************************************************************************INIT_USB***/
steveshun 0:86b7ebac3e60 69 void init_usb(void)
steveshun 0:86b7ebac3e60 70 {
steveshun 0:86b7ebac3e60 71 pc.baud(9600); //baud
steveshun 0:86b7ebac3e60 72
steveshun 0:86b7ebac3e60 73 } //end init_usb()
steveshun 0:86b7ebac3e60 74
steveshun 0:86b7ebac3e60 75 /*****************************************************************************INIT_ETH***/
steveshun 0:86b7ebac3e60 76 void init_eth(void)
steveshun 0:86b7ebac3e60 77 {
steveshun 0:86b7ebac3e60 78 eth.init(SERVER_IP, MASK, GATEWAY); //set up IP
steveshun 0:86b7ebac3e60 79 eth.connect(); //connect ethernet
steveshun 0:86b7ebac3e60 80 pc.printf("\nSERVER - Server IP Address is %s\r\n", eth.getIPAddress()); //get server IP address;
steveshun 0:86b7ebac3e60 81
steveshun 0:86b7ebac3e60 82 server.bind(PORT); //bind server
steveshun 0:86b7ebac3e60 83
steveshun 0:86b7ebac3e60 84 } //end init_eth()
steveshun 0:86b7ebac3e60 85
steveshun 0:86b7ebac3e60 86 /******************************************************************************RECEIVE***/
steveshun 0:86b7ebac3e60 87 void receive(void)
steveshun 0:86b7ebac3e60 88 {
steveshun 0:86b7ebac3e60 89 pc.printf("\nSERVER - Waiting for UDP packet...\r\n"); //wait for packet
steveshun 0:86b7ebac3e60 90 n = server.receiveFrom(client, counter, sizeof(counter)); //receive message from client
steveshun 0:86b7ebac3e60 91 counter[n] = '\0'; //add \0 to end of message
occle 1:e29e7d1b1929 92
occle 1:e29e7d1b1929 93 if(client.get_address()==BOILER_IP) //if address is boiler device
occle 1:e29e7d1b1929 94 {
occle 1:e29e7d1b1929 95 if(sensor.read() < idealtemp)
occle 1:e29e7d1b1929 96 {
occle 1:e29e7d1b1929 97 counter[0] = 0; //reset counter if temp is below ideal
occle 1:e29e7d1b1929 98 counter[1] = '/0';
occle 1:e29e7d1b1929 99 }
occle 1:e29e7d1b1929 100 }
occle 1:e29e7d1b1929 101 if(client.get_address()==MASTER_IP) //if address is boiler device
occle 1:e29e7d1b1929 102 {
occle 1:e29e7d1b1929 103 idealtemp = counter[0]; //new ideal temp is from master device
occle 1:e29e7d1b1929 104 counter[0] = (int)sensor.read(); //send sensor temp to master
occle 1:e29e7d1b1929 105 }
occle 1:e29e7d1b1929 106
steveshun 0:86b7ebac3e60 107
steveshun 0:86b7ebac3e60 108 pc.printf("SERVER - Received '%i' from client %s\r\n", counter[0], client.get_address()); //print message and client
steveshun 0:86b7ebac3e60 109 pc.printf("SERVER - Sending '%i' back to client %s\r\n", counter[0], client.get_address()); //print sending back
steveshun 0:86b7ebac3e60 110 server.sendTo(client, counter, n); //send message
steveshun 0:86b7ebac3e60 111
steveshun 0:86b7ebac3e60 112 } //end receive()
steveshun 0:86b7ebac3e60 113
steveshun 0:86b7ebac3e60 114 /*********************************************************************************MAIN***/
steveshun 0:86b7ebac3e60 115 int main(void)
steveshun 0:86b7ebac3e60 116 {
occle 1:e29e7d1b1929 117 int j=0;
occle 1:e29e7d1b1929 118 lcd.cls();
occle 1:e29e7d1b1929 119 lcd.locate(0,3);
occle 1:e29e7d1b1929 120 lcd.printf("mbed application shield!");
occle 1:e29e7d1b1929 121
steveshun 0:86b7ebac3e60 122 red = 1;
steveshun 0:86b7ebac3e60 123 green = 0; //server
steveshun 0:86b7ebac3e60 124
steveshun 0:86b7ebac3e60 125 init_usb(); //initialize the PC interface
steveshun 0:86b7ebac3e60 126 init_eth(); //initialize the Ethernet connection
steveshun 0:86b7ebac3e60 127
steveshun 0:86b7ebac3e60 128 while (true) //repeat forever
steveshun 0:86b7ebac3e60 129 {
occle 1:e29e7d1b1929 130 lcd.locate(0,15);
occle 1:e29e7d1b1929 131 lcd.printf("Counting : %d",j);
occle 1:e29e7d1b1929 132 j++;
occle 1:e29e7d1b1929 133 wait(1.0);
steveshun 0:86b7ebac3e60 134 receive(); //wait for message
steveshun 0:86b7ebac3e60 135 }
steveshun 0:86b7ebac3e60 136
steveshun 0:86b7ebac3e60 137 } //end main()