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:
Wed Jul 23 07:31:24 2014 +0000
Revision:
19:59a668f08555
Parent:
18:bed64c7e5a72
Child:
20:f0c3af075949
DHCP test is ok

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