A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.
Diff: main.cpp
- Revision:
- 0:8b40576553d2
- Child:
- 1:9c602cc98b9f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Sep 17 08:51:50 2014 +0000 @@ -0,0 +1,56 @@ +/* + * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser. + * Ethernet connection is via an ENC28J60 board driven by UIPEthernet + */ + +#include "mbed.h" +#include <UIPEthernet.h> +#include <UIPServer.h> +#include <UIPClient.h> + +// UIPEthernet is the name of a global instance of UIPEthernetClass. +// Do not change the name! It is used within the UIPEthernet library. +// Adapt the SPI pin names to your mbed platform/board if not present yet. +#if defined(TARGET_LPC1768) +UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs +#elif defined(TARGET_LPC1114) +UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs +#elif defined(TARGET_LPC11U68) +UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs +#elif defined (TARGET_NUCLEO_F103RB) +UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs +#endif + +// MAC number must be unique within the connected network. Modify as appropriate. +const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; +// IP address must be unique and compatible with your network. Change as appropriate. +const IPAddress MY_IP(192,168,1,181); +const uint16_t MY_PORT = 80; // for HTTP connection +EthernetServer myServer = EthernetServer(MY_PORT); +Serial serial(USBTX, USBRX); + +int main() +{ + Ethernet.begin(MY_MAC,MY_IP); + myServer.begin(); + while(1) { + EthernetClient client = myServer.available(); + if (client) { + size_t size = client.available(); + if(size > 0) { + char* buf = (char*)malloc(size); + size = client.read((uint8_t*)buf, size); + if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') { + serial.printf("GET request received:\n\r"); + serial.printf(buf); + char echoHeader[256] = {}; + sprintf(echoHeader,"HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text\r\nConnection: About to close\r\n\r\n", size); + client.write((uint8_t*)echoHeader,strlen(echoHeader)); + client.write((uint8_t*)buf, size); + serial.printf("Echo done.\r\n"); + } + free(buf); + } + } + } +}