0

how to call a function of disconnection
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.

how to call a function of disconnection

by omaromohsen on Sun Sep 29, 2019 9:34 am

I am a newbie user of adafruit.io server when Isaw the documentation the file publishing and subscribing
Code: Select all | TOGGLE FULL SIZE
# Example of using the MQTT client class to subscribe to a feed and print out
# any changes made to the feed.  Edit the variables below to configure the key,
# username, and feed to subscribe to for changes.

# Import standard python modules.
import sys

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'

# Set to the ID of the feed to subscribe to for updates.
FEED_ID = 'DemoFeed'


# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print('Connected to Adafruit IO!  Listening for {0} changes...'.format(FEED_ID))
    # Subscribe to changes on a feed named DemoFeed.
    client.subscribe(FEED_ID)

def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print('Feed {0} received new value: {1}'.format(feed_id, payload))


# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message

# Connect to the Adafruit IO server.
client.connect()

# Start a message loop that blocks forever waiting for MQTT messages to be
# received.  Note there are other options for running the event loop like doing
# so in a background thread--see the mqtt_client.py example to learn more.
client.loop_blocking()


Now I want when i disconnect the callback the function in the main program I tried using a flag with a value contradicting the value in connected function and counter
Code: Select all | TOGGLE FULL SIZE
 global loop_flag
   
    print('Connected to Adafruit IO!  Listening for {0} changes...'.format(FEED_ID))
    # Subscribe to changes on a feed named DemoFeed.
    client.subscribe(FEED_ID)
    loop_flag=0
    return(loop_flag)
   

but loop_flag is equal to 1 and there is a loop function
Code: Select all | TOGGLE FULL SIZE
while loop_flag==1:
    print("Waiting for callback{}.format(counter))
    time.sleep(0.01)
    counter=counter+1
    if(counter==100):
     client.disconnect()
      break




any help will be appreciated

omaromohsen
 
Posts: 5
Joined: Sat Mar 09, 2019 5:26 am

Re: how to call a function of disconnection

by brubell on Mon Sep 30, 2019 9:29 am

Now I want when i disconnect the callback the function in the main program I tried using a flag with a value contradicting the value in connected function and counter


Before we debug the code - are you trying to stop the loop in your main function after a disconnection event occurs?

brubell
 
Posts: 732
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.