Add Adfruit_MQTT::connectErrorString() method

This easily allows printing error messages without having to hard-code
return values in the sketch and makes the example sketches a bit more
concise.
This commit is contained in:
Matthijs Kooijman 2015-07-02 13:01:05 +02:00
parent 73f5be4e5c
commit 6879df86f6
5 changed files with 25 additions and 34 deletions

View File

@ -140,6 +140,19 @@ int8_t Adafruit_MQTT::connect() {
return 0; return 0;
} }
const __FlashStringHelper* Adafruit_MQTT::connectErrorString(int8_t code) {
switch (code) {
case 1: return F("Wrong protocol");
case 2: return F("ID rejected");
case 3: return F("Server unavail");
case 4: return F("Bad user/pass");
case 5: return F("Not authed");
case 6: return F("Failed to subscribe");
case -1: return F("Connection failed");
default: return F("Unknown error");
}
}
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) { bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
// Construct and send publish packet. // Construct and send publish packet.
uint8_t len = publishPacket(buffer, topic, data, qos); uint8_t len = publishPacket(buffer, topic, data, qos);

View File

@ -105,8 +105,16 @@ class Adafruit_MQTT {
// 4 = Bad username or password // 4 = Bad username or password
// 5 = Not authenticated // 5 = Not authenticated
// 6 = Failed to subscribe // 6 = Failed to subscribe
// Use connectErrorString() to get a printable string version of the
// error.
int8_t connect(); int8_t connect();
// Return a printable string version of the error code returned by
// connect(). This returns a __FlashStringHelper*, which points to a
// string stored in flash, but can be directly passed to e.g.
// Serial.println without any further processing.
const __FlashStringHelper* connectErrorString(int8_t code);
// Disconnect from the MQTT server. Returns true if disconnected, false // Disconnect from the MQTT server. Returns true if disconnected, false
// otherwise. // otherwise.
virtual bool disconnect() = 0; // Subclasses need to fill this in! virtual bool disconnect() = 0; // Subclasses need to fill this in!

View File

@ -152,18 +152,9 @@ void MQTT_connect() {
Serial.print("Connecting to MQTT... "); Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
switch (ret) { Serial.println(mqtt.connectErrorString(ret));
case 1: Serial.println("Wrong protocol"); break; if (ret < 0)
case 2: Serial.println("ID rejected"); break;
case 3: Serial.println("Server unavailable"); break;
case 4: Serial.println("Bad user/password"); break;
case 5: Serial.println("Not authenticated"); break;
case 6: Serial.println("Failed to subscribe"); break;
default:
Serial.println(F("Connection failed"));
CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again
break;
}
Serial.println("Retrying MQTT connection in 5 seconds..."); Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect(); mqtt.disconnect();
delay(5000); // wait 5 seconds delay(5000); // wait 5 seconds

View File

@ -137,17 +137,7 @@ void MQTT_connect() {
Serial.print("Connecting to MQTT... "); Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
switch (ret) { Serial.println(mqtt.connectErrorString(ret));
case 1: Serial.println("Wrong protocol"); break;
case 2: Serial.println("ID rejected"); break;
case 3: Serial.println("Server unavailable"); break;
case 4: Serial.println("Bad user/password"); break;
case 5: Serial.println("Not authenticated"); break;
case 6: Serial.println("Failed to subscribe"); break;
default: Serial.print("Couldn't connect to server, code: ");
Serial.println(ret);
break;
}
Serial.println("Retrying MQTT connection in 5 seconds..."); Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect(); mqtt.disconnect();
delay(5000); // wait 5 seconds delay(5000); // wait 5 seconds

View File

@ -122,18 +122,7 @@ void loop() {
Serial.println(F("Connecting to MQTT...")); Serial.println(F("Connecting to MQTT..."));
int8_t ret, retries = 5; int8_t ret, retries = 5;
while (retries && (ret = mqtt.connect()) != 0) { while (retries && (ret = mqtt.connect()) != 0) {
switch (ret) { Serial.println(mqtt.connectErrorString(ret));
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: {
Serial.println(F("Connection failed"));
break;
}
}
Serial.println(F("Retrying MQTT connection")); Serial.println(F("Retrying MQTT connection"));
retries--; retries--;
if (retries == 0) halt("Resetting system"); if (retries == 0) halt("Resetting system");