Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Mqtt subscribe stops receiving

    Scheduled Pinned Locked Moved GPy
    1 Posts 1 Posters 112 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      gt1
      last edited by

      Hi all,

      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()```
      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post

      Pycom on Twitter

      Pycom