Simple HTTP server example with WIZ550io, W5500 (WIZnet chipset) .access the mbed via any browser, mbed echo back http request header.

Dependencies:   W5500Interface mbed

Fork of HTTPServer_echoback_WIZ550io by Bongjun Hur

Committer:
Bongjun
Date:
Mon Jul 21 07:45:30 2014 +0000
Revision:
18:bed64c7e5a72
Parent:
17:c42419afb2f8
Child:
19:59a668f08555
add platform TARGET_NUCLEO_F030R8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
Bongjun 16:ea8f49af2d86 2 //#include "EthernetInterface.h"
Bongjun 16:ea8f49af2d86 3 #include "EthernetInterfaceW5500.h"
hsgw 13:25ce35614dd1 4 #include <stdio.h>
hsgw 13:25ce35614dd1 5 #include <string.h>
donatien 0:bb128f0e952f 6
hsgw 13:25ce35614dd1 7 #define PORT 80
hsgw 13:25ce35614dd1 8
Bongjun 17:c42419afb2f8 9 // move to main().
Bongjun 16:ea8f49af2d86 10 //EthernetInterface eth;
hsgw 13:25ce35614dd1 11
Bongjun 17:c42419afb2f8 12 // move to main().
Bongjun 16:ea8f49af2d86 13 //TCPSocketServer svr;
hsgw 13:25ce35614dd1 14 bool serverIsListened = false;
hsgw 13:25ce35614dd1 15
Bongjun 17:c42419afb2f8 16 // move to main().
Bongjun 16:ea8f49af2d86 17 //TCPSocketConnection client;
hsgw 13:25ce35614dd1 18 bool clientIsConnected = false;
hsgw 13:25ce35614dd1 19 DigitalOut led1(LED1); //server listning status
hsgw 13:25ce35614dd1 20 DigitalOut led2(LED2); //socket connecting status
hsgw 13:25ce35614dd1 21
hsgw 13:25ce35614dd1 22 Ticker ledTick;
hsgw 13:25ce35614dd1 23
hsgw 13:25ce35614dd1 24 void ledTickfunc()
hsgw 13:25ce35614dd1 25 {
hsgw 13:25ce35614dd1 26 if(serverIsListened) {
hsgw 13:25ce35614dd1 27 led1 = !led1;
hsgw 13:25ce35614dd1 28 } else {
hsgw 13:25ce35614dd1 29 led1 = false;
hsgw 13:25ce35614dd1 30 }
hsgw 13:25ce35614dd1 31 }
hsgw 13:25ce35614dd1 32
hsgw 13:25ce35614dd1 33 int main (void)
hsgw 13:25ce35614dd1 34 {
Bongjun 16:ea8f49af2d86 35 #if defined(TARGET_LPC1114)
Bongjun 16:ea8f49af2d86 36 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
Bongjun 16:ea8f49af2d86 37 EthernetInterfaceW5500 eth(&spi, dp25, dp26); // spi, cs, reset
Bongjun 16:ea8f49af2d86 38
Bongjun 16:ea8f49af2d86 39 #elif defined(TARGET_LPC1768)
Bongjun 16:ea8f49af2d86 40 SPI spi(p11, p12, p13); // mosi, miso, sclk
Bongjun 16:ea8f49af2d86 41 EthernetInterfaceW5500 eth(&spi, p14, p15); // spi, cs, reset
Bongjun 16:ea8f49af2d86 42
Bongjun 16:ea8f49af2d86 43 #elif defined(TARGET_LPC11U68)
Bongjun 16:ea8f49af2d86 44 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
Bongjun 16:ea8f49af2d86 45 //WIZnetInterface eth(&spi, P0_2, P1_25); // spi, cs, reset
Bongjun 16:ea8f49af2d86 46 //SPI spi(p5, p6, p7); // mosi, miso, sclk
Bongjun 16:ea8f49af2d86 47 EthernetInterfaceW5500 eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 16:ea8f49af2d86 48 spi.format(8,0); // 8bit, mode 0
Bongjun 16:ea8f49af2d86 49 spi.frequency(7000000); // 7MHz
Bongjun 16:ea8f49af2d86 50 wait(1); // 1 second for stable state
Bongjun 18:bed64c7e5a72 51 #elif defined (TARGET_NUCLEO_F030R8)
Bongjun 18:bed64c7e5a72 52 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
Bongjun 18:bed64c7e5a72 53 EthernetInterfaceW5500 eth(&spi, PB_6, PA_9);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 18:bed64c7e5a72 54 spi.format(8,0); // 8bit, mode 0
Bongjun 18:bed64c7e5a72 55 spi.frequency(7000000); // 7MHz
Bongjun 18:bed64c7e5a72 56 wait(1); // 1 second for stable state
Bongjun 16:ea8f49af2d86 57
Bongjun 16:ea8f49af2d86 58 #endif
Bongjun 16:ea8f49af2d86 59
Bongjun 17:c42419afb2f8 60 // late binding
Bongjun 17:c42419afb2f8 61 TCPSocketServer svr;
Bongjun 17:c42419afb2f8 62 TCPSocketConnection client;
Bongjun 16:ea8f49af2d86 63
hsgw 13:25ce35614dd1 64 ledTick.attach(&ledTickfunc,0.5);
hsgw 13:25ce35614dd1 65
Bongjun 17:c42419afb2f8 66 /*
Bongjun 17:c42419afb2f8 67 //setup ethernet interface
Bongjun 17:c42419afb2f8 68 eth.init(); //Use DHCP
Bongjun 17:c42419afb2f8 69 eth.connect();
Bongjun 17:c42419afb2f8 70 printf("IP Address is %s\n\r", eth.getIPAddress());
Bongjun 17:c42419afb2f8 71 */
Bongjun 16:ea8f49af2d86 72
Bongjun 16:ea8f49af2d86 73 #ifdef _DHCP // didn't test yet in DHCP
Bongjun 16:ea8f49af2d86 74
Bongjun 16:ea8f49af2d86 75 eth.init(); //Use DHCP
Bongjun 16:ea8f49af2d86 76 eth.connect();
Bongjun 16:ea8f49af2d86 77 //printf("IP Address is %s\n\r", eth.getIPAddress());
Bongjun 16:ea8f49af2d86 78 printf("Initialized, MAC: %s\n", eth.getMACAddress());
Bongjun 16:ea8f49af2d86 79 printf("Connected, IP: %s, MASK: %s, GW: %s\n",
Bongjun 16:ea8f49af2d86 80 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
Bongjun 16:ea8f49af2d86 81
Bongjun 16:ea8f49af2d86 82 #else
Bongjun 16:ea8f49af2d86 83 // as your env. change to real IP address and so on.
Bongjun 16:ea8f49af2d86 84 int ret = eth.init("222.98.173.212", "255.255.255.192", "222.98.173.254");
Bongjun 16:ea8f49af2d86 85 if (!ret) {
Bongjun 16:ea8f49af2d86 86 printf("Initialized, MAC: %s\n", eth.getMACAddress());
Bongjun 16:ea8f49af2d86 87 printf("Connected, IP: %s, MASK: %s, GW: %s\n",
Bongjun 16:ea8f49af2d86 88 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
Bongjun 16:ea8f49af2d86 89 } else {
Bongjun 16:ea8f49af2d86 90 printf("Error eth.init() - ret = %d\n", ret);
Bongjun 16:ea8f49af2d86 91 return -1;
Bongjun 16:ea8f49af2d86 92 }
Bongjun 16:ea8f49af2d86 93 #endif
hsgw 13:25ce35614dd1 94
hsgw 13:25ce35614dd1 95 //setup tcp socket
hsgw 13:25ce35614dd1 96 if(svr.bind(PORT)< 0) {
hsgw 13:25ce35614dd1 97 printf("tcp server bind failed.\n\r");
hsgw 13:25ce35614dd1 98 return -1;
hsgw 13:25ce35614dd1 99 } else {
hsgw 13:25ce35614dd1 100 printf("tcp server bind successed.\n\r");
hsgw 13:25ce35614dd1 101 serverIsListened = true;
hsgw 13:25ce35614dd1 102 }
hsgw 13:25ce35614dd1 103
hsgw 13:25ce35614dd1 104 if(svr.listen(1) < 0) {
hsgw 13:25ce35614dd1 105 printf("tcp server listen failed.\n\r");
hsgw 13:25ce35614dd1 106 return -1;
hsgw 13:25ce35614dd1 107 } else {
hsgw 13:25ce35614dd1 108 printf("tcp server is listening...\n\r");
emilmont 7:65188f4a8c25 109 }
hsgw 13:25ce35614dd1 110
hsgw 13:25ce35614dd1 111 //listening for http GET request
hsgw 13:25ce35614dd1 112 while (serverIsListened) {
hsgw 15:eae1575da9ca 113 //blocking mode(never timeout)
hsgw 13:25ce35614dd1 114 if(svr.accept(client)<0) {
hsgw 13:25ce35614dd1 115 printf("failed to accept connection.\n\r");
hsgw 13:25ce35614dd1 116 } else {
hsgw 13:25ce35614dd1 117 printf("connection success!\n\rIP: %s\n\r",client.get_address());
hsgw 13:25ce35614dd1 118 clientIsConnected = true;
hsgw 13:25ce35614dd1 119 led2 = true;
Bongjun 17:c42419afb2f8 120
hsgw 13:25ce35614dd1 121 while(clientIsConnected) {
hsgw 13:25ce35614dd1 122 char buffer[1024] = {};
hsgw 13:25ce35614dd1 123 switch(client.receive(buffer, 1023)) {
hsgw 13:25ce35614dd1 124 case 0:
hsgw 13:25ce35614dd1 125 printf("recieved buffer is empty.\n\r");
hsgw 13:25ce35614dd1 126 clientIsConnected = false;
hsgw 13:25ce35614dd1 127 break;
hsgw 13:25ce35614dd1 128 case -1:
hsgw 13:25ce35614dd1 129 printf("failed to read data from client.\n\r");
hsgw 13:25ce35614dd1 130 clientIsConnected = false;
hsgw 13:25ce35614dd1 131 break;
hsgw 13:25ce35614dd1 132 default:
hsgw 13:25ce35614dd1 133 printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
hsgw 13:25ce35614dd1 134 if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
hsgw 13:25ce35614dd1 135 printf("GET request incomming.\n\r");
hsgw 13:25ce35614dd1 136 //setup http response header & data
hsgw 13:25ce35614dd1 137 char echoHeader[256] = {};
hsgw 13:25ce35614dd1 138 sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
hsgw 13:25ce35614dd1 139 client.send(echoHeader,strlen(echoHeader));
hsgw 13:25ce35614dd1 140 client.send(buffer,strlen(buffer));
hsgw 13:25ce35614dd1 141 clientIsConnected = false;
hsgw 13:25ce35614dd1 142 printf("echo back done.\n\r");
hsgw 13:25ce35614dd1 143 }
hsgw 13:25ce35614dd1 144 break;
hsgw 13:25ce35614dd1 145 }
hsgw 13:25ce35614dd1 146 }
hsgw 13:25ce35614dd1 147 printf("close connection.\n\rtcp server is listening...\n\r");
hsgw 13:25ce35614dd1 148 client.close();
hsgw 13:25ce35614dd1 149 led2 = false;
hsgw 13:25ce35614dd1 150 }
hsgw 13:25ce35614dd1 151 }
donatien 0:bb128f0e952f 152 }