The code for PubSubClient::connected has a logic error related to mixing purposes of the return value.
The code below is taken from the 2.8 release. The method is supposed to return a boolean. If the client is NULL will return false.
If the client is not NULL and (int)_client->connected() returns a non-zero value (which means it is connected), then it will go to the else clause and return MQTT_CONNECTED which is defined as a 0.
false and 0 are equivents. Non-zero is true. So MQTT_CONNECTED is essentially defined as false, so when you use boolean PubSubClient::connected() to determine if you are connected you will always get a false.
The quick hack would be to change the defines so that MQTT_CONNECTED is not ZERO, but that would just perpetuate the back coding practice of mixing ints and bools. A more straight forward solution would be to change the contract of this method and have it return an int or add new method called connectoinStatus that returns values from this list:
The code for PubSubClient::connected has a logic error related to mixing purposes of the return value.
The code below is taken from the 2.8 release. The method is supposed to return a boolean. If the client is
NULL
will return false. If the client is notNULL
and(int)_client->connected()
returns a non-zero value (which means it is connected), then it will go to theelse
clause and returnMQTT_CONNECTED
which is defined as a 0.false and 0 are equivents. Non-zero is true. So
MQTT_CONNECTED
is essentially defined as false, so when you useboolean PubSubClient::connected()
to determine if you are connected you will always get a false.The quick hack would be to change the defines so that
MQTT_CONNECTED
is not ZERO, but that would just perpetuate the back coding practice of mixing ints and bools. A more straight forward solution would be to change the contract of this method and have it return an int or add new method called connectoinStatus that returns values from this list: