Skip to content

Client Disconnects from Server on new publish #66

Closed
@sops21

Description

@sops21

I'm running an Ethernet Feather Wing and the m4 Express Feather. I'm running the current version of adafruit_minimqtt and adafruit_minimqtt with CircuitPython 6.1.0.

After connecting to a local Mosquitto broker, the client will disconnected after I publish (From another client) to the same subscription. I don't see any error or crashes on the serial monitor, but on my broker log, I see:

"Client Test 2 closed its connection"

After what I assume is the timeout period, I receive "

 MQTT Error ----
 PINGRESP not returned from broker.

" on the serial monitor.

Any thoughts or where I should start to debug?

My test code and current version is below:

PS C:\Users\Jason> python3 -m circup freeze
Found device at E:, running CircuitPython 6.1.0.
A newer version of CircuitPython (6.2.0-beta.0) is available.
adafruit_debouncer==1.3.8
adafruit_displayio_ssd1306==1.2.3
adafruit_logging==1.2.5
adafruit_requests==1.9.4
neopixel==6.0.1
adafruit_bitmap_font==1.3.3
adafruit_bus_device==5.0.4
adafruit_led_animation==2.5.2
adafruit_character_lcd==3.3.6
adafruit_display_text==2.12.1
adafruit_minimqtt==5.0.0
adafruit_wiznet5k==1.9.0`

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

import time
import terminalio
import sys
import busio
import digitalio
import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_minimqtt.adafruit_minimqtt import MMQTTException
import gc


server_ip = "10.21.2.101"
apid = 1




# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(board.D10)
# For Particle Ethernet FeatherWing
# cs = digitalio.DigitalInOut(board.D5)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)











print(gc.mem_free())
# Initialize ethernet interface with DHCP
dhcp_retries = 3
while dhcp_retries:
  try:
    eth = WIZNET5K(spi_bus, cs, debug=False)
    
    # text_box(row1_label, 0, "Every time you t!", 18)
    break
  except:
    dhcp_retries = dhcp_retries - 1
if not dhcp_retries:
  print("Failed to get IP address from DHCP")

requests.set_socket(socket, eth)
 
print("Chip Version:", eth.chip)
print("MAC Address:", [hex(i) for i in eth.mac_address])
print("My IP address is:", eth.pretty_ip(eth.ip_address))

# MQTT Begin 
# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = "/feeds/onoff"
#  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=server_ip,
    # username=secrets["aio_username"],
    # password=secrets["aio_key"],
    is_ssl=False,
    client_id='Test2',
    # socket_pool=pool,
    # log=True,
    # keep_alive=15
)
# mqtt_client.set_logger_level("DEBUG")
 
# 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...")
try:
    mqtt_client.connect()
       
except (ValueError, RuntimeError, MMQTTException) as e:
    print("Failed to get connect, retrying\n", e)
    
    mqtt_client.reconnect()

while True:
    # Keyboard Input
    try:
        mqtt_client.loop()        # Poll the message queue
        
    except (ValueError, RuntimeError, MMQTTException) as e:
        print("MQTT Error ---- \n", e)
    except (AssertionError,) as e:
        print("Assertion Error ----- \n", e)
   
    time.sleep(1)
     

Metadata

Metadata

Assignees

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