Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rdpickard opened this issue Jan 29, 2020 · 0 comments
Open

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

rdpickard opened this issue Jan 29, 2020 · 0 comments

Comments

@rdpickard
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant