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