Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

MQTT Library CC3000, ping fails after publishing 14 hours
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

MQTT Library CC3000, ping fails after publishing 14 hours

by aforaste1 on Wed Nov 25, 2015 11:13 pm

Hello,

I've been working through this problem for a little while now, and would appreciate any feedback/advice as to the issue. Basically, I'm reading humidity and temperature using the DHT 11, and publishing to the AIO dashboard using the MQTT library. It works fantastic for several hours, but then disconnects and does not reconnect automatically. If powered off and re-initiated, it will normally get up and running again for a period of time, but will eventually lose connection again after several hours. Have tried this several times. Here are more details on the setup:

- Adafruit CC3000 WIFI breakout board w/ external antenna
- Uno,
- IDE 1.6.6
- MQTT libarary (using updated version, downloaded on 11/24)
- Adafruit_CC3000 libarary
- Sleepydog library

The original sketch was the group sensor from the Adafruit IO basics/Arduino CC3k, however this sketch has been modified some to read and publish feeds separately, use different sensor, etc.. Also, made some other adjustments to the way it connects and pings in the loop, which seems to get it to connect to the WIFI and AIO quicker and cleaner. Note I had this same connection issue using less modified version (only changed group reading to separate feeds). This sketch will load and connect to AIO, publishing data for many hours, but eventually fails to publish, with the "MQTT ping failed" error. Here is the serial monitor output when it first begins successfully:

========================================
Adafruit IO Example:
Free RAM: 734

Init the CC3000...Firmware V. : 1.24

Deleting old connection profiles

Attempting to connect to HOME-51E8...Connected!
Requesting DHCP
Unable to retrieve the IP Address!


IP Addr: 10.0.0.2
Netmask: 255.255.255.0
Gateway: 10.0.0.1
DHCPsrv: 10.0.0.1
DNSserv: 75.75.75.75
Connected to WiFi!
Connecting to Adafruit IO... io.adafruit.com -> 199.167.40.63
Adafruit IO Connected!
yes, still connected...getting ready to read next measurements in 4 seconds...
...wait
...and measure

Sending temperature:
69.80

and
37.00

just sent to AIO
yes, still connected...getting ready to read next measurements in 4 seconds...
...wait
...and measure
=================================
The above will continue for several hours, this output began at 10:50pm Tuesday evening (11/25) and it ran successfully publishing data to the dashboard for 14 hours, until it lost connection the next afternoon around 1pm. Here is the serial output, ignore the part that says "yes, still connected" :)
=================================
and
39.00
Failed to publish temperature.
Failed to publish humidity.
MQTT Ping failed.
yes, still connected...getting ready to read next measurements in 4 seconds...
...wait
...and measure

Sending temperature:
68.00

and
39.00
Failed to publish temperature.
Failed to publish humidity.
MQTT Ping failed.
yes, still connected...getting ready to read next measurements in 4 seconds...
...wait
...and measure
==========================================

and over and over... Here is the full sketch:

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  Adafruit MQTT Library CC3000 Example

  Designed specifically to work with the Adafruit WiFi products:
  ----> https://www.adafruit.com/products/1469

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  Adafruit IO example additions by Todd Treece.
  MIT license, all text above must be included in any redistribution
 ****************************************************/
#include <Adafruit_SleepyDog.h>
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <Wire.h>
#include "utility/debug.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_CC3000.h"
#include "DHT.h"
/****************************** Pins ******************************************/

#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT  5  // VBAT & CS can be any digital pins.
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
// DHT sensor
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "HOME-51E8"  // can't be longer than 32 characters!
#define WLAN_PASS       "----password here-------"
#define WLAN_SECURITY   WLAN_SEC_WPA2  // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
                                       //         WLAN_SEC_WPA or WLAN_SEC_WPA2

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "--------user name here---------------"
#define AIO_KEY         "------------key here--------------"

/************ Global State (you don't need to change this!) ******************/

// Setup the main CC3000 class, just like a normal CC3000 sketch.
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT);

// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM  = __TIME__ AIO_USERNAME;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details.
Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1);  }

// CC3000connect is a helper function that sets up the CC3000 and connects to
// the WiFi network. See the cc3000helper.cpp tab above for the source!
boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security);

/****************************** Feeds ***************************************/

// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);

const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);

/*************************** Sketch Code ************************************/

void setup() {
 
// Init DHT sensor
  dht.begin();
  Serial.begin(115200);

  Serial.println(F("Adafruit IO Example:"));
  Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);

  // Initialise the CC3000 module
  Serial.print(F("\nInit the CC3000..."));
  if (! cc3000.begin())
    halt("Failed to Initialize CC3000");

  // attempt wifi connection
  while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Retrying WiFi"));
    //while(1);
    delay(1000);
  }
  Serial.println(F("Connected to WiFi!"));

  // connect to adafruit io
  connect();

}

//*************************************** begin loop ***********************************************
void loop() {
  // Make sure to reset watchdog every loop iteration!
  Watchdog.reset();
 
  // ping adafruit io a few times to make sure we remain connected
  //  if(! mqtt.ping(3)) {
  // reconnect to adafruit io
  //  if(! mqtt.connected())
  //  Serial.println(F("not connected...trying to reconnect..beginning of loop"));
      connect();
 // }
     Serial.println(F("yes, still connected...getting ready to read next measurements in 4 seconds..."));
 
    Serial.println(F("...wait"));
     delay(2000);
    Serial.println(F("...and measure"));
     delay(2000);
     
// **********************
 
  float t = (dht.readTemperature() * 1.8) + 32;
  float h = dht.readHumidity();

 // Now we can publish stuff!
  Serial.println(F("\nSending temperature: "));
  Serial.println(t);
  Serial.println(F("\nand"));
  Serial.println(h);
 
  if (! temperature.publish(t))
    Serial.println(F("Failed to publish temperature."));
  else
    Serial.println(F(" "));
  if (! humidity.publish(h))
    Serial.println(F("Failed to publish humidity."));
  else
    Serial.println(F("just sent to AIO"));
 
 // ping the server to keep the mqtt connection alive
  if(! mqtt.ping()) {
    Serial.println(F("MQTT Ping failed."));
  }
}

