---
title: "(W5500)TCP Client Connect not working with Arduino Library"
url: "https://maker.wiznet.io/forum/12451"
markdown_url: "https://maker.wiznet.io/forum/12451/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "telliottosceola"
created: "2017-09-01T16:50:32+09:00"
last_activity: "2019-12-03T15:41:31+09:00"
language: "en"
views: 6082
replies: 24
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# (W5500)TCP Client Connect not working with Arduino Library

## Question

Asked by telliottosceola on 2017-09-01 in Ethernet Chips.

Hi Everyone,

I’ve been working with the W5500 for a couple of weeks now and everything was going quite well until yesterday. I ported the Wiznet W5500 Arduino library over to Particle for use with Particle Photon and Electron modules. I tested DHCP which worked well and I tested UDP which worked well for sending out broadcast data on the network.

Yesterday I decided to try out TCP clients and connect the W5500 to a server running via Netcat on my Mac. It’s not working yet.

The EthernetClient.connect(IP, port) function always returns 0. It goes into a while loop to check that status() returns SnSR::Established but it never does. status() always returns 0x13 which is SnSR::INIT Here is a block of my code which basically adds print debugs to the connect function:

```
int EthernetClient::connect(IPAddress ip, uint16_t port, network_interface_t) {
	Serial.println("in connect function");
	if (_sock != MAX_SOCK_NUM){
		Serial.println("_sock != MAX_SOCK_NUM");
		return 0;
	}
	Serial.println("Here");

	for (int i = 0; i < MAX_SOCK_NUM; i++) {
		Serial.println("Inside For Loop");
		uint8_t s = w5500.readSnSR(i);
		if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
			_sock = i;
			break;
		}
	}
	Serial.println("After for loop in connect");

	if (_sock == MAX_SOCK_NUM){
		Serial.println("_sock != MAX_SOCK_NUM, after loop");
		return 0;
	}

	_srcport++;
	if (_srcport == 0){
		_srcport = 1024;
	}
	socket(_sock, SnMR::TCP, _srcport, 0);

	delay(1000);

	byte* lA = (byte*)&ip.raw().ipv4;
	Serial.printf("Attempting to connect to: %i.%i.%i.%i on port %i", lA[3], lA[2], lA[1], lA[0], port);
	if (!::connect(_sock, lA, port)) {
		_sock = MAX_SOCK_NUM;
		return 0;
	}

	while (status() != SnSR::ESTABLISHED) {
		delay(10);
		uint8_t state = status();
		if (state == SnSR::CLOSED) {
			_sock = MAX_SOCK_NUM;
			Serial.println("Got here");
			return 0;
		}else{
			Serial.printf("status() = %02X \n", state);
		}
	}

	return 1;
}
```

My serial console shows status() = 13 repeatedly until got here prints and the connection fails. Any ideas on this? I can tell you the Particle module runs faster than the Arduino processor from previous experience. Could this be an issue?

## Replies

### Reply 1 by Eugeny, 2017-09-01

I do not immediately see anything wrong with the code. 0x13 is the status after socket is open, and as connect returns with 0x13, I guess it can not/does not proceed to connect. The only educated guess I have is that you pass variable `port` as 0. I am not sure, I had the case before that connect does not work if destination port is 0.

### Reply 2 by telliottosceola, 2017-09-01 (in reply to reply 1)

