Skip to content

Cannot connect to Adafruit IO #107

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

Closed
prcutler opened this issue Sep 2, 2023 · 2 comments
Closed

Cannot connect to Adafruit IO #107

prcutler opened this issue Sep 2, 2023 · 2 comments

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?

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Nov 5, 2024

For the PicoW and Feather S3 or S2 you need to use port 8883 instead of port 1883. There is a full example that works on these devices here: https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO/blob/main/examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py

If you attempt to connect over port 1883 on these device it will raise the mentioned exception.

I was able to successfully test a basic connection to adafruit IO on both a Feather S3 and a PicoW using port 8883 using circuitpython 9.2.0 and current libraries.

@FoamyGuy FoamyGuy closed this as completed Nov 5, 2024
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

3 participants