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

Unreliable connection
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.

Unreliable connection

by krambriw on Sat Feb 20, 2016 10:52 am

Hi,

I experience frequently that it is unreliable to connect. The Python script below verifies that several retries sometimes are needed to get a successful connection

Kind regards, Walter

Code: Select all | TOGGLE FULL SIZE
from Adafruit_IO import MQTTClient
import time

def connected(client):
    client.subscribe(adafruitFeedId)
    print client.is_connected()
    global started
    global cntr
    started = True
    cntr = 0
   

def disconnected(client):
    print client.is_connected()
    del client


def message(client, feed_id, payload):
    msg = feed_id
    pl = payload
    print msg, pl
    client.disconnect()


def goConnect():
    # Create a MQTT client instance.
    client = MQTTClient(adafruitUser, adafruitIOkey)

    # Setup the callback functions defined above.
    client.on_connect    = connected
    client.on_disconnect = disconnected
    client.on_message    = message
 
    # Connect to the Adafruit IO server and start subscribing.
    client.connect()
    client.loop_background()
    return client
       

adafruitUser = 'user'
adafruitIOkey = 'iokey'
adafruitFeedId = 'mytest'
cntr = 0
started = False

while not started:
    client = goConnect()
    if cntr > 0:
        print 'Retry: ', cntr
    cntr += 1
    time.sleep(0.5)

krambriw
 
Posts: 22
Joined: Fri Feb 19, 2016 6:07 pm

Please be positive and constructive with your questions and comments.