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

MQTT Status?
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 Status?

by jlbeck on Wed Mar 23, 2016 3:48 pm

Is the MQTT interface working? I have a simple code that works most of the time but I have been unable to connect today. Using a Raspberry Pi A+.

The status page says it's ok but my code won't connect.

Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python

# Import standard python modules.
import sys
import time
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

from Adafruit_IO import MQTTClient
AIO_USERNAME = "xxxx"
AIO_KEY = "xxxx"
aio = MQTTClient(AIO_USERNAME, AIO_KEY)

aio.connect()

while aio.is_connected() !=  True:
   logger.warning("Client not connected, connecting..")
   aio.connect()
   time.sleep(2)

aio.disconnect()

jlbeck
 
Posts: 26
Joined: Fri Aug 09, 2013 11:45 am

Re: MQTT Status?

by jwcooper on Wed Mar 23, 2016 5:16 pm

Everything seems to be working on our end. Our tests are passing, and we haven't heard anything from anyone else on this issue. Try creating a stream block on the dashboard with the feed you're trying to connect with. There could be other errors that aren't making it through to you.

jwcooper
 
Posts: 679
Joined: Tue May 01, 2012 9:08 pm

Re: MQTT Status?

by mikeadamz on Fri Mar 25, 2016 12:17 pm

I had similar problems. I get disconnected and for whatever reason I can't reconnect until I restart my app.

I suspect they're using an AWS ELB to balance MQTT requests across multiple servers and when the ELB IP address changes (which they do from time to time) we lose connectivity and can not reconnect.

It's likely an issue with the DNS TTL or with how Paho MQTT handles the DNS lookups for long-lived connections

The way I got around it was to check client.is_connected() before publishing data, if that returns false I reconnect and wait 30 seconds. If it fails 10 times in a row the script exits and is restarted by supervisord.

I'm running on a Raspberry Pi 2 model b and it's pretty idle with the MQTT client and supervisord running. It should be okay on an A+, depending on how much work you're doing with your MQTT client.

On modern raspian you can install supervisord with

Code: Select all | TOGGLE FULL SIZE
raspberrypi$ sudo apt-get install supervisor

For information on how to setup supervisor, check out the website at http://supervisord.org/

mikeadamz
 
Posts: 17
Joined: Thu Jan 21, 2016 1:20 am

Re: MQTT Status?

by jlbeck on Tue Mar 29, 2016 11:47 am

Thanks for the replies.

I changed

KEEP_ALIVE_SEC = 1800

and haven't had a disconnect in over a week.

jlbeck
 
Posts: 26
Joined: Fri Aug 09, 2013 11:45 am

Please be positive and constructive with your questions and comments.