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@5:068f4c368ab0, 2015-11-28 (annotated)
- Committer:
- hudakz
- Date:
- Sat Nov 28 08:59:25 2015 +0000
- Revision:
- 5:068f4c368ab0
- Parent:
- 4:06fcbe983e24
- Child:
- 6:c5eb31c60c8f
Updated.
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 | 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 | 5:068f4c368ab0 | 19 | #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F401RE) \ |
hudakz | 5:068f4c368ab0 | 20 | || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) \ |
hudakz | 5:068f4c368ab0 | 21 | || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \ |
hudakz | 5:068f4c368ab0 | 22 | || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \ |
hudakz | 5:068f4c368ab0 | 23 | || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ |
hudakz | 5:068f4c368ab0 | 24 | || defined(TARGET_NRF51822) \ |
hudakz | 5:068f4c368ab0 | 25 | || defined(TARGET_RZ_A1H) |
hudakz | 2:519b6ae198ae | 26 | UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs |
hudakz | 2:519b6ae198ae | 27 | #endif |
hudakz | 3:f3bfd257e138 | 28 | |
hudakz | 2:519b6ae198ae | 29 | // MAC number must be unique within the connected network. Modify as appropriate. |
hudakz | 2:519b6ae198ae | 30 | const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 2:519b6ae198ae | 31 | |
hudakz | 2:519b6ae198ae | 32 | #if !defined(DHCP) |
hudakz | 5:068f4c368ab0 | 33 | // In case you'd like to use static IP address: |
hudakz | 5:068f4c368ab0 | 34 | // IP address must be unique and compatible with your network. |
hudakz | 5:068f4c368ab0 | 35 | // Change as appropriate. |
hudakz | 2:519b6ae198ae | 36 | const IPAddress MY_IP(192, 168, 1, 181); |
hudakz | 0:8b40576553d2 | 37 | #endif |
hudakz | 0:8b40576553d2 | 38 | |
hudakz | 2:519b6ae198ae | 39 | const uint16_t MY_PORT = 80; // for HTTP connection |
hudakz | 2:519b6ae198ae | 40 | EthernetServer myServer = EthernetServer(MY_PORT); |
hudakz | 2:519b6ae198ae | 41 | |
hudakz | 2:519b6ae198ae | 42 | /** |
hudakz | 2:519b6ae198ae | 43 | * @brief |
hudakz | 2:519b6ae198ae | 44 | * @note |
hudakz | 2:519b6ae198ae | 45 | * @param |
hudakz | 2:519b6ae198ae | 46 | * @retval |
hudakz | 2:519b6ae198ae | 47 | */ |
hudakz | 2:519b6ae198ae | 48 | int main(void) { |
hudakz | 3:f3bfd257e138 | 49 | |
hudakz | 3:f3bfd257e138 | 50 | #if defined(DHCP) |
hudakz | 5:068f4c368ab0 | 51 | pc.printf("Searching for DHCP server..\r\n"); |
hudakz | 5:068f4c368ab0 | 52 | |
hudakz | 2:519b6ae198ae | 53 | if(UIPEthernet.begin(MY_MAC) != 1) { |
hudakz | 2:519b6ae198ae | 54 | pc.printf("No DHCP server found.\r\n"); |
hudakz | 4:06fcbe983e24 | 55 | pc.printf("Exiting application.\r\n"); |
hudakz | 2:519b6ae198ae | 56 | return 0; |
hudakz | 2:519b6ae198ae | 57 | } |
hudakz | 5:068f4c368ab0 | 58 | pc.printf("DHCP server found and configuration info received.\r\n"); |
hudakz | 2:519b6ae198ae | 59 | IPAddress localIP = UIPEthernet.localIP(); |
hudakz | 2:519b6ae198ae | 60 | pc.printf("Local IP = "); |
hudakz | 2:519b6ae198ae | 61 | for(uint8_t i = 0; i < 3; i++) |
hudakz | 2:519b6ae198ae | 62 | pc.printf("%d.", localIP[i]); |
hudakz | 2:519b6ae198ae | 63 | pc.printf("%d\r\n", localIP[3]); |
hudakz | 2:519b6ae198ae | 64 | #else |
hudakz | 2:519b6ae198ae | 65 | UIPEthernet.begin(MY_MAC, MY_IP); |
hudakz | 2:519b6ae198ae | 66 | #endif |
hudakz | 2:519b6ae198ae | 67 | |
hudakz | 0:8b40576553d2 | 68 | myServer.begin(); |
hudakz | 0:8b40576553d2 | 69 | while(1) { |
hudakz | 2:519b6ae198ae | 70 | EthernetClient client = myServer.available(); |
hudakz | 2:519b6ae198ae | 71 | if(client) { |
hudakz | 2:519b6ae198ae | 72 | size_t size = client.available(); |
hudakz | 0:8b40576553d2 | 73 | if(size > 0) { |
hudakz | 2:519b6ae198ae | 74 | char* buf = (char*)malloc(size); |
hudakz | 0:8b40576553d2 | 75 | size = client.read((uint8_t*)buf, size); |
hudakz | 0:8b40576553d2 | 76 | if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') { |
hudakz | 4:06fcbe983e24 | 77 | pc.printf("GET request received:\n\r"); |
hudakz | 4:06fcbe983e24 | 78 | pc.printf(buf); |
hudakz | 5:068f4c368ab0 | 79 | char echoHeader[256] = {}; |
hudakz | 2:519b6ae198ae | 80 | sprintf |
hudakz | 2:519b6ae198ae | 81 | ( |
hudakz | 2:519b6ae198ae | 82 | echoHeader, |
hudakz | 2:519b6ae198ae | 83 | "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 | 84 | size |
hudakz | 2:519b6ae198ae | 85 | ); |
hudakz | 2:519b6ae198ae | 86 | client.write((uint8_t*)echoHeader, strlen(echoHeader)); |
hudakz | 0:8b40576553d2 | 87 | client.write((uint8_t*)buf, size); |
hudakz | 4:06fcbe983e24 | 88 | pc.printf("Echo done.\r\n"); |
hudakz | 0:8b40576553d2 | 89 | } |
hudakz | 0:8b40576553d2 | 90 | free(buf); |
hudakz | 0:8b40576553d2 | 91 | } |
hudakz | 0:8b40576553d2 | 92 | } |
hudakz | 0:8b40576553d2 | 93 | } |
hudakz | 0:8b40576553d2 | 94 | } |