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

Revision:
6:c5eb31c60c8f
Parent:
5:068f4c368ab0
Child:
7:f6058bcaa614
--- a/main.cpp	Sat Nov 28 08:59:25 2015 +0000
+++ b/main.cpp	Fri Jun 30 19:57:05 2017 +0000
@@ -3,80 +3,67 @@
  * Ethernet connection is via an ENC28J60 Ethernet board driven by the UIPEthernet library
  */
 #include "mbed.h"
-#include <UIPEthernet.h>
-#include <UIPServer.h>
-#include <UIPClient.h>
+#include "UIPEthernet.h"
+#include "UIPServer.h"
+#include "UIPClient.h"
+
+#define DHCP    1   // if you'd like to use static IP address comment out this line
+
+// MAC address must be unique within the connected network. Modify as appropriate.
+const uint8_t   MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+const uint16_t  MY_PORT = 80;   // for HTTP connection
 
 Serial  pc(USBTX, USBRX);
 
-#define DHCP    1   // comment out this line if you'd like to use static IP address
-
-// 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
-#elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F401RE) \
-   || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) \
-   || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
+UIPEthernet    uIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
+#elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8)  \
+   || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8)  \
+   || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB)  \
+   || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB)  \
    || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
    || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
    || defined(TARGET_NRF51822) \
    || defined(TARGET_RZ_A1H)
-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)
-// In case you'd like to use static IP address:
-// IP address must be unique and compatible with your network.
-// Change as appropriate.
-const IPAddress MY_IP(192, 168, 1, 181);
+UIPEthernet    uIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
 #endif
 
-const uint16_t  MY_PORT = 80;   // for HTTP connection
 EthernetServer  myServer = EthernetServer(MY_PORT);
 
-/**
- * @brief
- * @note
- * @param
- * @retval
- */
-int main(void) {
-
+int main(void)
+{
 #if defined(DHCP)
     pc.printf("Searching for DHCP server..\r\n");
- 
-    if(UIPEthernet.begin(MY_MAC) != 1) {
+    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]);
+    pc.printf("DHCP server found.\r\n");
 #else
-    UIPEthernet.begin(MY_MAC, MY_IP);
+    // IP address must be unique and compatible with your network.
+    const IPAddress MY_IP(192, 168, 1, 181);    //  Change as appropriate.
+
+    uIPEthernet.begin(MY_MAC, MY_IP);
 #endif
 
+    pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString());
     myServer.begin();
-    while(1) {
+
+    while (1) {
+        char            echoHeader[256];
         EthernetClient  client = myServer.available();
-        if(client) {
+
+        if (client) {
             size_t  size = client.available();
-            if(size > 0) {
+
+            if (size > 0) {
                 char*   buf = (char*)malloc(size);
+
                 size = client.read((uint8_t*)buf, size);
-                if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') {
+                if (buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T') {
                     pc.printf("GET request received:\n\r");
                     pc.printf(buf);
-                    char    echoHeader[256] = {};
                     sprintf
                     (
                         echoHeader,
@@ -87,6 +74,7 @@
                     client.write((uint8_t*)buf, size);
                     pc.printf("Echo done.\r\n");
                 }
+
                 free(buf);
             }
         }