Thanks to visit codestin.com
Credit goes to github.com

Skip to content

minimqtt blocking on .loop() method #148

Closed
@lee-matthews

Description

@lee-matthews

CircuitPython version

W5500-EVB-Pico board 
adafruit-circuitpython-wiznet_w5500_evb_pico-en_GB-8.0.0-beta.6.uf2
adafruit-circuitpython-bundle-8.x-mpy-20230126

Code/REPL

import board
import busio
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import digitalio
import time

##SPI0
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17

##reset
W5x00_RSTn = board.GP20

def on_connect(client, userdata, flags, rc):
    print("MQTT connected to broker")

def main():

    # Setup your network configuration below
    # random MAC, later should change this value on your vendor ID
    MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)
    # Setup your network configuration below
    IP_ADDRESS = (192, 168, 1, 250)
    SUBNET_MASK = (255, 255, 255, 0)
    GATEWAY_ADDRESS = (192, 168, 1, 1)
    DNS_SERVER = (192, 168, 1, 1)

    ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
    ethernetRst.direction = digitalio.Direction.OUTPUT

    # For Adafruit Ethernet FeatherWing
    cs = digitalio.DigitalInOut(SPI0_CSn)
    # For Particle Ethernet FeatherWing
    # cs = digitalio.DigitalInOut(board.D5)
    spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)

    # Reset W5500 first
    ethernetRst.value = False
    time.sleep(1)
    ethernetRst.value = True

    # Initialize ethernet interface with DHCP
    # eth = WIZNET5K(spi_bus, cs)
    # Initialize ethernet interface without DHCP
    # *****  For DHCP to work correctly. Stop the code properly - STOP button, then click on play
    # otherwise WIZNET5K function call hangs and no IP address is recovered
    eth = WIZNET5K(spi_bus, cs, is_dhcp=False)
    eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)

    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))

    socket.set_interface(eth)

    MQTT.set_socket(socket, eth)
    
    g_mqtt_client = MQTT.MQTT(
        broker="broker ip",
        port=1883,
        username="user",
        password="password"
    )

    g_mqtt_client.on_connect = on_connect
    print("connecting")
    
    g_mqtt_client.connect()
    
    g_mqtt_client.subscribe("HELLO/WORLD")
    
    print("connected")
    while True:
        g_mqtt_client.publish("DETECTOR/ZONE1/ALIVE", "alive")
        print("loop")
        g_mqtt_client.loop()

if __name__ == "__main__":
    main()

Behavior

I'm having some problems with MQTT with circuitpython.

I'm using circuitpython on a W5500-EVB-Pico board to read data from GPIO inputs and then publish that data using MQTT.

I notice that my call to g_mqtt_client.loop() is blocking.

If however I publish a message on the topic "HELLO/WORLD", sometimes the loop() method unblocks and continues until it reaches the loop() method again. If I set the timeout parameter on the loop() method to 1, this doesn't change anything.

Description

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions