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>.

Dependencies:   UIPEthernet

Committer:
hudakz
Date:
Sat Dec 20 12:02:19 2014 +0000
Revision:
1:9c602cc98b9f
Parent:
0:8b40576553d2
Child:
2:519b6ae198ae
rev 01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:8b40576553d2 1 /*
hudakz 0:8b40576553d2 2 * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser.
hudakz 0:8b40576553d2 3 * Ethernet connection is via an ENC28J60 board driven by UIPEthernet
hudakz 0:8b40576553d2 4 */
hudakz 0:8b40576553d2 5
hudakz 0:8b40576553d2 6 #include "mbed.h"
hudakz 0:8b40576553d2 7 #include <UIPEthernet.h>
hudakz 0:8b40576553d2 8 #include <UIPServer.h>
hudakz 0:8b40576553d2 9 #include <UIPClient.h>
hudakz 0:8b40576553d2 10
hudakz 0:8b40576553d2 11 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:8b40576553d2 12 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:8b40576553d2 13 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 0:8b40576553d2 14 #if defined(TARGET_LPC1768)
hudakz 0:8b40576553d2 15 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 16 #elif defined(TARGET_LPC1114)
hudakz 0:8b40576553d2 17 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 18 #elif defined(TARGET_LPC11U68)
hudakz 0:8b40576553d2 19 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 20 #elif defined (TARGET_NUCLEO_F103RB)
hudakz 0:8b40576553d2 21 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 22 #endif
hudakz 0:8b40576553d2 23
hudakz 0:8b40576553d2 24 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 0:8b40576553d2 25 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
hudakz 0:8b40576553d2 26 // IP address must be unique and compatible with your network. Change as appropriate.
hudakz 0:8b40576553d2 27 const IPAddress MY_IP(192,168,1,181);
hudakz 0:8b40576553d2 28 const uint16_t MY_PORT = 80; // for HTTP connection
hudakz 0:8b40576553d2 29 EthernetServer myServer = EthernetServer(MY_PORT);
hudakz 0:8b40576553d2 30 Serial serial(USBTX, USBRX);
hudakz 0:8b40576553d2 31
hudakz 0:8b40576553d2 32 int main()
hudakz 0:8b40576553d2 33 {
hudakz 1:9c602cc98b9f 34 UIPEthernet.begin(MY_MAC,MY_IP);
hudakz 0:8b40576553d2 35 myServer.begin();
hudakz 0:8b40576553d2 36 while(1) {
hudakz 0:8b40576553d2 37 EthernetClient client = myServer.available();
hudakz 0:8b40576553d2 38 if (client) {
hudakz 0:8b40576553d2 39 size_t size = client.available();
hudakz 0:8b40576553d2 40 if(size > 0) {
hudakz 0:8b40576553d2 41 char* buf = (char*)malloc(size);
hudakz 0:8b40576553d2 42 size = client.read((uint8_t*)buf, size);
hudakz 0:8b40576553d2 43 if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') {
hudakz 0:8b40576553d2 44 serial.printf("GET request received:\n\r");
hudakz 0:8b40576553d2 45 serial.printf(buf);
hudakz 0:8b40576553d2 46 char echoHeader[256] = {};
hudakz 0:8b40576553d2 47 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);
hudakz 0:8b40576553d2 48 client.write((uint8_t*)echoHeader,strlen(echoHeader));
hudakz 0:8b40576553d2 49 client.write((uint8_t*)buf, size);
hudakz 0:8b40576553d2 50 serial.printf("Echo done.\r\n");
hudakz 0:8b40576553d2 51 }
hudakz 0:8b40576553d2 52 free(buf);
hudakz 0:8b40576553d2 53 }
hudakz 0:8b40576553d2 54 }
hudakz 0:8b40576553d2 55 }
hudakz 0:8b40576553d2 56 }