fizista / micropython-umqtt.robust2

MIT License
49 stars 7 forks source link

Getting MQTTException(1,) when trying to check_msg #9

Closed guillochon closed 2 years ago

guillochon commented 2 years ago

This is a weird one: I have two Pico W's with I think exactly the same setup: same version of micropython, installed simple2 and robust2 on both, and am running identical code on both. One of the Picos is working just fine, the other one is throwing these errors when I try to run check_msg():

MQTT (check_msg): MQTTException(1,)
MQTT (check_msg): MQTTException(1,)
MQTT (check_msg): MQTTException(1,)
MQTT (check_msg): MQTTException(1,)
MQTT (reconnect): OSError(12,)
MQTT (check_msg): MQTTException(1,)
MQTT (check_msg): MQTTException(1,)
MQTT (reconnect): OSError(12,)
MQTT (check_msg): MQTTException(1,)
MQTT (check_msg): MQTTException(1,)
MQTT (reconnect): OSError(12,)

Code I am running is the following:

from machine import Pin
import utime
import network
from umqtt.robust2 import MQTTClient

global state
state = True

ssid = "abcd"
password = "1234"
device_name = b"Coffee Machine"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# wlan.config(dhcp_hostname=device_name)  # Not yet supported on Pico W https://github.com/micropython/micropython/pull/8918
wlan.connect(ssid, password)

# Wait for connect or fail
max_wait = 15
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print("waiting for connection...")
    utime.sleep(1)

# Handle connection error
if wlan.status() != 3:
    raise RuntimeError("network connection failed")
else:
    print("connected")
    status = wlan.ifconfig()
    print("ip = " + status[0])

relay1 = Pin(6, Pin.OUT)  # set pin GP6 as output
relay1(1 if not state else 0)
led = Pin("LED", Pin.OUT)
led.on()

def sub_cb(topic, msg, retained, duplicate):
    global state
    if topic == device_name + b"/set":
        state = True if msg.lower() in [b"1", b"true"] else False
        relay1(1 if not state else 0)  # Toggle Relay
        if state:
            led.on()
        else:
            led.off()

    print((topic, msg, retained, duplicate))

c = MQTTClient("umqtt_client", "homebridge.local")
c.DEBUG = True
# Information whether we store unsent messages with the flag QoS==0 in the queue.
c.KEEP_QOS0 = False
# Option, limits the possibility of only one unique message being queued.
c.NO_QUEUE_DUPS = True
# Limit the number of unsent messages in the queue.
c.MSG_QUEUE_MAX = 2

c.set_callback(sub_cb)

if not c.connect(clean_session=False):
    print("New session being set up")
    c.subscribe(device_name + b"/set")

last_state = True
c.publish(device_name + b"/state", "true")
c.send_queue()

while 1:
    if last_state != state:
        c.publish(device_name + b"/state", str(state).lower())
    utime.sleep_ms(100)
    last_state = state

    # At this point in the code you must consider how to handle
    # connection errors.  And how often to resume the connection.
    if c.is_conn_issue():
        while c.is_conn_issue():
            # If the connection is successful, the is_conn_issue
            # method will not return a connection error.
            c.reconnect()
        else:
            c.resubscribe()

    c.check_msg()  # needed when publish(qos=1), ping(), subscribe()
    c.send_queue()  # needed when using the caching capabilities for unsent messages

c.disconnect()

Because one of my two Picos is working, I'm really struggling to figure out what's going wrong. Any ideas here? I'm kind of at a loss...

Details:

fizista commented 2 years ago

And in both clients you have client_id="umqtt_client" the same?

guillochon commented 2 years ago

That was it, working now! šŸ˜µā€šŸ’«

I mean maybe it's obvious to some folk, but perhaps it should be mentioned in the docs that clients cannot have the same ID on the same network? Or maybe the error message could be made more clear?

fizista commented 2 years ago

There is probably enough description in the code of the mqtt.simple2 class. A more detailed description of errors, would cause an unnecessary increase in the amount of code. And in this class of devices we try to save every byte of data.

Thank you for your comments.