MQTT stops updating?

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
tarrant1704
 
Posts: 20
Joined: Sun Jun 10, 2018 9:13 am

MQTT stops updating?

Post by tarrant1704 »

Ok so here's my setup:
Aqara M2 hub connected via LAN cable
- door/window sensor

Created IFTTT applet so that when sensor is open/close it sends this to the appropriate AdafruitIO feed.

Have a MatrixPortal M4 and wrote CircuitPython code to subscribe to MQTT feed and get updates. Initially, all seems well. After executing the code, if I manually "open" the sensor, my AdafruitIO Feed receives "open" and my code registers this. Then if I manually "close" the sensor, again the AdafruitIO Feed receives "close" and my code also registers this. So far so good.

But after 2-5 minutes of run time, my M4 no longer seems to register any changes. The AdafruitIO Feed still updates appropriately based on the state of the sensor. The AdafruitIO Monitor page also shows the correct status of the sensor and lists "0 live errors"

REPL doesn't display any errors however, and my code still seems to be running. Here's a snippet of the code I wrote:

Code: Select all

from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_matrixportal.network import Network
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

while True:
    try:
        mqtt.is_connected()
        mqtt.loop()
    except (MQTT.MMQTTException, RuntimeError):
        print("MQTT is disconnected ... re-connecting")
        network.connect()
        mqtt.reconnect()
Any thoughts on how to debug this further or fix it?

User avatar
tarrant1704
 
Posts: 20
Joined: Sun Jun 10, 2018 9:13 am

Re: MQTT stops updating?

Post by tarrant1704 »

The IO monitor also shows I connect (when I run the code) and after a couple minutes I think it disconnects for some reason. Yet no REPL error reported.
Screenshot 2023-01-14 171700.jpg
Screenshot 2023-01-14 171700.jpg (46.66 KiB) Viewed 186 times

User avatar
tarrant1704
 
Posts: 20
Joined: Sun Jun 10, 2018 9:13 am

Re: MQTT stops updating?

Post by tarrant1704 »

Interesting, stumbled my way to the answer (I think). My original code:

Code: Select all

mqtt.loop()
I then tried specifying a timeout value:

Code: Select all

mqtt.loop(1)
and that seemed to do the trick! So far it's been up 10 min and still updating as intended.

User avatar
tarrant1704
 
Posts: 20
Joined: Sun Jun 10, 2018 9:13 am

Re: MQTT stops updating?

Post by tarrant1704 »

A new problem ... my Matrix Portal device has been working well the past few days. Then today, I lost internet for a couple minutes. Rebooted my router and Wi-Fi access point and my computers all seem happy again. However, my Matrix Portal now gives this error after booting up:

Traceback (most recent call last):
File "code.py", line 164, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 858, in loop
File "adafruit_minimqtt/adafruit_minimqtt.py", line 589, in ping
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 86, in send
File "adafruit_esp32spi/adafruit_esp32spi.py", line 766, in socket_write
ConnectionError: Failed to send 2 bytes (sent 0)

There was no change in code, AIO key, etc. It loads the initial values in my Feed without issue. But after reading initial values, it can't receive any MQTT message updates. Any thoughts?

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: MQTT stops updating?

Post by brubell »

The socket_write issue is within CircuitPython, specifically a possible bug in adafruit_esp32spi. You may want to file this over in the CircuitPython subforum instead. I can also move this topic there, if desired.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”