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

Cannot connect to Adafruit IO #107

Open
prcutler opened this issue Sep 2, 2023 · 1 comment
Open

Cannot connect to Adafruit IO #107

prcutler opened this issue Sep 2, 2023 · 1 comment

Comments

@prcutler
Copy link

prcutler commented Sep 2, 2023

Hi,

I'm trying to use the AdafruitIO library instead of (or with) Adafruit MiniMQTT. I've tried on both an S3 MatrixPortal and a Pico W.

It fails on the line:
io.connect() with the error:

Traceback (most recent call last):
  File "code.py", line 79, in <module>
  File "adafruit_io/adafruit_io.py", line 116, in connect
AdafruitIO_MQTTError: MQTT Error: Unable to connect to Adafruit IO.

Here's a short example with the Pico W. If I swap out the line io = IO_MQTT(mqtt_client) and use a normal MQTT setup, it works fine and I can subscribe to my feed. I've tried using SSL and non-SSL (like below). I don't know if anyone can verify that io.connect() works for them.

import os
import ssl
import time
import json
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import socketpool
import wifi
import displayio
import terminalio
import adafruit_display_text.label
from adafruit_io.adafruit_io import IO_MQTT


wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()

# ------------- MQTT Topic Setup ------------- #
mqtt_topic = "prcutler/feeds/audio"


mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    port=1883,
    username=os.getenv('aio_username'),
    password=os.getenv('aio_key'),
    socket_pool=pool,
)


### Code ###
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print("Subscribing to %s" % mqtt_topic)
    client.subscribe(mqtt_topic)


def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def unsubscribe(client, userdata, topic, pid):
    # This method is called when the client unsubscribes from a feed.
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))


def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from MQTT Broker!")


def message(client, topic, message):
    """Method callled when a client's subscribed feed has a new
    value.
    :param str topic: The topic of the feed with a new value.
    :param str message: The new value
    """
    print(topic, message)

    print(message)


io = IO_MQTT(mqtt_client)

io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message

# Connect to Adafruit IO

print("Connecting to Adafruit IO...")
io.connect()

while True:
    io.loop()
@luke-edward
Copy link

Still having this issue. Any fix?

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

2 participants