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

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Fri Aug 30 14:14:59 2019 +0000
Parent:
6:c5eb31c60c8f
Child:
8:9e3b50f6dc81
Commit message:
Mbed OS 5 enabled.

Changed in this revision

UIPEthernet.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- a/UIPEthernet.lib	Fri Jun 30 19:57:05 2017 +0000
+++ b/UIPEthernet.lib	Fri Aug 30 14:14:59 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/hudakz/code/UIPEthernet/#4acb22344932
+http://mbed.org/users/hudakz/code/UIPEthernet/#647d53d146f1
--- a/main.cpp	Fri Jun 30 19:57:05 2017 +0000
+++ b/main.cpp	Fri Aug 30 14:14:59 2019 +0000
@@ -3,80 +3,73 @@
  * 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"
-
-#define DHCP    1   // if you'd like to use static IP address comment out this line
+#include "UipEthernet.h"
+#include "TcpServer.h"
+#include "TcpClient.h"
 
-// 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 IP      "192.168.1.35"
+#define GATEWAY "192.168.1.1"
+#define NETMASK "255.255.255.0"
+#define PORT    80
 
-#if defined(TARGET_LPC1768)
-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)
-UIPEthernet    uIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
-#endif
-
-EthernetServer  myServer = EthernetServer(MY_PORT);
-
+const uint8_t   MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+UipEthernet     net(MAC, D11, D12, D13, D10);   // mac, mosi, miso, sck, cs
+TcpServer       server;                         // Ethernet server
+TcpClient*      client;
+char            receiveBuf[1024];
+char            echoHeader[256];
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 int main(void)
 {
-#if defined(DHCP)
-    pc.printf("Searching for DHCP server..\r\n");
-    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.\r\n");
-#else
-    // IP address must be unique and compatible with your network.
-    const IPAddress MY_IP(192, 168, 1, 181);    //  Change as appropriate.
+    printf("Starting ...\r\n");
+
+    //net.set_network(IP, NETMASK, GATEWAY);  // include this to use static IP address
+    net.connect();
+
+    // Show the network address
+    const char*     ip = net.get_ip_address();
+    const char*     netmask = net.get_netmask();
+    const char*     gateway = net.get_gateway();
 
-    uIPEthernet.begin(MY_MAC, MY_IP);
-#endif
+    printf("IP address: %s\r\n", ip ? ip : "None");
+    printf("Netmask: %s\r\n", netmask ? netmask : "None");
+    printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
 
-    pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString());
-    myServer.begin();
+    /* Open the server on ethernet stack */
+    server.open(&net);
+
+    /* Bind the HTTP port (TCP 80) to the server */
+    server.bind(PORT);
+
+    /* Can handle 5 simultaneous connections */
+    server.listen(5);
 
     while (1) {
-        char            echoHeader[256];
-        EthernetClient  client = myServer.available();
+        client = server.accept();
 
         if (client) {
-            size_t  size = client.available();
-
-            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') {
-                    pc.printf("GET request received:\n\r");
-                    pc.printf(buf);
-                    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);
-                    pc.printf("Echo done.\r\n");
-                }
-
-                free(buf);
+            size_t receiveLen = client->recv((uint8_t*)receiveBuf, client->available());
+            if (receiveBuf[0] == 'G' && receiveBuf[1] == 'E' && receiveBuf[2] == 'T') {
+                printf("\r\n-------------------------------------------------------\r\n");
+                printf("GET request received from a client with IP address %s\n\r", client->getpeername());
+                printf("%s\r\n", receiveBuf);
+                sprintf
+                (
+                    echoHeader,
+                    "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text\r\nConnection: About to close\r\n\r\n",
+                    receiveLen
+                );
+                client->send((uint8_t*)echoHeader, strlen(echoHeader));
+                client->send((uint8_t*)receiveBuf, receiveLen);
+                printf("Echo done.\r\n");
             }
         }
+
+        client->close();
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Aug 30 14:14:59 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#1bf6b20df9d3cd5f29f001ffc6f0d0fcbbb96118
--- a/mbed.bld	Fri Jun 30 19:57:05 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/64910690c574
\ No newline at end of file