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
main.cpp@17:c42419afb2f8, 2014-07-21 (annotated)
- Committer:
- Bongjun
- Date:
- Mon Jul 21 05:18:06 2014 +0000
- Revision:
- 17:c42419afb2f8
- Parent:
- 16:ea8f49af2d86
- Child:
- 18:bed64c7e5a72
add comments only
Who changed what in which revision?
User | Revision | Line number | New 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 | 16:ea8f49af2d86 | 51 | |
Bongjun | 16:ea8f49af2d86 | 52 | #endif |
Bongjun | 16:ea8f49af2d86 | 53 | |
Bongjun | 17:c42419afb2f8 | 54 | // late binding |
Bongjun | 17:c42419afb2f8 | 55 | TCPSocketServer svr; |
Bongjun | 17:c42419afb2f8 | 56 | TCPSocketConnection client; |
Bongjun | 16:ea8f49af2d86 | 57 | |
hsgw | 13:25ce35614dd1 | 58 | ledTick.attach(&ledTickfunc,0.5); |
hsgw | 13:25ce35614dd1 | 59 | |
Bongjun | 17:c42419afb2f8 | 60 | /* |
Bongjun | 17:c42419afb2f8 | 61 | //setup ethernet interface |
Bongjun | 17:c42419afb2f8 | 62 | eth.init(); //Use DHCP |
Bongjun | 17:c42419afb2f8 | 63 | eth.connect(); |
Bongjun | 17:c42419afb2f8 | 64 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
Bongjun | 17:c42419afb2f8 | 65 | */ |
Bongjun | 16:ea8f49af2d86 | 66 | |
Bongjun | 16:ea8f49af2d86 | 67 | #ifdef _DHCP // didn't test yet in DHCP |
Bongjun | 16:ea8f49af2d86 | 68 | |
Bongjun | 16:ea8f49af2d86 | 69 | eth.init(); //Use DHCP |
Bongjun | 16:ea8f49af2d86 | 70 | eth.connect(); |
Bongjun | 16:ea8f49af2d86 | 71 | //printf("IP Address is %s\n\r", eth.getIPAddress()); |
Bongjun | 16:ea8f49af2d86 | 72 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
Bongjun | 16:ea8f49af2d86 | 73 | printf("Connected, IP: %s, MASK: %s, GW: %s\n", |
Bongjun | 16:ea8f49af2d86 | 74 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
Bongjun | 16:ea8f49af2d86 | 75 | |
Bongjun | 16:ea8f49af2d86 | 76 | #else |
Bongjun | 16:ea8f49af2d86 | 77 | // as your env. change to real IP address and so on. |
Bongjun | 16:ea8f49af2d86 | 78 | int ret = eth.init("222.98.173.212", "255.255.255.192", "222.98.173.254"); |
Bongjun | 16:ea8f49af2d86 | 79 | if (!ret) { |
Bongjun | 16:ea8f49af2d86 | 80 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
Bongjun | 16:ea8f49af2d86 | 81 | printf("Connected, IP: %s, MASK: %s, GW: %s\n", |
Bongjun | 16:ea8f49af2d86 | 82 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
Bongjun | 16:ea8f49af2d86 | 83 | } else { |
Bongjun | 16:ea8f49af2d86 | 84 | printf("Error eth.init() - ret = %d\n", ret); |
Bongjun | 16:ea8f49af2d86 | 85 | return -1; |
Bongjun | 16:ea8f49af2d86 | 86 | } |
Bongjun | 16:ea8f49af2d86 | 87 | #endif |
hsgw | 13:25ce35614dd1 | 88 | |
hsgw | 13:25ce35614dd1 | 89 | //setup tcp socket |
hsgw | 13:25ce35614dd1 | 90 | if(svr.bind(PORT)< 0) { |
hsgw | 13:25ce35614dd1 | 91 | printf("tcp server bind failed.\n\r"); |
hsgw | 13:25ce35614dd1 | 92 | return -1; |
hsgw | 13:25ce35614dd1 | 93 | } else { |
hsgw | 13:25ce35614dd1 | 94 | printf("tcp server bind successed.\n\r"); |
hsgw | 13:25ce35614dd1 | 95 | serverIsListened = true; |
hsgw | 13:25ce35614dd1 | 96 | } |
hsgw | 13:25ce35614dd1 | 97 | |
hsgw | 13:25ce35614dd1 | 98 | if(svr.listen(1) < 0) { |
hsgw | 13:25ce35614dd1 | 99 | printf("tcp server listen failed.\n\r"); |
hsgw | 13:25ce35614dd1 | 100 | return -1; |
hsgw | 13:25ce35614dd1 | 101 | } else { |
hsgw | 13:25ce35614dd1 | 102 | printf("tcp server is listening...\n\r"); |
emilmont | 7:65188f4a8c25 | 103 | } |
hsgw | 13:25ce35614dd1 | 104 | |
hsgw | 13:25ce35614dd1 | 105 | //listening for http GET request |
hsgw | 13:25ce35614dd1 | 106 | while (serverIsListened) { |
hsgw | 15:eae1575da9ca | 107 | //blocking mode(never timeout) |
hsgw | 13:25ce35614dd1 | 108 | if(svr.accept(client)<0) { |
hsgw | 13:25ce35614dd1 | 109 | printf("failed to accept connection.\n\r"); |
hsgw | 13:25ce35614dd1 | 110 | } else { |
hsgw | 13:25ce35614dd1 | 111 | printf("connection success!\n\rIP: %s\n\r",client.get_address()); |
hsgw | 13:25ce35614dd1 | 112 | clientIsConnected = true; |
hsgw | 13:25ce35614dd1 | 113 | led2 = true; |
Bongjun | 17:c42419afb2f8 | 114 | |
hsgw | 13:25ce35614dd1 | 115 | while(clientIsConnected) { |
hsgw | 13:25ce35614dd1 | 116 | char buffer[1024] = {}; |
hsgw | 13:25ce35614dd1 | 117 | switch(client.receive(buffer, 1023)) { |
hsgw | 13:25ce35614dd1 | 118 | case 0: |
hsgw | 13:25ce35614dd1 | 119 | printf("recieved buffer is empty.\n\r"); |
hsgw | 13:25ce35614dd1 | 120 | clientIsConnected = false; |
hsgw | 13:25ce35614dd1 | 121 | break; |
hsgw | 13:25ce35614dd1 | 122 | case -1: |
hsgw | 13:25ce35614dd1 | 123 | printf("failed to read data from client.\n\r"); |
hsgw | 13:25ce35614dd1 | 124 | clientIsConnected = false; |
hsgw | 13:25ce35614dd1 | 125 | break; |
hsgw | 13:25ce35614dd1 | 126 | default: |
hsgw | 13:25ce35614dd1 | 127 | printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
hsgw | 13:25ce35614dd1 | 128 | if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { |
hsgw | 13:25ce35614dd1 | 129 | printf("GET request incomming.\n\r"); |
hsgw | 13:25ce35614dd1 | 130 | //setup http response header & data |
hsgw | 13:25ce35614dd1 | 131 | char echoHeader[256] = {}; |
hsgw | 13:25ce35614dd1 | 132 | 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 | 133 | client.send(echoHeader,strlen(echoHeader)); |
hsgw | 13:25ce35614dd1 | 134 | client.send(buffer,strlen(buffer)); |
hsgw | 13:25ce35614dd1 | 135 | clientIsConnected = false; |
hsgw | 13:25ce35614dd1 | 136 | printf("echo back done.\n\r"); |
hsgw | 13:25ce35614dd1 | 137 | } |
hsgw | 13:25ce35614dd1 | 138 | break; |
hsgw | 13:25ce35614dd1 | 139 | } |
hsgw | 13:25ce35614dd1 | 140 | } |
hsgw | 13:25ce35614dd1 | 141 | printf("close connection.\n\rtcp server is listening...\n\r"); |
hsgw | 13:25ce35614dd1 | 142 | client.close(); |
hsgw | 13:25ce35614dd1 | 143 | led2 = false; |
hsgw | 13:25ce35614dd1 | 144 | } |
hsgw | 13:25ce35614dd1 | 145 | } |
donatien | 0:bb128f0e952f | 146 | } |