Chat freely about anything...

User avatar
By electronicsguy
#46122 Regarding connecting to a remote host. I am trying to understand why all example sketches use this:

Code: Select allif (!client.connect(server, port)) {
     < print error message here>
}

<continue execution here>


Since WiFiClient is derived from "Client", I am assuming it's connect() method would return the same error codes as listed here: https://www.arduino.cc/en/Reference/ClientConnect

If this is the case, the if statement will be true only for the returned code "0", which doesn't even exist in the list. Any non-zero value (including the negative values) would be treated as as true and hence the not condition would fail for these, thinking the connection is successful. (AFAIK, this is the convention in C++).

Shouldn't the if statement be this instead:
Code: Select allif (client.connect(server, port) != 1){
  <error ...>
}

Am I wrong here?
User avatar
By martinayotte
#46150 That's is true for the other Arduino platforms, but in the case of ESP8266WiFi, if you look at the code, the only possible returned values are 0 and 1.
But, Yes, the code you are showing can be consider as the "best practice", especially if you wish that your sketch been used under several platforms.
User avatar
By electronicsguy
#46182
martinayotte wrote:That's is true for the other Arduino platforms, but in the case of ESP8266WiFi, if you look at the code, the only possible returned values are 0 and 1.
But, Yes, the code you are showing can be consider as the "best practice", especially if you wish that your sketch been used under several platforms.


Ok thanks. I looked into "WiFiClient.cpp" and yes, it indeed does return only a 1 or 0.

So is there any way of getting more information, like why there is an error (timeout, nor route to host, etc) like the original Arduino Client?