MQTT QoS & Flag
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 QoS & Flag

by niccolorgu on Sun Jan 17, 2021 7:49 am

I have set up a raspberry pi and am connecting to AdaFruit feeds using Adafruit IO MQTT API.

I have the following code working and would like to know how to modify it to set a QoS or retain flag;

Code: Select all | TOGGLE FULL SIZE
# Example of using the MQTT client class to subscribe to and publish feed values.
# Modified from examples by
# Author: Tony DiCola
# https://github.com/adafruit/Adafruit_IO_Python/tree/master/examples/mqtt

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

# 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 = 'Activity Key'

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

# 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 DemoFeed changes...')
    # Subscribe to changes on a feed named DemoFeed.

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

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.

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

# Connect to the Adafruit IO server.

# Now the program needs to use a client loop function to ensure messages are
# sent and received.  There are a few options for driving the message loop,
# depending on what your program needs to do.

# This is why the python modules random, sys, time have been imported
# The first option is to run a thread in the background so you can continue
# doing things in your program.
# Now send new values every 10 seconds.
print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
  while True:
    value = random.randint(0, 100)
    print('Publishing {0} to DemoFeed.'.format(value))
    client.publish('DemoFeed', value)
# If you do not have a feed called DemoFeed, one will be created with the first message.
except KeyboardInterrupt: # detect ctrl+c and handel it

Posts: 4
Joined: Wed Jan 13, 2021 5:37 pm

Re: MQTT QoS & Flag

by brubell on Wed Jan 20, 2021 11:34 am

Adafruit IO does not support /retain functionality, but it supports a similar /get functionality: https://io.adafruit.com/api/docs/mqtt.h ... -get-topic

For QoS, add a `qos` kwarg to the client.subscribe call:
Code: Select all | TOGGLE FULL SIZE
client.subscribe('DemoFeed', qos=1)

Posts: 1412
Joined: Fri Jul 17, 2015 10:33 pm

Re: MQTT QoS & Flag

by niccolorgu on Wed Feb 03, 2021 11:20 am

Thank you

Posts: 4
Joined: Wed Jan 13, 2021 5:37 pm

Please be positive and constructive with your questions and comments.