Add connected() function to MQTT class. Update examples with robust reconnect logic.

This commit is contained in:
Tony DiCola 2015-07-05 14:29:13 -07:00
parent 207bcd44dd
commit 643c906aa6
7 changed files with 148 additions and 93 deletions

View File

@ -111,6 +111,9 @@ class Adafruit_MQTT {
// otherwise. // otherwise.
virtual bool disconnect() = 0; // Subclasses need to fill this in! virtual bool disconnect() = 0; // Subclasses need to fill this in!
// Return true if connected to the MQTT server, otherwise false.
virtual bool connected() = 0; // Subclasses need to fill this in!
// Publish a message to a topic using the specified QoS level. Returns true // Publish a message to a topic using the specified QoS level. Returns true
// if the message was published, false otherwise. // if the message was published, false otherwise.
bool publish(const char *topic, char *payload, uint8_t qos); bool publish(const char *topic, char *payload, uint8_t qos);

View File

@ -82,7 +82,16 @@ class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
} }
bool disconnect() { bool disconnect() {
return (mqttclient.close() == 0); if (connected()) {
return (mqttclient.close() == 0);
}
else {
return true;
}
}
bool connected() {
return mqttclient.connected();
} }
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout, uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,

View File

@ -34,11 +34,19 @@ bool Adafruit_MQTT_Client::connectServer() {
} }
bool Adafruit_MQTT_Client::disconnect() { bool Adafruit_MQTT_Client::disconnect() {
// Stop connection and return success (stop has no indication of failure). // Stop connection if connected and return success (stop has no indication of
client->stop(); // failure).
if (client->connected()) {
client->stop();
}
return true; return true;
} }
bool Adafruit_MQTT_Client::connected() {
// Return true if connected, false if not connected.
return client->connected();
}
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
int16_t timeout, int16_t timeout,
bool checkForValidPubPacket) { bool checkForValidPubPacket) {

View File

@ -44,6 +44,7 @@ class Adafruit_MQTT_Client : public Adafruit_MQTT {
bool connectServer(); bool connectServer();
bool disconnect(); bool disconnect();
bool connected();
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout, uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false); bool checkForValidPubPacket = false);
bool sendPacket(uint8_t *buffer, uint8_t len); bool sendPacket(uint8_t *buffer, uint8_t len);

View File

@ -55,6 +55,11 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
return fona->TCPclose(); return fona->TCPclose();
} }
bool connected() {
// Return true if connected, false if not connected.
return fona->TCPconnected();
}
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout, uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false) { bool checkForValidPubPacket = false) {
uint8_t *buffp = buffer; uint8_t *buffp = buffer;

View File

@ -93,31 +93,6 @@ void setup() {
Serial.println(F("Retrying WiFi")); Serial.println(F("Retrying WiFi"));
while(1); while(1);
} }
Serial.println(F("Connected to WiFi!"));
//////////////////////////////
Serial.println(F("Connecting to MQTT..."));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (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"));
CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again
return; // restart the loop
}
}
Serial.println(F("Retrying MQTT connection"));
delay(5000);
}
//////////////////////////////
Serial.println(F("MQTT Connected!"));
} }
uint32_t x=0; uint32_t x=0;
@ -126,6 +101,11 @@ void loop() {
// Make sure to reset watchdog every loop iteration! // Make sure to reset watchdog every loop iteration!
Watchdog.reset(); Watchdog.reset();
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Try to ping the MQTT server // Try to ping the MQTT server
/* /*
if (! mqtt.ping(3) ) { if (! mqtt.ping(3) ) {
@ -155,3 +135,35 @@ void loop() {
Serial.println(F("OK!")); Serial.println(F("OK!"));
} }
} }
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
switch (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.println(F("Connection failed"));
CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again
break;
}
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}

View File

@ -81,30 +81,16 @@ void setup() {
// Setup MQTT subscription for onoff feed. // Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoffbutton); mqtt.subscribe(&onoffbutton);
// Connect to MQTT server.
Serial.println(F("Connecting to MQTT..."));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (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("Couldn't connect to MQTT server")); break;
}
Serial.println(F("Retrying MQTT connection"));
mqtt.disconnect();
delay(5000);
}
Serial.println(F("MQTT Connected!"));
} }
uint32_t x=0; uint32_t x=0;
void loop() { void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Try to ping the MQTT server // Try to ping the MQTT server
/* /*
if (! mqtt.ping(3) ) { if (! mqtt.ping(3) ) {
@ -134,3 +120,34 @@ void loop() {
delay(1000); delay(1000);
} }
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
switch (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...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}