Projekt na płytce k64f
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Ethernet_UDP_server by
Revision 1:bc203220ef47, committed 2018-09-15
- Comitter:
- majster
- Date:
- Sat Sep 15 19:04:52 2018 +0000
- Parent:
- 0:9108b822f763
- Commit message:
- projekt
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Dec 23 21:02:48 2015 +0000 +++ b/main.cpp Sat Sep 15 19:04:52 2018 +0000 @@ -1,108 +1,149 @@ -/*------------------------------------------------------------------------------------*/ -/* 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-----------------------------------------------------------------------------*/ - +const int PORT = 80; //port -/*--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* SERVER_IP = "192.168.1.101"; //IP serwera +static const char* MASK = "255.255.255.0"; //maska 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 + +Serial pc(USBTX, USBRX); //PC interface -DigitalOut red(LED_RED); //debug led -DigitalOut green(LED_GREEN); //debug led +DigitalOut RedLed(LED1); +//DigitalOut GreenLed(LED2); +DigitalOut BlueLed(LED3); +InterruptIn Switch1(SW2); // przycisk lewo +InterruptIn Switch2(SW3); // przycisk od prawy -/*--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 +void init_usb(void); //init usb +void init_eth(void); //init Ethernet - -/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ - -/*****************************************************************************INIT_USB***/ void init_usb(void) { - pc.baud(9600); //baud + //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) +void blink() { - 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 + RedLed=!RedLed; - 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 +} +void blink1() +{ + BlueLed=!BlueLed; -} //end receive() - -/*********************************************************************************MAIN***/ +} int main(void) { - red = 1; - green = 0; //server + char c; + // int licznik; + // licznik=0; + // int a; + // int b; + //int d[2]={a,b}; + + //init_usb(); //init usb + + EthernetInterface eth; + eth.init(SERVER_IP, MASK, GATEWAY); + eth.connect(); + + pc.printf("\nIP Address is %s\n\r", eth.getIPAddress()); - init_usb(); //initialize the PC interface - init_eth(); //initialize the Ethernet connection + TCPSocketServer server; + server.bind(PORT); + server.listen(); // nasluch przychodzacych polaczen + + + Switch1.fall(&blink); + Switch2.fall(&blink1); + + + + + while (true) //wykonuj caly czas + { + + + TCPSocketConnection client; + + server.accept(client); // akceptacja nowego polaczenia - while (true) //repeat forever - { - receive(); //wait for message + client.set_blocking(false, 1500); // timeout (1.5)s + + c=RedLed; + + switch(c) + { + case 0: + if (BlueLed==0){ + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> LEDY </h1> <p><font color=""red"">Dioda Czerwona ON</font></p> oraz <p><font color=""blue"">Dioda Niebieska ON</font></p><form action=""demo_form.asp""></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); // wyslij wszystko + } + else + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> LEDY </h1> <p><font color=""red"">Dioda Czerwona ON</font></p> oraz <p><font color=""blue"">Dioda Niebieska OFF</font></p><form action=""demo_form.asp""></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); // wyslij wszystko + } + break; + + case 1: + if (BlueLed==1){ + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> LEDY </h1> <p><font color=""red"">Dioda Czerwona OFF</font></p> oraz <p><font color=""blue"">Dioda Niebieska OFF</font></p><form action=""demo_form.asp""></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); // wyslij wszystko + + } + else + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> LEDY </h1> <p><font color=""red"">Dioda Czerwona OFF</font></p> oraz <p><font color=""blue"">Dioda Niebieska ON</font></p><form action=""demo_form.asp""></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); // wyslij wszystko + + } + } + + + + /* + if (RedLed==0) + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Dioda Czerwona włączona </h1> <p> LED </p><form action=""demo_form.asp""><SELECT NAME=""LED_COLOUR""> <OPTION VALUE=""""> <OPTION VALUE=""GREEN"">GREEN <OPTION VALUE=""RED"">RED <input type=""submit"" value=""Submit""> </SELECT></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); + } + else + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Dioda Czerwona wyłączona </h1> <p> LED </p><form action=""demo_form.asp""><SELECT NAME=""LED_COLOUR""> <OPTION VALUE=""""> <OPTION VALUE=""GREEN"">GREEN <OPTION VALUE=""RED"">RED <input type=""submit"" value=""Submit""> </SELECT></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); + } +} + if (RedBlue==0) + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Dioda Niebieska włączona </h1> <p> LED </p><form action=""demo_form.asp""><SELECT NAME=""LED_COLOUR""> <OPTION VALUE=""""> <OPTION VALUE=""GREEN"">GREEN <OPTION VALUE=""RED"">RED <input type=""submit"" value=""Submit""> </SELECT></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); + else + { + char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Dioda Niebieska włączona </h1> <p> LED </p><form action=""demo_form.asp""><SELECT NAME=""LED_COLOUR""> <OPTION VALUE=""""> <OPTION VALUE=""GREEN"">GREEN <OPTION VALUE=""RED"">RED <input type=""submit"" value=""Submit""> </SELECT></form></body> </html>"; + client.send_all(http_cmd, sizeof(http_cmd)-1); + } + } + + */ + + + client.close(); + + pc.printf("\n\nClient closed\n\n\r"); + + + } - -} //end main() + }