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

Skip to content

MiniMQTT loop() blocking and throwing exception if no message is published to a subscribed topic during blocking #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chriswei opened this issue Feb 22, 2021 · 2 comments

Comments

@chriswei
Copy link

I'm trying to use Adafruit IO's CircuitPython library on a Feather M4 Express with the Airlift FeatherWing. I want to be able to use MQTT/IO pub/sub to subscribe to a topic and publish to another topic in a loop, but the mqtt_client.loop() functions seems to block waiting for data to be posted to the subscribed topic once, then the loop call continues as expected. If I don't 'kick' the subscribed topic by publishing data to this topic from the adafruit.io Feeds screen once, the loop() will eventually throw

MMQTTException: Unable to receive 1 bytes within 60 seconds.

I'm using the example code in minimqtt_pub_sub_nonblocking_esp32spi.py (from the latest official v6 release of the CircuitPython IO library) at https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/blob/85d9d846f3fd8f9d3ee8ae354c5c34d2e2970c1a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py

while True:
   mqtt_client.loop()
   mqtt_client.publish(topic, value)
   time.sleep(5)

I tried catching and eating the exception, which works intermittently.

while True:
  try:
    mqtt_client.loop()
  except (MQTT.MMQTTException) as e:
    print(e)
  mqtt_client.publish(topic, value)
  time.sleep(5)

Sometimes it'll work (i.e. exception caught, publish works fine, repeat), other times loop() will throw:

Traceback (most recent call last):
File "code.py", line 142, in <module>
File "code.py", line 140, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 809, in loop
File "adafruit_minimqtt/adafruit_minimqtt.py", line 569, in ping
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 82, in send
File "adafruit_esp32spi/adafruit_esp32spi.py", line 721, in socket_write
RuntimeError: Failed to send 2 bytes (sent 0)

@chriswei
Copy link
Author

chriswei commented Feb 23, 2021

Following brubell's reply to my Forums post in https://forums.adafruit.com/viewtopic.php?f=60&t=175702, I ran my code using the adafruit_minimqtt library from the master branch of the repo, and the loop() function no longer blocks and throws exceptions.

I ran the new library code in both my code that uses the adafruit_minimqtt library and my code that uses the adafruit_io library with the IO_MQTT wrapper, and they both now run as expected (the loop() function no longer blocks).

@brentru
Copy link
Member

brentru commented Feb 24, 2021

Thanks for testing @chriswei - we've released a new version of this library to patch the blocking loop.

https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/releases/tag/5.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants