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>.
Diff: main.cpp
- Revision:
- 2:519b6ae198ae
- Parent:
- 1:9c602cc98b9f
- Child:
- 3:f3bfd257e138
--- a/main.cpp Sat Dec 20 12:02:19 2014 +0000 +++ b/main.cpp Fri Nov 27 20:35:35 2015 +0000 @@ -1,51 +1,96 @@ -/* +/* * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser. * Ethernet connection is via an ENC28J60 board driven by UIPEthernet */ - #include "mbed.h" #include <UIPEthernet.h> #include <UIPServer.h> #include <UIPClient.h> +Serial pc(USBTX, USBRX); + +#define DHCP 1 // comment out this line if you are not using DHCP + // UIPEthernet is the name of a global instance of UIPEthernetClass. + // Do not change the name! It is used within the UIPEthernet library. // Adapt the SPI pin names to your mbed platform/board if not present yet. #if defined(TARGET_LPC1768) -UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs #elif defined(TARGET_LPC1114) -UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs #elif defined(TARGET_LPC11U68) -UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs -#elif defined (TARGET_NUCLEO_F103RB) -UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F103RB) +UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F103RB) +UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F401RE) +UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F411RE) +UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +#endif +// MAC number must be unique within the connected network. Modify as appropriate. + +const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; + +#if !defined(DHCP) +// IP address must be unique and compatible with your network. Change as appropriate. +const IPAddress MY_IP(192, 168, 1, 181); #endif -// MAC number must be unique within the connected network. Modify as appropriate. -const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; -// IP address must be unique and compatible with your network. Change as appropriate. -const IPAddress MY_IP(192,168,1,181); -const uint16_t MY_PORT = 80; // for HTTP connection -EthernetServer myServer = EthernetServer(MY_PORT); -Serial serial(USBTX, USBRX); +const uint16_t MY_PORT = 80; // for HTTP connection +EthernetServer myServer = EthernetServer(MY_PORT); +Serial serial(USBTX, USBRX); + +/** + * @brief + * @note + * @param + * @retval + */ + +int main(void) { + pc.printf("Searching for DHCP server.\r\n"); + pc.printf("It takes some time. Please wait..\r\n"); -int main() -{ - UIPEthernet.begin(MY_MAC,MY_IP); +#if defined(DHCP) + if(UIPEthernet.begin(MY_MAC) != 1) { + pc.printf("No DHCP server found.\r\n"); + pc.printf("Exiting application\r\n"); + return 0; + } + + pc.printf("DHCP server found and configuration info received\r\n"); + + IPAddress localIP = UIPEthernet.localIP(); + pc.printf("Local IP = "); + for(uint8_t i = 0; i < 3; i++) + pc.printf("%d.", localIP[i]); + pc.printf("%d\r\n", localIP[3]); +#else + UIPEthernet.begin(MY_MAC, MY_IP); +#endif + myServer.begin(); while(1) { - EthernetClient client = myServer.available(); - if (client) { - size_t size = client.available(); + EthernetClient client = myServer.available(); + if(client) { + size_t size = client.available(); if(size > 0) { - char* buf = (char*)malloc(size); + char* buf = (char*)malloc(size); size = client.read((uint8_t*)buf, size); if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') { serial.printf("GET request received:\n\r"); serial.printf(buf); - char echoHeader[256] = {}; - 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); - client.write((uint8_t*)echoHeader,strlen(echoHeader)); + char echoHeader[256] = { }; + 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 + ); + client.write((uint8_t*)echoHeader, strlen(echoHeader)); client.write((uint8_t*)buf, size); serial.printf("Echo done.\r\n"); }