Eth library

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
jackolo
Date:
Sat Apr 21 08:43:33 2018 +0000
Revision:
0:9b72cde90412
SKM - eth lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jackolo 0:9b72cde90412 1 #include "mbed.h"
jackolo 0:9b72cde90412 2 #include "EthernetInterface.h"
jackolo 0:9b72cde90412 3
jackolo 0:9b72cde90412 4 #define MBED_DEV_IP "192.168.0.52"
jackolo 0:9b72cde90412 5 #define MBED_DEV_MASK "255.255.255.0"
jackolo 0:9b72cde90412 6 #define MBED_DEV_GW "0.0.0.0"
jackolo 0:9b72cde90412 7 #define ECHO_SERVER_PORT 80
jackolo 0:9b72cde90412 8
jackolo 0:9b72cde90412 9 Serial pc(USBTX, USBRX); // Debug via USB port.
jackolo 0:9b72cde90412 10 DigitalOut red_led(LED_RED, 1);
jackolo 0:9b72cde90412 11 DigitalOut green_led(LED_GREEN, 1);
jackolo 0:9b72cde90412 12
jackolo 0:9b72cde90412 13 char find_green[50] = "GET /demo_form.asp?LED_COLOUR=GREEN";
jackolo 0:9b72cde90412 14 char find_red[50] = "GET /demo_form.asp?LED_COLOUR=RED";
jackolo 0:9b72cde90412 15
jackolo 0:9b72cde90412 16 int main (void) {
jackolo 0:9b72cde90412 17
jackolo 0:9b72cde90412 18 pc.printf("\nHello!\n\r");
jackolo 0:9b72cde90412 19 //pc.baud(9600);
jackolo 0:9b72cde90412 20 EthernetInterface eth; // Interface using Ethernet to connect to an IP-based network.
jackolo 0:9b72cde90412 21 eth.init(); // Initialize the interface with DHCP.
jackolo 0:9b72cde90412 22 //eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); // Initialize the interface with a static IP address.
jackolo 0:9b72cde90412 23 eth.connect(); // Connect Bring the interface up, start DHCP if needed (Dynamic Host Configuration Protocol).
jackolo 0:9b72cde90412 24
jackolo 0:9b72cde90412 25 pc.printf("\nIP Address is %s\n\r", eth.getIPAddress());
jackolo 0:9b72cde90412 26
jackolo 0:9b72cde90412 27 TCPSocketServer server; // Instantiate a TCP Server.
jackolo 0:9b72cde90412 28 server.bind(ECHO_SERVER_PORT); // Bind a socket to a specific port (80 = HTTP).
jackolo 0:9b72cde90412 29 server.listen(); // Start listening for incoming connections.
jackolo 0:9b72cde90412 30
jackolo 0:9b72cde90412 31 while (true) {
jackolo 0:9b72cde90412 32 pc.printf("\nWait for new connection...\n\r");
jackolo 0:9b72cde90412 33 TCPSocketConnection client; // TCP socket connection.
jackolo 0:9b72cde90412 34 server.accept(client); // Accept a new connection.
jackolo 0:9b72cde90412 35 client.set_blocking(false, 1500); // Timeout after (1.5)s
jackolo 0:9b72cde90412 36
jackolo 0:9b72cde90412 37 pc.printf("\nConnection from: %s\n\r", client.get_address());
jackolo 0:9b72cde90412 38
jackolo 0:9b72cde90412 39 char buffer[600];
jackolo 0:9b72cde90412 40 int ret;
jackolo 0:9b72cde90412 41
jackolo 0:9b72cde90412 42 /////////////GET//////////////
jackolo 0:9b72cde90412 43 while (true) {
jackolo 0:9b72cde90412 44 ret = client.receive(buffer, sizeof(buffer)-1); // Receive data from the remote host.
jackolo 0:9b72cde90412 45 if (ret <= 0)break;
jackolo 0:9b72cde90412 46 buffer[ret] = '\0';
jackolo 0:9b72cde90412 47 printf("Received %d chars from server:\n\r%s\n\r", ret, buffer);
jackolo 0:9b72cde90412 48
jackolo 0:9b72cde90412 49 // LED colour seeking:
jackolo 0:9b72cde90412 50 if(strstr(buffer, find_red)>0) {
jackolo 0:9b72cde90412 51 red_led=0;
jackolo 0:9b72cde90412 52 green_led=1;
jackolo 0:9b72cde90412 53 }
jackolo 0:9b72cde90412 54 if(strstr(buffer, find_green)>0) {
jackolo 0:9b72cde90412 55 red_led=1;
jackolo 0:9b72cde90412 56 green_led=0;
jackolo 0:9b72cde90412 57 }
jackolo 0:9b72cde90412 58 }
jackolo 0:9b72cde90412 59 /////////////SET//////////////
jackolo 0:9b72cde90412 60 // Webpage here:
jackolo 0:9b72cde90412 61
jackolo 0:9b72cde90412 62 char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Hello World! </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>";
jackolo 0:9b72cde90412 63 /*
jackolo 0:9b72cde90412 64 <!DOCTYPE html>
jackolo 0:9b72cde90412 65 <html>
jackolo 0:9b72cde90412 66 <head>
jackolo 0:9b72cde90412 67 <title> FRDM-K64F </title>
jackolo 0:9b72cde90412 68 </head>
jackolo 0:9b72cde90412 69 <body>
jackolo 0:9b72cde90412 70
jackolo 0:9b72cde90412 71 <h1> Hello World! </h1>
jackolo 0:9b72cde90412 72 <p> LED </p>
jackolo 0:9b72cde90412 73
jackolo 0:9b72cde90412 74 <form action="demo_form.asp">
jackolo 0:9b72cde90412 75 <SELECT NAME="LED_COLOUR">
jackolo 0:9b72cde90412 76 <OPTION VALUE="">
jackolo 0:9b72cde90412 77 <OPTION VALUE="GREEN">
jackolo 0:9b72cde90412 78 GREEN
jackolo 0:9b72cde90412 79 <OPTION VALUE="RED">
jackolo 0:9b72cde90412 80 RED
jackolo 0:9b72cde90412 81 <input type="submit" value="Submit">
jackolo 0:9b72cde90412 82 </SELECT>
jackolo 0:9b72cde90412 83 </form>
jackolo 0:9b72cde90412 84
jackolo 0:9b72cde90412 85 </body>
jackolo 0:9b72cde90412 86 </html>";
jackolo 0:9b72cde90412 87 */
jackolo 0:9b72cde90412 88 client.send_all(http_cmd, sizeof(http_cmd)-1); // Send all the data to the remote host.
jackolo 0:9b72cde90412 89
jackolo 0:9b72cde90412 90 client.close(); // Close the socket.
jackolo 0:9b72cde90412 91 pc.printf("\n\nClient closed\n\n\r");
jackolo 0:9b72cde90412 92 }
jackolo 0:9b72cde90412 93 }