Thanks for the reply [@Eugeny](https://maker.wiznet.io/u/eugeny)

I have the destination port set to 4000. I also added debug code to the connect function which reads back the destination IP and port from the W5500 class. It prints out the correct IP and port number. See revised connect function here where I check the destination IP and port:
int EthernetClient::connect(IPAddress ip, uint16_t port, network_interface_t) {
Serial.println(“in connect function”);
if (_sock != MAX_SOCK_NUM){
Serial.println(“_sock != MAX_SOCK_NUM”);
return 0;
}
Serial.println(“Here”);

```
	for (int i = 0; i < MAX_SOCK_NUM; i++) {
		Serial.println("Inside For Loop");
		uint8_t s = w5500.readSnSR(i);
		if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
			_sock = i;
			break;
		}
	}
	Serial.println("After for loop in connect");

	if (_sock == MAX_SOCK_NUM){
		Serial.println("_sock != MAX_SOCK_NUM, after loop");
		return 0;
	}

	_srcport++;
	if (_srcport == 0){
		_srcport = 1024;
	}
	socket(_sock, SnMR::TCP, _srcport, 0);

	delay(1000);

	byte* lA = (byte*)&ip.raw().ipv4;
	Serial.printf("Attempting to connect to: %i.%i.%i.%i on port %i", lA[3], lA[2], lA[1], lA[0], port);
	if (!::connect(_sock, lA, port)) {
		_sock = MAX_SOCK_NUM;
		return 0;
	}

	uint8_t ipArray[4];
	w5500.readSnDIPR(_sock, ipArray);
	uint16_t setPort = w5500.readSnDPORT(_sock);
	Serial.printf("_sock IP and port: %i.%i.%i.%i on port %i", ipArray[3], ipArray[2], ipArray[1], ipArray[0], setPort);

	while (status() != SnSR::ESTABLISHED) {
		delay(10);
		uint8_t state = status();
		if (state == SnSR::CLOSED) {
			_sock = MAX_SOCK_NUM;
			Serial.println("Got here");
			return 0;
		}else{
			Serial.printf("status() = %02X \n", state);
		}
	}

	return 1;
}
```

It prints out correctly as 192.168.1.53 which is my computer’s local IP on the network and port 4000 which is the port I’m listening on. So it seems the IP and port of the server is set correctly but it’s just never succeeding. I also cannot get the TCP server to work on the W5500 either.

### Reply 3 by Eugeny, 2017-09-01

Do you check for interrupt flags - do they change at least?
It seems `connect` function needs to be troubleshot. It would help if you can point to its code.

### Reply 4 by telliottosceola, 2017-09-01

My full port of the Arduino Library thus far is available on this github repo:

![](https://github.githubassets.com/favicons/favicon.svg) [GitHub](https://github.com/ControlEverythingCom/Wiznet-W5500-Particle)

![](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/8/828c3d4553f2e0d9ae86b93fecba5228e572edab.png)

### [GitHub - ControlEverythingCom/Wiznet-W5500-Particle: Particle library for...](https://github.com/ControlEverythingCom/Wiznet-W5500-Particle)

Particle library for Wiznet W5500. Contribute to ControlEverythingCom/Wiznet-W5500-Particle development by creating an account on GitHub.

Not sure where to check interrupt flags or which ones you are refering to. Can you elaborate? Thank you very much for your help/interest to this point.

### Reply 5 by Eugeny, 2017-09-02

Everything seems to be ok. Please perform dump of the register values of the common register block and socket’s register block. You can use

`uint8_t W5500Class::read(uint16_t _addr, uint8_t _cb)`

function for it. Common register addresses are 0x00 to 0x39, socket register addresses are 0x00 to 0x2f, CB parameter is set as:

- 0x01 for common register block

- 0x09 for socket # 0 (you did not mention which socket you use).

### Reply 6 by telliottosceola, 2017-09-21

Hi [@Eugeny](https://maker.wiznet.io/u/eugeny)

Sorry for the ridiculously delayed response. I have been really tied up lately and just got back to this project.

Going through your requests here. _sock in EthernetClient::connect prints out to my serial log as 0. Is that valid?

### Reply 7 by Eugeny, 2017-09-21

![](https://avatars.discourse-cdn.com/v4/letter/t/da6949/48.png) telliottosceola:

> _sock in EthernetClient::connect prints out to my serial log as 0. Is that valid?

Why not? There’s such socket 0 and you can use it (properly ![:slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12) )

### Reply 8 by telliottosceola, 2017-09-21

![:wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=12) Thank you for that.

Can you elaborate on how you want me to read the register values you requested?

### Reply 9 by Eugeny, 2017-09-21

I am afraid to be not so familiar with libraries you use; please look into “sockets” header files, or “w5500” header file, you should be able to locate W5500 register read call (I am sure there will be comment/verbose description saying so). Then read register one by one and dump to the log.

### Reply 10 by telliottosceola, 2017-09-21

Not seeing a generic read function on the W5500 header or socket. I think what you are looking for is probably in the W5500 header(gut says so anyway). Here it is:
<https://github.com/ControlEverythingCom/Wiznet-W5500-Particle/blob/master/src/utility/w5500.h>

### Reply 11 by telliottosceola, 2017-09-21

Nix that. Found the Read function you wanted. It’s a private function.

### Reply 12 by Eugeny, 2017-09-21 (in reply to reply 11)

Yes, right these two are your friends

```
static uint8_t  read(uint16_t _addr, uint8_t _cb );
static uint16_t read(uint16_t _addr, uint8_t _cb, uint8_t *buf, uint16_t len);
```

One reads single register, another a set in a row. Please refer to the datasheet to set CB properly.

### Reply 13 by telliottosceola, 2017-09-21

Hi [@Eugeny](https://maker.wiznet.io/u/eugeny)
Forgive me if I’m being dense and this is not what you were looking for.

```
Registers for Socket 0:
Register 00 : 01
Register 01 : 00
Register 02 : 00
Register 03 : 13
Register 04 : 04
Register 05 : 01
Register 06 : FF
Register 07 : FF
Register 08 : FF
Register 09 : FF
Register 0A : FF
Register 0B : FF
Register 0C : 00
Register 0D : 00
Register 0E : 00
Register 0F : 00
Register 10 : 00
Register 11 : 00
Register 12 : 05
Register 13 : B4
Register 14 : 00
Register 15 : 00
Register 16 : 80
Register 17 : 00
Register 18 : 00
Register 19 : 00
Register 1A : 00
Register 1B : 00
Register 1C : 00
Register 1D : 00
Register 1E : 02
Register 1F : 02
Register 20 : 08
Register 21 : 00
Register 22 : 00
Register 23 : 00
Register 24 : 00
Register 25 : 00
Register 26 : 00
Register 27 : 00
Register 28 : 00
Register 29 : 00
Register 2A : 00
Register 2B : 00
Register 2C : FF
Register 2D : 40
Register 2E : 00
Register 2F : 00
Register 30 : 01
Register 31 : 00
Register 32 : 00
Register 33 : 00
Register 34 : 00
Register 35 : 00
Register 36 : 00
Register 37 : 00
Register 38 : 00
Register 39 : 00

Common Registers:
Register 00 : 00
Register 01 : 01
Register 02 : 01
Register 03 : A8
Register 04 : C0
Register 05 : 00
Register 06 : FF
Register 07 : FF
Register 08 : FF
Register 09 : DE
Register 0A : AD
Register 0B : BE
Register 0C : EF
Register 0D : FE
Register 0E : ED
Register 0F : 46
Register 10 : 01
Register 11 : A8
Register 12 : C0
Register 13 : 00
Register 14 : 00
Register 15 : 00
Register 16 : 00
Register 17 : 00
Register 18 : 00
Register 19 : 07
Register 1A : D0
Register 1B : 08
Register 1C : 28
Register 1D : 00
Register 1E : 00
Register 1F : 00
Register 20 : 00
Register 21 : 00
Register 22 : 00
Register 23 : 00
Register 24 : 00
Register 25 : 00
Register 26 : FF
Register 27 : FF
Register 28 : 00
Register 29 : 00
Register 2A : 00
Register 2B : 00
Register 2C : 00
Register 2D : 00
Register 2E : BF
Register 2F : 00
Register 30 : 00
Register 31 : 00
Register 32 : 00
Register 33 : 00
Register 34 : 00
Register 35 : 00
Register 36 : 00
Register 37 : 78
Register 38 : 25
Register 39 : 04
```

### Reply 14 by Eugeny, 2017-09-21 (in reply to reply 13)

Now you can decode info yourself, following information in datasheet:
MR = 0
Gateway = 1.1.168.192, looks very strange, I think it should be 192.168.1.1?
Network mask = 0.255.255.255 - also looks reversed?
MAC address = DE:AD:BE:EF:FE:ED (dead beef feed ![:slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12) )
W5500 IP address = 70.1.168.192 - for sure reversed

Well, I see something strange for sure. Either you get dump in wrong order, or you program registers incorrectly.

Looking to socket registers I see TCP with delayed ACK, open state, and local port 1025. Also note that first octet of local MAC address has bit 1 set.

### Reply 15 by telliottosceola, 2017-09-21

I’m seeing the same thing with all IPs being reversed. I honestly thought that was normal. This is Wiznet’s Arduino library, I really didn’t touch anything I can think of that would have reversed that. Also I can atest that DHCP is working properly which would have to make use of these reversed IPs right? Heck I don’t know.

Lol that dead beef feed was also straight out of the Arduino library samples. I can’t take comedic credit for that.

Here’s my block of code that prints out the registers. No way it’s wrong, pretty straight forward.

```
//dump registers for Socket 0.
Serial.println("Registers for Socket 0");
for(int i = 0x00; i < 0x3A; i++){
	Serial.printf("Register %02X : %02X\n", i, read(i, 0x09));
}
Serial.println();
Serial.println("Common Registers");
//dump common registers.
for(int i = 0x00; i < 0x3A; i++){
	Serial.printf("Register %02X : %02X\n", i, read(i, 0x01));
}
```

I could try passing the server IP in to the client connect function backwards and see if that works I suppose.

### Reply 16 by Eugeny, 2017-09-21

![](https://avatars.discourse-cdn.com/v4/letter/t/da6949/48.png) telliottosceola:

> I could try passing the server IP in to the client connect function backwards and see if that works I suppose.

Yes, please try doing that.

![](https://avatars.discourse-cdn.com/v4/letter/t/da6949/48.png) telliottosceola:

> Also I can atest that DHCP is working properly which would have to make use of these reversed IPs right?

With DHCP which works using UDP broadcast you actually do not need IP addresses and mask fields I guess. May it be that you read this information from DHCP response packet in wrong way?

### Reply 17 by telliottosceola, 2017-09-21

OK. I might have fibbed just a little bit earlier when I said I didn’t touch anything. The Ethernet object had calls that required raw IP addresses. The platform I’m working on “Particle” does not have that function like Arduino does so I had to get the raw address in another way. It has a ipv4 function that returns the IP in raw form but as luck would have it it reverses the order of the array for some really stupid reason. Anyway I went through and reversed the array everywhere in the Wiznet library and viola Connected to Server. Pretty stocked at this point.

[@Eugeny](https://maker.wiznet.io/u/eugeny) can’t thank you enough for all the help. Hopefully it’s smooth sailing from here on out(it never is so I’ll talk to you soon ![:wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=12)

### Reply 18 by Voha, 2019-11-23

Hello telliottosceola! Is your project “ethernet client secure” working? How can I contact you? Or send me an email: 888voha@gmail.com
Thanks!

### Reply 19 by telliottosceola, 2019-11-27

Hi [@Voha](https://maker.wiznet.io/u/voha)

Yes, I did complete the port to Particle and developed a library for Particle here which works with the Photon. The forum actually will not let me post a link to the repo for some reason but just search GitHub for Wiznet-W5500-Particle
If you are using [build.particle.io](http://build.particle.io) then just search libraries for NCD_Ethernet_Overlay. We developed the library to work with this Wiznet based Ethernet overlay module we manufacture.

### Reply 20 by Voha, 2019-11-28 (in reply to reply 19)

![](https://avatars.discourse-cdn.com/v4/letter/t/da6949/48.png) telliottosceola:

> NCD_Ethernet_Overlay

Thanks for the feedback. But I asked about working with SSL \ TLS connection. I looked through the libraries you wrote about, but they don’t have support for secure connections?

### Reply 21 by telliottosceola, 2019-12-02

Unfortunately no, they do not support SSL/TLS. This would require integrating with a TLS library such as Tropic SSL or possibly MBED TLS. Maybe there is a TLS/SSL library out there which a Client object created by the Wiznet library could be passed to.

### Reply 22 by Mxyxixptlick, 2019-12-03

wolfSSL supports SSL/TLS. I’ve integrated it with a W5500-based system running on a Cortex-M4 based system. Their software is open-source, but there’s a license fee for commercial use.

![](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/a/af7d3872c3e24a2beb231be8ecc89272d7c2d90b.png) [wolfssl.com](https://www.wolfssl.com/)

### [wolfSSL Embedded SSL/TLS Library | Now Supporting TLS 1.3](https://www.wolfssl.com/)

wolfSSL is an embedded SSL/TLS library providing secure communication for IoT, smart grid, connected home, routers, applications, games, phones, and more.

### Reply 23 by telliottosceola, 2019-12-03 (in reply to reply 22)

Ah, yes, how could I forget Wolf ![:wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=12) Do you happen to know of a good integration of Wolf SSL for Espressif by any chance? I’d like to connect a W5500 to an ESP32 and support MQTTS connections to cloud services over an ethernet connection. The ESP32 is really just there for a soft AP web interface for configuration and processor power really.

### Reply 24 by Mxyxixptlick, 2019-12-03

I haven’t personally worked with Espressif, but it appears that they support it. Just enable the definition of WOLFSSL_ESP32WROOM32_CRYPT in user_settings.h. They have a good forum that their employees monitor regularly, should you run into problems.

---

Source: https://maker.wiznet.io/forum/12451
