Skip to content

Can't detect MQTT client login errors on the main thread #109

Open
@rdpickard

Description

@rdpickard

Hi all,

There does not seem to be a good way to detect a client error on the python main thread when connecting to a feed. I believe this is because the paho.mqtt.client module goes into 'async' mode when there are callbacks set (from the paho docs here).

If the user credentials are incorrect there isn't a way to detect this in a single thread application, such as something that connects and just posts a message to a feed then disconnects.

I have a bit of a hacky work-around

import logging
import json

import Adafruit_IO

logging.basicConfig(level=logging.DEBUG)

AGENT_MESSAGE_VERSION = .1
ADAFRUIT_IO_USERNAME = "my ada fruit username"
ADAFRUIT_IO_KEY = "WRONG KEY"
FEED_ID = "test_feed_example_derp"


adafruit_io_mqtt_client = Adafruit_IO.MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

adafruit_io_mqtt_client.connect()

feed_message = {"message" : "message would go here"}

adafruit_io_mqtt_client.loop()

# Do some work here

adafruit_io_mqtt_client.publish(FEED_ID, json.dumps(feed_message))

if adafruit_io_mqtt_client.is_connected():
    adafruit_io_mqtt_client.disconnect()
else:
    logging.error("Message not sent!")

The is_connected() call will only return true if the networking thread is connected to the feed, but in this case the paho.mqtt.client doesn't try to connect to the IO servers until the 'publish()' call is made.

Ideally the call adafruit_io_mqtt_client.connect() or adafruit_io_mqtt_client.publish() would raise an exception.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions