Merge connected branch, bump to version 0.9.3

This commit is contained in:
Tony DiCola 2015-07-14 15:54:04 -07:00
commit 23f919adc3
8 changed files with 125 additions and 70 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

@ -94,33 +94,8 @@ void setup() {
while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Retrying WiFi")); Serial.println(F("Retrying WiFi"));
while(1); delay(1000);
} }
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;
@ -129,6 +104,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) ) {
@ -158,3 +138,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

@ -84,30 +84,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) ) {
@ -137,3 +123,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!");
}

View File

@ -1,5 +1,5 @@
name=Adafruit MQTT Library name=Adafruit MQTT Library
version=0.9.1 version=0.9.2
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware. sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware.