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 Nov 27 21:15:45 2015 +0000
Revision:
4:06fcbe983e24
Parent:
3:f3bfd257e138
Child:
5:068f4c368ab0
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 0:8b40576553d2 6 #include <UIPEthernet.h>
hudakz 0:8b40576553d2 7 #include <UIPServer.h>
hudakz 0:8b40576553d2 8 #include <UIPClient.h>
hudakz 0:8b40576553d2 9
hudakz 2:519b6ae198ae 10 Serial pc(USBTX, USBRX);
hudakz 2:519b6ae198ae 11
hudakz 4:06fcbe983e24 12 #define DHCP 1 // comment out this line if you'd like to use static IP address
hudakz 2:519b6ae198ae 13
hudakz 0:8b40576553d2 14 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:8b40576553d2 15 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:8b40576553d2 16 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 0:8b40576553d2 17 #if defined(TARGET_LPC1768)
hudakz 2:519b6ae198ae 18 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 19 #elif defined(TARGET_LPC1114)
hudakz 2:519b6ae198ae 20 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
hudakz 0:8b40576553d2 21 #elif defined(TARGET_LPC11U68)
hudakz 2:519b6ae198ae 22 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
hudakz 2:519b6ae198ae 23 #elif defined(TARGET_NUCLEO_F103RB)
hudakz 2:519b6ae198ae 24 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 2:519b6ae198ae 25 #elif defined(TARGET_NUCLEO_F103RB)
hudakz 2:519b6ae198ae 26 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 2:519b6ae198ae 27 #elif defined(TARGET_NUCLEO_F401RE)
hudakz 2:519b6ae198ae 28 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 2:519b6ae198ae 29 #elif defined(TARGET_NUCLEO_F411RE)
hudakz 2:519b6ae198ae 30 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 2:519b6ae198ae 31 #endif
hudakz 3:f3bfd257e138 32
hudakz 2:519b6ae198ae 33 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 2:519b6ae198ae 34 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 2:519b6ae198ae 35
hudakz 2:519b6ae198ae 36 #if !defined(DHCP)
hudakz 2:519b6ae198ae 37 // IP address must be unique and compatible with your network. Change as appropriate.
hudakz 2:519b6ae198ae 38 const IPAddress MY_IP(192, 168, 1, 181);
hudakz 0:8b40576553d2 39 #endif
hudakz 0:8b40576553d2 40
hudakz 2:519b6ae198ae 41 const uint16_t MY_PORT = 80; // for HTTP connection
hudakz 2:519b6ae198ae 42 EthernetServer myServer = EthernetServer(MY_PORT);
hudakz 2:519b6ae198ae 43
hudakz 2:519b6ae198ae 44 /**
hudakz 2:519b6ae198ae 45 * @brief
hudakz 2:519b6ae198ae 46 * @note
hudakz 2:519b6ae198ae 47 * @param
hudakz 2:519b6ae198ae 48 * @retval
hudakz 2:519b6ae198ae 49 */
hudakz 2:519b6ae198ae 50
hudakz 2:519b6ae198ae 51 int main(void) {
hudakz 3:f3bfd257e138 52
hudakz 3:f3bfd257e138 53 #if defined(DHCP)
hudakz 2:519b6ae198ae 54 pc.printf("Searching for DHCP server.\r\n");
hudakz 4:06fcbe983e24 55 pc.printf("Please wait..\r\n");
hudakz 0:8b40576553d2 56
hudakz 2:519b6ae198ae 57 if(UIPEthernet.begin(MY_MAC) != 1) {
hudakz 2:519b6ae198ae 58 pc.printf("No DHCP server found.\r\n");
hudakz 4:06fcbe983e24 59 pc.printf("Exiting application.\r\n");
hudakz 2:519b6ae198ae 60 return 0;
hudakz 2:519b6ae198ae 61 }
hudakz 2:519b6ae198ae 62
hudakz 2:519b6ae198ae 63 pc.printf("DHCP server found and configuration info received\r\n");
hudakz 2:519b6ae198ae 64
hudakz 2:519b6ae198ae 65 IPAddress localIP = UIPEthernet.localIP();
hudakz 2:519b6ae198ae 66 pc.printf("Local IP = ");
hudakz 2:519b6ae198ae 67 for(uint8_t i = 0; i < 3; i++)
hudakz 2:519b6ae198ae 68 pc.printf("%d.", localIP[i]);
hudakz 2:519b6ae198ae 69 pc.printf("%d\r\n", localIP[3]);
hudakz 2:519b6ae198ae 70 #else
hudakz 2:519b6ae198ae 71 UIPEthernet.begin(MY_MAC, MY_IP);
hudakz 2:519b6ae198ae 72 #endif
hudakz 2:519b6ae198ae 73
hudakz 0:8b40576553d2 74 myServer.begin();
hudakz 0:8b40576553d2 75 while(1) {
hudakz 2:519b6ae198ae 76 EthernetClient client = myServer.available();
hudakz 2:519b6ae198ae 77 if(client) {
hudakz 2:519b6ae198ae 78 size_t size = client.available();
hudakz 0:8b40576553d2 79 if(size > 0) {
hudakz 2:519b6ae198ae 80 char* buf = (char*)malloc(size);
hudakz 0:8b40576553d2 81 size = client.read((uint8_t*)buf, size);
hudakz 0:8b40576553d2 82 if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') {
hudakz 4:06fcbe983e24 83 pc.printf("GET request received:\n\r");
hudakz 4:06fcbe983e24 84 pc.printf(buf);
hudakz 2:519b6ae198ae 85 char echoHeader[256] = { };
hudakz 2:519b6ae198ae 86 sprintf
hudakz 2:519b6ae198ae 87 (
hudakz 2:519b6ae198ae 88 echoHeader,
hudakz 2:519b6ae198ae 89 "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 90 size
hudakz 2:519b6ae198ae 91 );
hudakz 2:519b6ae198ae 92 client.write((uint8_t*)echoHeader, strlen(echoHeader));
hudakz 0:8b40576553d2 93 client.write((uint8_t*)buf, size);
hudakz 4:06fcbe983e24 94 pc.printf("Echo done.\r\n");
hudakz 0:8b40576553d2 95 }
hudakz 0:8b40576553d2 96 free(buf);
hudakz 0:8b40576553d2 97 }
hudakz 0:8b40576553d2 98 }
hudakz 0:8b40576553d2 99 }
hudakz 0:8b40576553d2 100 }