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

Dependencies:   UIPEthernet

Committer:
hudakz
Date:
Tue Sep 03 09:58:30 2019 +0000
Revision:
4:9f42e50733be
Parent:
3:9c32e3375fc5
Updated to use the latest version of the UIPEthernet library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:5e91bb901022 1 /*
hudakz 3:9c32e3375fc5 2 * TcpClient example using the UIPEthernet library for ENC28J60 Ethernet boards.
hudakz 0:5e91bb901022 3 *
hudakz 0:5e91bb901022 4 */
hudakz 3:9c32e3375fc5 5 #include "mbed.h"
hudakz 3:9c32e3375fc5 6 #include "UipEthernet.h"
hudakz 3:9c32e3375fc5 7 #include "TcpClient.h"
hudakz 0:5e91bb901022 8
hudakz 4:9f42e50733be 9 #define IP "192.168.1.35"
hudakz 4:9f42e50733be 10 #define GATEWAY "192.168.1.1"
hudakz 4:9f42e50733be 11 #define NETMASK "255.255.255.0"
hudakz 4:9f42e50733be 12
hudakz 3:9c32e3375fc5 13 const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 3:9c32e3375fc5 14 UipEthernet net(MAC, D11, D12, D13, D10); // mac, mosi, miso, sck, cs
hudakz 0:5e91bb901022 15
hudakz 3:9c32e3375fc5 16 /**
hudakz 3:9c32e3375fc5 17 * @brief
hudakz 3:9c32e3375fc5 18 * @note
hudakz 3:9c32e3375fc5 19 * @param
hudakz 3:9c32e3375fc5 20 * @retval
hudakz 3:9c32e3375fc5 21 */
hudakz 0:5e91bb901022 22 int main()
hudakz 0:5e91bb901022 23 {
hudakz 3:9c32e3375fc5 24 const time_t TIMEOUT = 5; // Connection timeout time
hudakz 3:9c32e3375fc5 25 time_t timeOut;
hudakz 3:9c32e3375fc5 26 char data[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
hudakz 3:9c32e3375fc5 27 char* remaining;
hudakz 3:9c32e3375fc5 28 uint8_t* recvBuf;
hudakz 3:9c32e3375fc5 29 int result;
hudakz 3:9c32e3375fc5 30
hudakz 3:9c32e3375fc5 31 printf("Starting ...\r\n");
hudakz 3:9c32e3375fc5 32
hudakz 4:9f42e50733be 33 //net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address
hudakz 3:9c32e3375fc5 34 net.connect();
hudakz 0:5e91bb901022 35
hudakz 3:9c32e3375fc5 36 // Show the network address
hudakz 3:9c32e3375fc5 37 const char* ip = net.get_ip_address();
hudakz 3:9c32e3375fc5 38 const char* netmask = net.get_netmask();
hudakz 3:9c32e3375fc5 39 const char* gateway = net.get_gateway();
hudakz 3:9c32e3375fc5 40 printf("IP address: %s\n", ip ? ip : "None");
hudakz 3:9c32e3375fc5 41 printf("Netmask: %s\n", netmask ? netmask : "None");
hudakz 3:9c32e3375fc5 42 printf("Gateway: %s\n", gateway ? gateway : "None");
hudakz 3:9c32e3375fc5 43
hudakz 3:9c32e3375fc5 44 // Open a socket on the network interface, and create a TCP connection to ifconfig.io
hudakz 3:9c32e3375fc5 45 TcpClient socket;
hudakz 3:9c32e3375fc5 46
hudakz 3:9c32e3375fc5 47 result = socket.open(&net);
hudakz 3:9c32e3375fc5 48 if (result != 0) {
hudakz 3:9c32e3375fc5 49 printf("Error! socket.open() returned: %d\n", result);
hudakz 0:5e91bb901022 50 }
hudakz 0:5e91bb901022 51
hudakz 3:9c32e3375fc5 52 timeOut = time(NULL) + TIMEOUT;
hudakz 3:9c32e3375fc5 53 printf("Connecting to the 'ifconfig.io' server ...\r\n");
hudakz 3:9c32e3375fc5 54
hudakz 3:9c32e3375fc5 55 result = socket.connect("ifconfig.io", 80);
hudakz 3:9c32e3375fc5 56 if (result != 0) {
hudakz 3:9c32e3375fc5 57 printf("Error! socket.connect() returned: %d\n", result);
hudakz 3:9c32e3375fc5 58 goto DISCONNECT;
hudakz 3:9c32e3375fc5 59 }
hudakz 3:9c32e3375fc5 60
hudakz 3:9c32e3375fc5 61 printf("Server connected.\r\n");
hudakz 3:9c32e3375fc5 62 printf("Sending data to server:\r\n");
hudakz 3:9c32e3375fc5 63 remaining = data;
hudakz 3:9c32e3375fc5 64 result = strlen(remaining);
hudakz 3:9c32e3375fc5 65 while (result) {
hudakz 3:9c32e3375fc5 66 result = socket.send((uint8_t*)remaining, strlen(remaining));
hudakz 3:9c32e3375fc5 67 if (result < 0) {
hudakz 3:9c32e3375fc5 68 printf("Error! socket.send() returned: %d\n", result);
hudakz 3:9c32e3375fc5 69 goto DISCONNECT;
hudakz 3:9c32e3375fc5 70 }
hudakz 3:9c32e3375fc5 71 printf("%.*s", result, remaining);
hudakz 3:9c32e3375fc5 72 remaining += result;
hudakz 3:9c32e3375fc5 73 }
hudakz 3:9c32e3375fc5 74
hudakz 3:9c32e3375fc5 75 printf("Waiting for data from server:\r\n");
hudakz 3:9c32e3375fc5 76 while (socket.available() == 0) {
hudakz 3:9c32e3375fc5 77 if (time(NULL) > timeOut) {
hudakz 3:9c32e3375fc5 78 printf("Connection time out.\r\n");
hudakz 3:9c32e3375fc5 79 goto DISCONNECT;
hudakz 0:5e91bb901022 80 }
hudakz 0:5e91bb901022 81 }
hudakz 3:9c32e3375fc5 82
hudakz 3:9c32e3375fc5 83 printf("Data received:\r\n");
hudakz 3:9c32e3375fc5 84 while ((result = socket.available()) > 0) {
hudakz 3:9c32e3375fc5 85 recvBuf = (uint8_t*)malloc(result);
hudakz 3:9c32e3375fc5 86 result = socket.recv(recvBuf, result);
hudakz 3:9c32e3375fc5 87 if (result < 0) {
hudakz 3:9c32e3375fc5 88 printf("Error! socket.recv() returned: %d\n", result);
hudakz 3:9c32e3375fc5 89 goto DISCONNECT;
hudakz 3:9c32e3375fc5 90 }
hudakz 3:9c32e3375fc5 91 printf("%.*s\r\n", result, recvBuf);
hudakz 3:9c32e3375fc5 92 free(recvBuf);
hudakz 3:9c32e3375fc5 93 }
hudakz 3:9c32e3375fc5 94
hudakz 3:9c32e3375fc5 95 printf("\r\n");
hudakz 3:9c32e3375fc5 96
hudakz 3:9c32e3375fc5 97 DISCONNECT:
hudakz 3:9c32e3375fc5 98 // Close the socket to return its memory and bring down the network interface
hudakz 3:9c32e3375fc5 99 socket.close();
hudakz 3:9c32e3375fc5 100
hudakz 3:9c32e3375fc5 101 // Bring down the ethernet interface
hudakz 3:9c32e3375fc5 102 net.disconnect();
hudakz 3:9c32e3375fc5 103 printf("Done\n");
hudakz 0:5e91bb901022 104 }