adafruit / Adafruit_CircuitPython_MiniMQTT

MQTT Client Library for CircuitPython
Other
80 stars 49 forks source link

Frequent errors in .loop calls with CircuitPython 8 #124

Open brucewinter opened 2 years ago

brucewinter commented 2 years ago

No problems with CircuitPython 7, but with 8 I get frequent errors in .loop calls, about 1 error for 5->10 0calls, done once a second.

This is my code:

def mqtt_connect():
    try:
        print("Connecting to mqtt server ...")
#       mqtt_client.connect()
        mqtt_client.reconnect()
        print('Connected :)')
        mqtt_subscribes()
    except Exception as e:
        print("mqtt connect error")
        print(e)

... in loop ...

       ...
        time.sleep(1.0)
        try: 
            mqtt_client.loop()   
        except (ValueError, RuntimeError) as e:
            print("Failed to get data, retrying\n", e)
            wifi.reset()
            mqtt_client.reconnect()
            continue
        except OSError as e:
            print('mqtt OSError')
            print(e.errno)
            print(e)
            print('reconnecting ...')
            mqtt_connect()
        except Exception as e:
            print('mqtt other error')
            print(e)
            print('reconnecting ...')
            mqtt_connect()

This is the error:

New message on topic sensor/Outside Temperature: 76.8 Temp Out: 76.8 mqtt other error

Connecting to mqtt server ... Connected to mqtt server Connected :) Subscribed to ha/heartbeatM with QOS level 0 Subscribed to sensor/Outside Temperature with QOS level 0

I also tried wifi.reset() but got: 'module' object has no attribute 'reset'

The resulting error structure 'e' is empty. Any suggestions?

Thanks!

tekktrik commented 2 years ago

This may be related to #101 or #108

brucewinter commented 2 years ago

Thanks for the pointers! I tried modifing the OSError check suggested at the end of #101 and the _wait_for_msg() instead of .loop from #108. Also tried the suggesions change the default timeout in _wait_for_msg from 0.1 to 1.0, from circuitpython issues #6988. None of these changes fixed my problem.

Note that this problem is only with Circuitpython 8, the same code works great withg Circuitpython 7. Also tried the lastest build of 8, same problem.

I forgot to mention I am using the Feather ESP32-S2 with TFT. I have some other Adafruit ESP32 boards arriving tomrrow that I will try as well.

Thanks again for the help.

mopore commented 1 year ago

I experiencing the same with a Feather ESP32-S3 TFT and CircuitPython 8.0.0-beta.2.

rdagger commented 1 year ago

Same problem on QT PY ESP32-S2. Subscribing to a topic on Adafruit IO works reliably in CircuitPython 7.3.3 but unusable in CircuitPython 8.

  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 864, in loop
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 894, in _wait_for_msg
]0;🐍10.0.7.41 | 894@/lib/adafruit_minimqtt/adafruit_ MMQTTException | 8.0.0-beta.3\

I couldn't even get the subscribe method to work until I hard coded a longer timeout. I tried zero and longer timeouts for the loop method, but nothing seems to work.

GearlooseTechnology commented 1 year ago

This code will crash upon wifi reconnect.

try:
   self.client.loop()
except Exception as e:
   self.debug(f"Could not connect to MQTT broker")
   self.debug(e)
   self.client.reconnect(resub_topics=False)

I'm very sorry to say, but I have never experienced such a crappy library as this one. Random crashes and hangs everywhere.

vladak commented 8 months ago

I'm very sorry to say, but I have never experienced such a crappy library as this one. Random crashes and hangs everywhere.

To be fair, the library got more robust in the last year, so it is recommended to try the latest version. Also, it is evident from the above comments (running on CircuitPython 7 vs 8) that it sometimes is merely a victim of CircuitPython (CP) changes. All in all, the best thing is to approach the problems one by one and make sure they are being solved whether they are in the library or in CP or both.