//**************************************end loop*************************************

// connect to adafruit io via MQTT
void connect() {

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {  // connect will return 0 for connected

    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);
        break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}


I'd really appreciate any insight into what might be going on and what the issue is. Also, if someone else has a different example sketch that publishes feeds to AIO that has been running smoothly and continuously for many days over the past few weeks, I'd be interested in hearing about that. Just looking to try out different approaches. Thanks!

aforaste1
 
Posts: 18
Joined: Sun Nov 01, 2015 11:40 am

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by adafruit2 on Thu Nov 26, 2015 8:51 pm

CC3000 core firmware is a litle flakey over long periods, i'd have your code do a hard reset of the CC3K and reconnect after ~5 hours. you'll be offline for 2 minutes but it might keep overall uptime higher

adafruit2
Site Admin
 
Posts: 19224
Joined: Fri Mar 11, 2005 7:36 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by aforaste1 on Thu Dec 03, 2015 9:30 am

Thank you for pointing me down this path. I wasn't aware of this issue previously, but have since become aware of it. I'm going to make some adaptations including the incorporation of a hard reset as that seems to be a key part of the solution in the end. Having a couple minutes of downtown is no problem at all for this application. Thanks.

aforaste1
 
Posts: 18
Joined: Sun Nov 01, 2015 11:40 am

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by aforaste1 on Thu Dec 31, 2015 10:07 pm

adafruit2 wrote:CC3000 core firmware is a litle flakey over long periods, i'd have your code do a hard reset of the CC3K and reconnect after ~5 hours. you'll be offline for 2 minutes but it might keep overall uptime higher

Just to clarify what is meant by hard reset, is this referring to using CC3000.disconnect, reboot and other functions OR an actual temporary cut in power to the CC3000 chip before powering it back up again using a transistor? I was thinking the latter, but thought I would double check. And if the latter, should the code within the loop kick out to the void setup routine every once in awhile (i.e. every 5 hours or so as you mention) after the CC3000 is power cycled off then on? I wasn't sure if sending it to the beginning of void setup would be sufficient to get the CC3000 up and running again, or whether there was information that came before that which would be necessary. I'm using the group sensor AIO Basics sketch as the starting point. Thanks.

aforaste1
 
Posts: 18
Joined: Sun Nov 01, 2015 11:40 am

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by Chipmonger on Thu Feb 25, 2016 3:59 pm

I am experiencing the same problem with the Mega and the Ethernet shield so I suspect it is not only an issue with the CC3000 Wifi chip. Did a power reset and all is normal again but only for a few hours.

Chipmonger
 
Posts: 7
Joined: Mon Jul 06, 2015 11:23 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by adafruit2 on Thu Feb 25, 2016 4:58 pm

use watchdog timers and error checking, it will help keep your project from 'hanging'! :)

adafruit2
Site Admin
 
Posts: 19224
Joined: Fri Mar 11, 2005 7:36 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by Chipmonger on Thu Feb 25, 2016 7:04 pm

The sketch is not hanging. It's running in the 5 second loop that is trying to reconnect to the MQTT server but for some reason it doesn't until I cycle the power. Happened in more than one network environment (totally different physical locations) so ruled out the network. I would expect it to re-establish the connection by itself without having to power down.

Chipmonger
 
Posts: 7
Joined: Mon Jul 06, 2015 11:23 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by adafruit2 on Thu Feb 25, 2016 7:17 pm

the CC3000 may be crashed inside and you cant tell. watchdog timers are awesome. also, the CC3000 kinda sucks. it was great 3 years ago

adafruit2
Site Admin
 
Posts: 19224
Joined: Fri Mar 11, 2005 7:36 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by Chipmonger on Fri Feb 26, 2016 2:14 am

@adafruit not using the CC3000; using the official Arduino Ethernet shield.

Chipmonger
 
Posts: 7
Joined: Mon Jul 06, 2015 11:23 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by Chipmonger on Fri Feb 26, 2016 2:36 am

@adafruit2 if the CC3000 "sucks," you should let Limor know so that she can stop selling products using them.

Chipmonger
 
Posts: 7
Joined: Mon Jul 06, 2015 11:23 pm

Re: MQTT Library CC3000, ping fails after publishing 14 hour

by aforaste1 on Fri Feb 26, 2016 11:59 pm

@chipmonger, I don't know if this will help or not, but I began to put serial print messages throughout so I could see where and when mine hung. I noticed that it would often hang after a ping failed. Had pings in there to keep the connection alive. I think both the Mqtt and the cc3000(in my case) have timeouts if I remember correctly and if they don't see activity disconnect. While everything was running smoothly with my code, it would publish and ping just fine and the connection was maintained. But if it missed a ping or two or failed to publish a value, it might push it past these timeout limits and disconnect. So, basically I added if then statements associated with these places where it was failing. If it failed, then the connect function was called (or alternatively you could use a delay to force the sleepydog or avr watchdog timer to kick it out of the loop and back to the void setup routine and reconnect fresh. Not sure if I'm explaining it well, but it did become more robust and mended itself more often than before.

FYI, I've seen adafruit say they will discontinue the cc3000, just not sure when that will happen.

aforaste1
 
Posts: 18
Joined: Sun Nov 01, 2015 11:40 am

Please be positive and constructive with your questions and comments.