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 05:13:27 2014 +0000
Revision:
16:ea8f49af2d86
Parent:
15:eae1575da9ca
Child:
17:c42419afb2f8
HTTPServer echo back example for WIZ550io, W5500 ( WIZnet chipset); Ethernet connection.

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