pro_EthBoiler

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of exe5_3_Ethernet_UDP_client by Zhengguo Sheng

Committer:
occle
Date:
Thu Jan 12 15:50:18 2017 +0000
Revision:
1:f117ec229280
Parent:
0:3527566e031c
pro_EthBoiler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
steveshun 0:3527566e031c 1 /*------------------------------------------------------------------------------------*/
steveshun 0:3527566e031c 2 /* Ethernet UDP Client (to be used with Ethernet_UDP_server) */
steveshun 0:3527566e031c 3 /*------------------------------------------------------------------------------------*/
steveshun 0:3527566e031c 4
steveshun 0:3527566e031c 5 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
steveshun 0:3527566e031c 6 /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */
steveshun 0:3527566e031c 7 /* */
steveshun 0:3527566e031c 8 /*------------------------------------------------------------------------------------*/
steveshun 0:3527566e031c 9 /* This "Hello World" program is used in conjunction with the Ethernet_UDP_server */
steveshun 0:3527566e031c 10 /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */
steveshun 0:3527566e031c 11 /* To use this program, you need to do the following: */
steveshun 0:3527566e031c 12 /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */
steveshun 0:3527566e031c 13 /* is not required). */
steveshun 0:3527566e031c 14 /* - Flash one board with Ethernet_UDP_client and the other with */
steveshun 0:3527566e031c 15 /* Ethernet_UDP_server */
steveshun 0:3527566e031c 16 /* - [optional] If you would like to see the "Hello World" output on your */
steveshun 0:3527566e031c 17 /* monitor, install and open a terminal. Tera Term is used in the Wiki for */
steveshun 0:3527566e031c 18 /* this program. */
steveshun 0:3527566e031c 19 /*------------------------------------------------------------------------------------*/
steveshun 0:3527566e031c 20
steveshun 0:3527566e031c 21
steveshun 0:3527566e031c 22 /*--INCLUDES----------------------------------------------------------------------------*/
steveshun 0:3527566e031c 23 #include "mbed.h"
steveshun 0:3527566e031c 24 #include "EthernetInterface.h"
steveshun 0:3527566e031c 25
steveshun 0:3527566e031c 26
steveshun 0:3527566e031c 27 /*--DEFINES-----------------------------------------------------------------------------*/
steveshun 0:3527566e031c 28
steveshun 0:3527566e031c 29
steveshun 0:3527566e031c 30
steveshun 0:3527566e031c 31 /*--CONSTANTS---------------------------------------------------------------------------*/
steveshun 0:3527566e031c 32 const int PORT = 7;
steveshun 0:3527566e031c 33
steveshun 0:3527566e031c 34 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
steveshun 0:3527566e031c 35 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
steveshun 0:3527566e031c 36 static const char* MASK = "255.255.255.0"; //mask
steveshun 0:3527566e031c 37 static const char* GATEWAY = "192.168.1.1"; //gateway
steveshun 0:3527566e031c 38
steveshun 0:3527566e031c 39
steveshun 0:3527566e031c 40 /*--INITS-------------------------------------------------------------------------------*/
steveshun 0:3527566e031c 41 Serial pc(USBTX, USBRX); //create PC interface
steveshun 0:3527566e031c 42 EthernetInterface eth; //create ethernet
steveshun 0:3527566e031c 43 UDPSocket sock; //creat Ethernet socket
steveshun 0:3527566e031c 44 Endpoint server; //create endpoint
steveshun 0:3527566e031c 45
steveshun 0:3527566e031c 46 DigitalOut red(LED_RED); //debug led
steveshun 0:3527566e031c 47 DigitalOut green(LED_GREEN); //debug led
steveshun 0:3527566e031c 48
steveshun 0:3527566e031c 49
occle 1:f117ec229280 50
occle 1:f117ec229280 51
steveshun 0:3527566e031c 52 /*--VARIABLES---------------------------------------------------------------------------*/
steveshun 0:3527566e031c 53 int n; //size of received message
steveshun 0:3527566e031c 54 char in_buffer[1]; //create receive buffer
steveshun 0:3527566e031c 55 char counter[1] = {0}; //sample send buffer
steveshun 0:3527566e031c 56
steveshun 0:3527566e031c 57
steveshun 0:3527566e031c 58 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
steveshun 0:3527566e031c 59 void init_usb(void); //initializes pc.printf if required
steveshun 0:3527566e031c 60 void init_eth(void); //initializes Ethernet
steveshun 0:3527566e031c 61 void end_eth(void); //closes Ethernet socket
steveshun 0:3527566e031c 62 int main(void); //main
steveshun 0:3527566e031c 63
steveshun 0:3527566e031c 64
steveshun 0:3527566e031c 65 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
steveshun 0:3527566e031c 66
steveshun 0:3527566e031c 67 /*****************************************************************************INIT_USB***/
steveshun 0:3527566e031c 68 void init_usb(void)
steveshun 0:3527566e031c 69 {
steveshun 0:3527566e031c 70 pc.baud(9600); //baud
steveshun 0:3527566e031c 71
steveshun 0:3527566e031c 72 } //end init_usb()
steveshun 0:3527566e031c 73
steveshun 0:3527566e031c 74 /*****************************************************************************INIT_ETH***/
steveshun 0:3527566e031c 75 void init_eth(void)
steveshun 0:3527566e031c 76 {
steveshun 0:3527566e031c 77 eth.init(CLIENT_IP, MASK, GATEWAY); //set up IP
steveshun 0:3527566e031c 78 eth.connect(); //connect ethernet
steveshun 0:3527566e031c 79 pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress()); //get client IP address
steveshun 0:3527566e031c 80
steveshun 0:3527566e031c 81 sock.init(); //initialize socket
steveshun 0:3527566e031c 82
steveshun 0:3527566e031c 83 server.set_address(SERVER_IP, PORT); //set address of server
steveshun 0:3527566e031c 84
steveshun 0:3527566e031c 85 } //end init_eth()
steveshun 0:3527566e031c 86
steveshun 0:3527566e031c 87 /******************************************************************************END_ETH***/
steveshun 0:3527566e031c 88 void end_eth(void)
steveshun 0:3527566e031c 89 {
steveshun 0:3527566e031c 90 sock.close(); //close socket
steveshun 0:3527566e031c 91 eth.disconnect(); //close Ethernet
steveshun 0:3527566e031c 92
steveshun 0:3527566e031c 93 } //end end_eth()
steveshun 0:3527566e031c 94
steveshun 0:3527566e031c 95 /*********************************************************************************MAIN***/
steveshun 0:3527566e031c 96 int main(void)
steveshun 0:3527566e031c 97 {
steveshun 0:3527566e031c 98 red = 0; //client
steveshun 0:3527566e031c 99 green = 1;
steveshun 0:3527566e031c 100
steveshun 0:3527566e031c 101 init_usb(); //initialize the PC interface
steveshun 0:3527566e031c 102 init_eth(); //initialize the Ethernet connection
steveshun 0:3527566e031c 103
steveshun 0:3527566e031c 104 while(true) //repeat forever
steveshun 0:3527566e031c 105 {
occle 1:f117ec229280 106 counter[0] = (counter[0] + 1)%11; //only count up to 10, then reset to 0
steveshun 0:3527566e031c 107 pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP); //print message to send
steveshun 0:3527566e031c 108 sock.sendTo(server, counter, sizeof(counter)); //send message
steveshun 0:3527566e031c 109
steveshun 0:3527566e031c 110 pc.printf("CLIENT - Waiting for UDP packet...\r\n"); //wait for message
steveshun 0:3527566e031c 111
steveshun 0:3527566e031c 112 n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer)); //receive message from server
steveshun 0:3527566e031c 113
steveshun 0:3527566e031c 114 in_buffer[n] = '\0'; //add \0 to end of message
steveshun 0:3527566e031c 115 pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP); //print message received
occle 1:f117ec229280 116 if(in_buffer[0] ==0)
occle 1:f117ec229280 117 {
occle 1:f117ec229280 118 //pwmout to turn on boiler if the message received is 0
occle 1:f117ec229280 119 }
occle 1:f117ec229280 120 else
occle 1:f117ec229280 121 //turn off boiler
steveshun 0:3527566e031c 122
occle 1:f117ec229280 123 wait(30); //wait 30 seconds
steveshun 0:3527566e031c 124 }
steveshun 0:3527566e031c 125
steveshun 0:3527566e031c 126 } //end main()