Zoltan Hudak / Mbed OS TcpClient_ENC28J60

Dependencies:   UIPEthernet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * TcpClient example using the UIPEthernet library for ENC28J60 Ethernet boards.
00003  *
00004  */
00005 #include "mbed.h"
00006 #include "UipEthernet.h"
00007 #include "TcpClient.h"
00008 
00009 #define IP      "192.168.1.35"
00010 #define GATEWAY "192.168.1.1"
00011 #define NETMASK "255.255.255.0"
00012 
00013 const uint8_t   MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
00014 UipEthernet     net(MAC, D11, D12, D13, D10);   // mac, mosi, miso, sck, cs
00015 
00016 /**
00017  * @brief
00018  * @note
00019  * @param
00020  * @retval
00021  */
00022 int main()
00023 {
00024     const time_t    TIMEOUT = 5;    // Connection timeout time
00025     time_t          timeOut;
00026     char            data[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
00027     char*           remaining;
00028     uint8_t*        recvBuf;
00029     int             result;
00030 
00031     printf("Starting ...\r\n");
00032 
00033     //net.set_network(IP, NETMASK, GATEWAY);  // include this to use static IP address
00034     net.connect();
00035 
00036     // Show the network address
00037     const char*     ip = net.get_ip_address();
00038     const char*     netmask = net.get_netmask();
00039     const char*     gateway = net.get_gateway();
00040     printf("IP address: %s\n", ip ? ip : "None");
00041     printf("Netmask: %s\n", netmask ? netmask : "None");
00042     printf("Gateway: %s\n", gateway ? gateway : "None");
00043 
00044     // Open a socket on the network interface, and create a TCP connection to ifconfig.io
00045     TcpClient   socket;
00046 
00047     result = socket.open(&net);
00048     if (result != 0) {
00049         printf("Error! socket.open() returned: %d\n", result);
00050     }
00051 
00052     timeOut = time(NULL) + TIMEOUT;
00053     printf("Connecting to the 'ifconfig.io' server ...\r\n");
00054 
00055     result = socket.connect("ifconfig.io", 80);
00056     if (result != 0) {
00057         printf("Error! socket.connect() returned: %d\n", result);
00058         goto DISCONNECT;
00059     }
00060 
00061     printf("Server connected.\r\n");
00062     printf("Sending data to server:\r\n");
00063     remaining = data;
00064     result = strlen(remaining);
00065     while (result) {
00066         result = socket.send((uint8_t*)remaining, strlen(remaining));
00067         if (result < 0) {
00068             printf("Error! socket.send() returned: %d\n", result);
00069             goto DISCONNECT;
00070         }
00071         printf("%.*s", result, remaining);
00072         remaining += result;
00073     }
00074 
00075     printf("Waiting for data from server:\r\n");
00076     while (socket.available() == 0) {
00077         if (time(NULL) > timeOut) {
00078             printf("Connection time out.\r\n");
00079             goto DISCONNECT;
00080         }
00081     }
00082 
00083     printf("Data received:\r\n");
00084     while ((result = socket.available()) > 0) {
00085         recvBuf = (uint8_t*)malloc(result);
00086         result = socket.recv(recvBuf, result);
00087         if (result < 0) {
00088             printf("Error! socket.recv() returned: %d\n", result);
00089             goto DISCONNECT;
00090         }
00091         printf("%.*s\r\n", result, recvBuf);
00092         free(recvBuf);
00093     }
00094 
00095     printf("\r\n");
00096 
00097 DISCONNECT:
00098     // Close the socket to return its memory and bring down the network interface
00099     socket.close();
00100 
00101     // Bring down the ethernet interface
00102     net.disconnect();
00103     printf("Done\n");
00104 }