Wipy transmits data for 4 hours then stops



  • My code below appears to work for only 4 hours, then the Wipy stops sending data over MQTT. Any ideas why it stops - runs out of memory?

    import pycom
    import time
    from machine import Pin
    from dth import DTH
    from simple import MQTTClient
    
    def settimeout(duration):
       pass
    
    client = MQTTClient(client_id="wipy_client", server='192.168.0.XX', port=1883)  #
    
    client.settimeout = settimeout
    client.connect()
    
    pycom.heartbeat(False)
    pycom.rgbled(0x000008) # blue
    th = DTH(Pin('P3', mode=Pin.OPEN_DRAIN),0)
    time.sleep(2)
    
    while True:
        result = th.read()
        if result.is_valid():
            pycom.rgbled(0x001000) # green
            print("Temperature: %d C" % result.temperature)
            client.publish("wipy/Temperature", str(result.temperature))
            print("Humidity: %d %%" % result.humidity)
            client.publish("wipy/Humidity", str(result.humidity))
        time.sleep(1)
    


  • @robmarkcole Using robust.py, I'm seeing the same problems, but now I'm getting a good error message back. Typically after a few hours I get (continuously repeated):
    mqtt reconnect: OSError(105,)
    So, I should be able to catch that and then kill and then restart the session.



  • @pwest I'm no expert on MQTT, but believe it an be configured to deal with lost connections etc.
    The upcoming pybytes platform may also be of interested.
    Keep us posted with your progress :)



  • @pwest I now have it running w/robust.py 79 mins so far.



  • @robmarkcole Running a local broker would be easy for me, but my ultimate architecture requires that I publish from multiple locations to a remote commercial system (I had been planning to use AWS services). A reliable broker might largely solve my problem, but I'm thinking that I'd like my application to gracefully deal with lost connections, timeouts, etc.: hive and mosquitto seem to provide these issues for free!
    The first error message associated with my current problem seems to be:

    Traceback (most recent call last):
    File "main.py", line 108, in new_adv_event
    File "/flash/lib/umqtt.py", line 110, in publish
    OSError: [Errno 104] ECONNRESET

    So, it looks like the connection is getting reset, and I'm not handling that. Hopefully, this is one of the things that the robust mqtt code will help with.



  • @pwest There are usually limitaitons with the free brokers, wrote a bit more about my experience on https://www.hackster.io/robin-cole/micropython-leak-detector-with-adafruit-and-home-assistant-a2fa9e

    I am running a broker in Docker on my NAS, very easy to setup https://hub.docker.com/_/eclipse-mosquitto/

    If you have a pi to dedicate you could install Hassio and run the MQTT broker
    https://home-assistant.io/addons/



  • @robmarkcole It looks like you are posting to a local broker. I've been publishing to either broker.hivemq.com or test.mosquitto.org and my sessions always terminate with some kind of a network error, usually after a few hours. I think I need to look into the robust mqtt library, but I'm having trouble getting started...



  • Hi Phil
    my script will run without any interruption indefinitely now. The wipy isn't connected to my computer but running off the mains.
    Hope that helps.
    Cheers



  • @robmarkcole :
    Are you still experiencing your original problem? With a similar program, I generally get well over 4 hours, but rarely over 20 hours before my program fails. I'm pretty sure it's related to socket timeout problems that are not handled by the simple umqtt library.

    -Phil



  • Thanks @bucknall have edited the code and after an initial reduction in memoryt the available memory plateaus with slight variations depending on the value of variables. cheers for the advice

    import pycom
    import time
    from machine import Pin
    import gc
    from dth import DTH
    from simple import MQTTClient
    
    gc.enable()  # enable auto garbage collection
    
    def settimeout(duration):
       pass
    
    client = MQTTClient(client_id="wipy_client", server='192.168.0.XX', port=1883)  #
    
    client.settimeout = settimeout
    client.connect()
    
    pycom.heartbeat(False)
    pycom.rgbled(0x000008) # blue
    th = DTH(Pin('P3', mode=Pin.OPEN_DRAIN),0)
    time.sleep(2)
    
    while True:
        result = th.read()
        if result.is_valid():
            pycom.rgbled(0x001000) # green
            gc.collect()   # perform garbage collection
            print(gc.mem_free())
            client.publish("wipy/Memory", str(gc.mem_free()))
            print("Temperature: %d C" % result.temperature)
            client.publish("wipy/Temperature", str(result.temperature))
            print("Humidity: %d %%" % result.humidity)
            client.publish("wipy/Humidity", str(result.humidity))
        time.sleep(5)


  • This post is deleted!


  • Hi @robmarkcole,

    You could check for the memory usage by transmitting the gc.mem_free() value over MQTT along with your other messages.

    From your code, I would imagine the device memory is likely the problem! You can free this up by manually using the garbage collector or eliminating the point in your code that's forcing the memory issues.

    Thanks,

    Alex


Log in to reply
 

Pycom on Twitter