Zoltan Hudak
/
TcpClient_ENC28J60
Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Diff: main.cpp
- Revision:
- 2:5656f7efd7c7
- Parent:
- 1:4e4b811a7f9b
- Child:
- 3:9c32e3375fc5
--- a/main.cpp Sat Dec 05 14:42:49 2015 +0000 +++ b/main.cpp Fri Jun 30 20:18:02 2017 +0000 @@ -11,7 +11,7 @@ Serial pc(USBTX, USBRX); #if defined(TARGET_LPC1768) -UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs +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) \ @@ -20,10 +20,10 @@ || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ || defined(TARGET_NRF51822) \ || defined(TARGET_RZ_A1H) -UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs #endif -// MAC number must be unique within the connected network. Modify as appropriate. +// 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; @@ -41,7 +41,7 @@ #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) { + if(uIPEthernet.begin(MY_MAC) != 1) { pc.printf("No DHCP server found.\r\n"); pc.printf("Exiting application.\r\n"); return 0; @@ -50,13 +50,10 @@ #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); + uIPEthernet.begin(MY_MAC, MY_IP); #endif - 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]); + IPAddress localIP = uIPEthernet.localIP(); + pc.printf("Local IP = %s\r\n", localIP.toString()); while(1) { if(time(NULL) > timeOut) { @@ -72,24 +69,26 @@ pc.printf("Awaiting message from server:\r\n"); while(client.available() == 0) { if(time(NULL) > timeOut) { - printf("Connection timed out.\r\n"); - goto close; + pc.printf("Connection time out.\r\n"); + client.stop(); + pc.printf("Disconnected from server.\r\n"); + break; } } - 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); + 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"); } -close: - //disconnect client - client.stop(); - pc.printf("Disconnected from server.\r\n"); } else { pc.printf("Failed to connect to server\r\n"); }