MQQT subscribe and publishing issue

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
piotrchizinski
 
Posts: 2
Joined: Mon Nov 14, 2022 2:53 pm

MQQT subscribe and publishing issue

Post by piotrchizinski »

Hello,

I am having a issue with circuitpython and the mqqt lib. I am not sure where I am running into an issue.
Running a M4 express board with ethernet sheild, when I run the example - Connecting to the Adafruit IO MQTT Broker via (https://learn.adafruit.com/mqtt-in-circ ... qtt-broker)

I am not able to get a updated subscription and publish to a topic at the same time like the example shows. If I comment out the

#mqtt_client.loop()----> publishing occurs at the correct interval, but of course no subscription updates occur

mqtt_client.loop() active maintains the subscription but the program does not include any publishing commands below the mqqt_client.loop() entry.

Thank you for you Help!
Piotr

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from digitalio import DigitalInOut

from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Get Adafruit IO details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("Adafruit IO secrets are kept in secrets.py, please add them there!")
    raise

cs = DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs)

### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = secrets["aio_username"] + "/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = secrets["aio_username"] + "/feeds/onoff"

### 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("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
    # Subscribe to all changes on the onoff_feed.
    client.subscribe(onoff_feed)


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


def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    # has a new message.
    print("New message on topic {0}: {1}".format(topic, message))


# Initialize MQTT interface with the ethernet interface
MQTT.set_socket(socket, eth)

# Set up a MiniMQTT Client
# NOTE: We'll need to connect insecurely for ethernet configurations.
mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    is_ssl=False,
)

# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message

# Connect the client to the MQTT broker.
print("Connecting to Adafruit IO...")
mqtt_client.connect()

photocell_val = 0
while True:
    # Poll the message queue
    mqtt_client.loop()

    # Send a new message
    print("Sending photocell value: %d..." % photocell_val)
    mqtt_client.publish(photocell_feed, photocell_val)
    print("Sent!")
    photocell_val += 1
    time.sleep(5)

User avatar
wdyn1
 
Posts: 4
Joined: Sun Aug 21, 2022 8:37 pm

Re: MQQT subscribe and publishing issue

Post by wdyn1 »

Hi Piotr,

Could you tell us what version of CircuitPython and MQTT you are using? I had an issue with 8.0.0beta as it was sending the wrong error code to MQTT loop() which would cause loop() to fail before reading anything. My solution involved diving into the MQTT code and making some changes. If you feel you need to do that, I can try to go back and reproduce what I did to fix it.

User avatar
piotrchizinski
 
Posts: 2
Joined: Mon Nov 14, 2022 2:53 pm

Re: MQQT subscribe and publishing issue

Post by piotrchizinski »

thanks for the replay! I have version 7.x runninf on both. When I have the loop active amd send a subscription update sometimes it also sends a publish message but not with any regularity. that would be helpful if you could do that, thank you

User avatar
wdyn1
 
Posts: 4
Joined: Sun Aug 21, 2022 8:37 pm

Re: MQQT subscribe and publishing issue

Post by wdyn1 »

Ah. Ok. The fix I had in mind wouldn't apply to you. However, check to see that you're on the latest MQTT version. There was a fix pushed last week.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”