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:
Fri Jun 30 19:57:05 2017 +0000
Revision:
6:c5eb31c60c8f
Parent:
5:068f4c368ab0
Child:
7:f6058bcaa614
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 2:519b6ae198ae 1 /*
hudakz 0:8b40576553d2 2 * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser.
hudakz 3:f3bfd257e138 3 * Ethernet connection is via an ENC28J60 Ethernet board driven by the UIPEthernet library
hudakz 0:8b40576553d2 4 */
hudakz 0:8b40576553d2 5 #include "mbed.h"
hudakz 6:c5eb31c60c8f 6 #include "UIPEthernet.h"
hudakz 6:c5eb31c60c8f 7 #include "UIPServer.h"
hudakz 6:c5eb31c60c8f 8 #include "UIPClient.h"
hudakz 6:c5eb31c60c8f 9
hudakz 6:c5eb31c60c8f 10 #define DHCP 1 // if you'd like to use static IP address comment out this line
hudakz 6:c5eb31c60c8f 11
hudakz 6:c5eb31c60c8f 12 // MAC address must be unique within the connected network. Modify as appropriate.
hudakz 6:c5eb31c60c8f 13 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 6:c5eb31c60c8f 14 const uint16_t MY_PORT = 80; // for HTTP connection
hudakz 0:8b40576553d2 15
hudakz 2:519b6ae198ae 16 Serial pc(USBTX, USBRX);
hudakz 2:519b6ae198ae 17
hudakz 0:8b40576553d2 18 #if defined(TARGET_LPC1768)
hudakz 6:c5eb31c60c8f 19 UIPEthernet uIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 6:c5eb31c60c8f 20 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \
hudakz 6:c5eb31c60c8f 21 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \
hudakz 6:c5eb31c60c8f 22 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \
hudakz 6:c5eb31c60c8f 23 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
hudakz 5:068f4c368ab0 24 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
hudakz 5:068f4c368ab0 25 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
hudakz 5:068f4c368ab0 26 || defined(TARGET_NRF51822) \
hudakz 5:068f4c368ab0 27 || defined(TARGET_RZ_A1H)
hudakz 6:c5eb31c60c8f 28 UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 29 #endif
hudakz 0:8b40576553d2 30
hudakz 2:519b6ae198ae 31 EthernetServer myServer = EthernetServer(MY_PORT);
hudakz 2:519b6ae198ae 32
hudakz 6:c5eb31c60c8f 33 int main(void)
hudakz 6:c5eb31c60c8f 34 {
hudakz 3:f3bfd257e138 35 #if defined(DHCP)
hudakz 5:068f4c368ab0 36 pc.printf("Searching for DHCP server..\r\n");
hudakz 6:c5eb31c60c8f 37 if (uIPEthernet.begin(MY_MAC) != 1) {
hudakz 2:519b6ae198ae 38 pc.printf("No DHCP server found.\r\n");
hudakz 4:06fcbe983e24 39 pc.printf("Exiting application.\r\n");
hudakz 2:519b6ae198ae 40 return 0;
hudakz 2:519b6ae198ae 41 }
hudakz 6:c5eb31c60c8f 42 pc.printf("DHCP server found.\r\n");
hudakz 2:519b6ae198ae 43 #else
hudakz 6:c5eb31c60c8f 44 // IP address must be unique and compatible with your network.
hudakz 6:c5eb31c60c8f 45 const IPAddress MY_IP(192, 168, 1, 181); // Change as appropriate.
hudakz 6:c5eb31c60c8f 46
hudakz 6:c5eb31c60c8f 47 uIPEthernet.begin(MY_MAC, MY_IP);
hudakz 2:519b6ae198ae 48 #endif
hudakz 2:519b6ae198ae 49
hudakz 6:c5eb31c60c8f 50 pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString());
hudakz 0:8b40576553d2 51 myServer.begin();
hudakz 6:c5eb31c60c8f 52
hudakz 6:c5eb31c60c8f 53 while (1) {
hudakz 6:c5eb31c60c8f 54 char echoHeader[256];
hudakz 2:519b6ae198ae 55 EthernetClient client = myServer.available();
hudakz 6:c5eb31c60c8f 56
hudakz 6:c5eb31c60c8f 57 if (client) {
hudakz 2:519b6ae198ae 58 size_t size = client.available();
hudakz 6:c5eb31c60c8f 59
hudakz 6:c5eb31c60c8f 60 if (size > 0) {
hudakz 2:519b6ae198ae 61 char* buf = (char*)malloc(size);
hudakz 6:c5eb31c60c8f 62
hudakz 0:8b40576553d2 63 size = client.read((uint8_t*)buf, size);
hudakz 6:c5eb31c60c8f 64 if (buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') {
hudakz 4:06fcbe983e24 65 pc.printf("GET request received:\n\r");
hudakz 4:06fcbe983e24 66 pc.printf(buf);
hudakz 2:519b6ae198ae 67 sprintf
hudakz 2:519b6ae198ae 68 (
hudakz 2:519b6ae198ae 69 echoHeader,
hudakz 2:519b6ae198ae 70 "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text\r\nConnection: About to close\r\n\r\n",
hudakz 2:519b6ae198ae 71 size
hudakz 2:519b6ae198ae 72 );
hudakz 2:519b6ae198ae 73 client.write((uint8_t*)echoHeader, strlen(echoHeader));
hudakz 0:8b40576553d2 74 client.write((uint8_t*)buf, size);
hudakz 4:06fcbe983e24 75 pc.printf("Echo done.\r\n");
hudakz 0:8b40576553d2 76 }
hudakz 6:c5eb31c60c8f 77
hudakz 0:8b40576553d2 78 free(buf);
hudakz 0:8b40576553d2 79 }
hudakz 0:8b40576553d2 80 }
hudakz 0:8b40576553d2 81 }
hudakz 0:8b40576553d2 82 }