Mqtt subscribe stops receiving
gt1 last edited by
I have a gpy that i am testing a relay to trigger from the cloud on the gpy.
everything works. But after around 5 hours or so it stops working. the device is still working and seems to be connected to lte. I read on another post that it could be a memory issue so i added gc collect to the loop and it prints the same amount of memory free. any ideas or help to point me in the right direction.
im running a mosquitto mqtt server that i have a node red dashboard running as well to get my button push from the cloud. In the dashboard i have a button pressed received topic so i can confirm the button was pressed and the msg is on the mqtt server.
here is my code
import time from machine import RTC from network import LTE import pycom,ubinascii import machine import math import network import os import time import utime import gc from utime import ticks_ms, ticks_diff from machine import Pin from umqtt.robust import MQTTClient p_out = Pin('P8', mode=Pin.OUT) p_out(True) TOPIC =b"relayoff" server="*******" c = MQTTClient("umqtt_client", server) gc.enable() # Need to use global variables. # If in each function you delare a new reference, functionality is broken lte = LTE() rtc = RTC() pycom.heartbeat(False) # Returns a network.LTE object with an active Internet connection. def getLTE(): # If already used, the lte device will have an active connection. # If not, need to set up a new connection. if lte.isconnected(): return lte # Modem does not connect successfully without first being reset. print("Resetting LTE modem ... ", end='') lte.send_at_cmd('AT^RESET') print("OK") time.sleep(1) # While the configuration of the CGDCONT register survives resets, # the other configurations don't. So just set them all up every time. print("Configuring LTE ", end='') #lte.send_at_cmd('AT+CGDCONT=1,"IP","pse.telus.iot"') print(".", end='') #lte.send_at_cmd('AT!="RRC::addscanfreq band=28 dl-earfcn=9410"') print(".", end='') #lte.send_at_cmd('AT+CFUN=1') print(" OK") # If correctly configured for carrier network, attach() should succeed. if not lte.isattached(): print("Attaching to LTE network ", end='') lte.attach(band=28,apn="pse.telus.iot") while(True): if lte.isattached(): print(" OK") break print('.', end='') pycom.rgbled(0xFFFFFF) time.sleep(1) # Once attached, connect() should succeed. if not lte.isconnected(): print("Connecting on LTE network ", end='') lte.connect() while(True): if lte.isconnected(): print(" OK") pycom.rgbled(0xFF00FF) break print('.', end='') time.sleep(1) # Once connect() succeeds, any call requiring Internet access will # use the active LTE connection. return lte # Clean disconnection of the LTE network is required for future # successful connections without a complete power cycle between. def endLTE(): print("Disonnecting LTE ... ", end='') lte.disconnect() print("OK") time.sleep(1) print("Detaching LTE ... ", end='') lte.dettach() print("OK") # Sets the internal real-time clock. # Needs LTE for Internet access. # Program starts here. lte=getLTE() def sub_cb(topic, msg): print((topic, msg)) #c.publish(TOPIC, b"off") if msg ==(b'1'): print("yes") p_out(False) c.publish(b"relayon", b"on") time.sleep(10) p_out(True) c.publish(b"relayoff", b"off") def main(server="*******"): c.set_callback(sub_cb) c.connect() c.subscribe(b"relay") #c.publish(b"relayon", b"on") while True: if True: # Blocking wait for message gc.collect() c.check_msg() print (gc.mem_free()) time.sleep(1) else: # Non-blocking wait for message c.check_msg() time.sleep(1) c.disconnect() if __name__ == "__main__": main()```