0

MQTT Service
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MQTT Service

by dcorsar on Mon Mar 04, 2019 12:18 pm

Hi

I've been trying to use the Python Paho MQTT library to publish and subscribe to my data feeds; however, the feeds aren't being updated. I've tried connecting to the broker at io.adafruit.com using the Eclipse Paho MQTT Utility, but its unable to connect. I've also tried using the Adafruit Python MQTTClient class, but it also seems to fail to connect when using SSL or not. I've also tried using the Adafruit Python Client library, which does connect and publish data to the feeds, but is it using the HTTP API rather than the MQTT to achieve this? Ideally, I'd like to use he MQTT broker to publish and subscribe to data to/from my feeds.

I was just wondering - are there currently issues with the MQTT broker?

Thanks,
David

dcorsar
 
Posts: 2
Joined: Mon Mar 04, 2019 11:29 am

Re: MQTT Service

by abachman on Mon Mar 04, 2019 12:44 pm

Hi David,


There are not any issues with the MQTT broker right now, no.

The easiest way to check yourself is to open a dashboard and see if it can send/receive data with itself. All live data on Adafruit IO is powered by the same MQTT broker that devices / scripts connect to. Basically: if dashboards are up, MQTT is up.

If you could paste in a minimal example script without your key/username/credentials, someone might be able to help you troubleshoot.


- adam

abachman
 
Posts: 276
Joined: Mon Feb 01, 2010 12:48 pm

Re: MQTT Service

by dcorsar on Mon Mar 04, 2019 6:18 pm

Hi Adam

Thanks for getting back to me so quickly. I didn't realise the dashboards could publish as well as display - that's useful to know.

The code below is most of the code I'm using, with ids, etc, removed. I've got a similar program for subscribing that is now working once I made the feed public. However, looking at the logging for the code below, what seems to be happening is that every time a request to publish is made, the client is disconnected (with an rc value of 1) and then automatically reconnects but the message isn't being sent.

I've had a look at the Adafruit library code on github, and it seems to be using the same functions from the paho_mqtt library, but I may be missing something. This code works fine with a Mosquitto MQTT broker we have running locally - are there any conditions that would cause a client to be disconnected from the Adafruit broker?

Code: Select all | TOGGLE FULL SIZE
import paho.mqtt.client as mqttClient
import time

def on_connect(client, userdata, flags, rc):

    if rc == 0:
        print("Connected to broker")
        global Connected                #Use global variable
        Connected = True                #Signal connection
    else:
        print("Connection failed")
       
def on_disconnect(client, userdata, rc):
    print("disconnected %d"%rc)
 
def on_log(client, userdata, level, buf):
    print("log %s"%str(buf))
 
Connected = False   #global variable for the state of the connection
 
broker_address= "io.adafruit.com"
port = 1883
user = ""
password = ""


client = mqttClient.Client(client_id="",protocol=mqttClient.MQTTv311)               #create new instance
client.username_pw_set(user, password=password)    #set username and password
client.on_connect= on_connect                      #attach function to callback
client.on_disconnect = on_disconnect
client.on_log = on_log
client.enable_logger()
client.connect(broker_address, port=port)          #connect to broker

client.loop_start()        #start the loop
 
while Connected != True:    #Wait for connection
    time.sleep(0.1)

try:
    while True:
        value = input('Enter the message:')
        client.publish("feed-id",value)
except KeyboardInterrupt:
    client.disconnect()
    client.loop_stop()



Thanks,
David

dcorsar
 
Posts: 2
Joined: Mon Mar 04, 2019 11:29 am

Re: MQTT Service

by brubell on Mon Mar 04, 2019 6:56 pm

David -

. I've also tried using the Adafruit Python MQTTClient class, but it also seems to fail to connect when using SSL or not.


I've also tried using the Adafruit Python Client library, which does connect and publish data to the feeds, but is it using the HTTP API rather than the MQTT to achieve this? Ideally, I'd like to use he MQTT broker to publish and subscribe to data to/from my feeds.


The Adafruit IO Python library contains two clients which can interact with the Adafruit IO API:

Client.py: Adafruit IO REST API Client
MQTT_Client: Adafruit IO MQTT API Client, https://github.com/adafruit/Adafruit_IO ... _client.py

We have plenty of examples for the Adafruit IO Python MQTT_Client on this repository as well, https://github.com/adafruit/Adafruit_IO ... mples/mqtt

You mentioned that the MQTTClient fails when trying to connect via SSL (the MQTTClient uses a SSL connection as a default). Can you provide the version of the code, and the output where it fails?

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

Please be positive and constructive with your questions and comments.