Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Dependencies:   UIPEthernet

Revision:
3:9c32e3375fc5
Parent:
2:5656f7efd7c7
Child:
4:9f42e50733be
--- a/main.cpp	Fri Jun 30 20:18:02 2017 +0000
+++ b/main.cpp	Sat Aug 31 20:48:02 2019 +0000
@@ -1,98 +1,99 @@
 /*
- * Simple TcpClient using the UIPEthernet library for ENC28J60 Ethernet boards.
+ * TcpClient example using the UIPEthernet library for ENC28J60 Ethernet boards.
  *
  */
+#include "mbed.h"
+#include "UipEthernet.h"
+#include "TcpClient.h"
 
-#include "mbed.h"
-#include "UIPEthernet.h"
-#include "UIPServer.h"
-#include "UIPClient.h"
-
-Serial  pc(USBTX, USBRX);
+const uint8_t   MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+UipEthernet     net(MAC, D11, D12, D13, D10);   // mac, mosi, miso, sck, cs
 
-#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
-
-// MAC address must be unique within the connected network. Modify as appropriate.
-const uint8_t   MY_MAC[6] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
-EthernetClient  client;
-
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 int main()
 {
-    const unsigned long MAXTIME = 5;                    // Connection timeout time
-    unsigned long       t = 0;
-    unsigned long       timeOut = 0;
-    const IPAddress     SERVER_IP(192, 168, 1, 33);     // Change as appropriate.
-    const uint16_t      SERVER_PORT = 10001;
-    
-    float   pi = 3.1416F;  // some data to be sent to the server
-    uint8_t *data = reinterpret_cast<uint8_t*>(&pi);
+    const time_t    TIMEOUT = 5;    // Connection timeout time
+    time_t          timeOut;
+    char            data[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
+    char*           remaining;
+    uint8_t*        recvBuf;
+    int             result;
+
+    printf("Starting ...\r\n");
+
+    net.connect();
 
-#define DHCP    1   // if you'd like to use static IP address comment out this line 
-#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;
+    // Show the network address
+    const char*     ip = net.get_ip_address();
+    const char*     netmask = net.get_netmask();
+    const char*     gateway = net.get_gateway();
+    printf("IP address: %s\n", ip ? ip : "None");
+    printf("Netmask: %s\n", netmask ? netmask : "None");
+    printf("Gateway: %s\n", gateway ? gateway : "None");
+
+    // Open a socket on the network interface, and create a TCP connection to ifconfig.io
+    TcpClient   socket;
+
+    result = socket.open(&net);
+    if (result != 0) {
+        printf("Error! socket.open() returned: %d\n", result);
     }
-    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.
-    uIPEthernet.begin(MY_MAC, MY_IP);
-#endif
-    IPAddress   localIP = uIPEthernet.localIP();
-    pc.printf("Local IP = %s\r\n", localIP.toString());
 
-    while(1) {
-        if(time(NULL) > timeOut) {
-            timeOut = time(NULL) + MAXTIME;
-            pc.printf("Connecting to TCP/IP server ..\r\n");
-            if(client.connect(SERVER_IP, SERVER_PORT)) {
-                pc.printf("Server connected.\r\n");
-                pc.printf("Sending some data to server:\r\n");
-                client.write(data, sizeof(pi));
-                for(int i = 0; i < sizeof(pi); i++)
-                    pc.printf("0x%x ", data[i]);
-                pc.printf("\r\n");
-                pc.printf("Awaiting message from server:\r\n");
-                while(client.available() == 0) {
-                    if(time(NULL) > timeOut) {
-                        pc.printf("Connection time out.\r\n");
-                        client.stop();
-                        pc.printf("Disconnected from server.\r\n");
-                        break;
-                    }
-                }
-                if(client.connected()) {
-                    pc.printf("Message received from server:\r\n");
-                    int size;
-                    while((size = client.available()) > 0) {
-                        uint8_t* msg = (uint8_t*)malloc(size);
-                        size = client.read(msg, size);
-                        for(int i = 0; i < size; i++)
-                            pc.printf("0x%x ", msg[i]);
-                        pc.printf("\r\n");
-                        free(msg);
-                    }
-                    client.stop();
-                    pc.printf("Disconnected from server.\r\n");
-                }
-            } else {
-                pc.printf("Failed to connect to server\r\n");
-            }
+    timeOut = time(NULL) + TIMEOUT;
+    printf("Connecting to the 'ifconfig.io' server ...\r\n");
+
+    result = socket.connect("ifconfig.io", 80);
+    if (result != 0) {
+        printf("Error! socket.connect() returned: %d\n", result);
+        goto DISCONNECT;
+    }
+
+    printf("Server connected.\r\n");
+    printf("Sending data to server:\r\n");
+    remaining = data;
+    result = strlen(remaining);
+    while (result) {
+        result = socket.send((uint8_t*)remaining, strlen(remaining));
+        if (result < 0) {
+            printf("Error! socket.send() returned: %d\n", result);
+            goto DISCONNECT;
+        }
+        printf("%.*s", result, remaining);
+        remaining += result;
+    }
+
+    printf("Waiting for data from server:\r\n");
+    while (socket.available() == 0) {
+        if (time(NULL) > timeOut) {
+            printf("Connection time out.\r\n");
+            goto DISCONNECT;
         }
     }
+
+    printf("Data received:\r\n");
+    while ((result = socket.available()) > 0) {
+        recvBuf = (uint8_t*)malloc(result);
+        result = socket.recv(recvBuf, result);
+        if (result < 0) {
+            printf("Error! socket.recv() returned: %d\n", result);
+            goto DISCONNECT;
+        }
+        printf("%.*s\r\n", result, recvBuf);
+        free(recvBuf);
+    }
+
+    printf("\r\n");
+
+DISCONNECT:
+    // Close the socket to return its memory and bring down the network interface
+    socket.close();
+
+    // Bring down the ethernet interface
+    net.disconnect();
+    printf("Done\n");
 }
-