Onewire, Wifi, MQTT, crash...



  • Hi!

    I finally had the time to go further with my project with the Lopy : reading temperature from a DS18B20 (onewire) sensor, and send it to my MQTT broken on the LAN via WIFI.

    Ok, I manage to read the temperature by using the code from this forum post : https://forum.pycom.io/topic/303/ds18b20-temperature-sensor. It works perfectly, I can leave it running for a long time without any issue.

    Wifi is quite straightforward, and I used this a MicroPython library for MQTT : https://pypi.python.org/pypi/micropython-umqtt.simple/1.3.2. I did a simple test program that send a value on the broker, and it can run forever too.

    But... when I read the temperature and send it on MQTT in a loop... it freezes after 2-3 minutes.

    On a firmware version from late december 2016 (1.4 or 1.5 something), a lot of messages were printed on the console when it crashed :

    Running temperature_wifi_app.py
    Soft resetting the LoPy
    Connecting to WIFI...
    I (10786) wifi: mode : sta (24:0a:c4:00:c0:52)
    I (12716) wifi: n:13 0, o:6 0, ap:255 255, sta:13 0, prof:6
    I (13690) wifi: state: init -> auth (b0)
    I (13693) wifi: state: auth -> assoc (0)
    I (13697) wifi: state: assoc -> run (10)
    I (13710) wifi: connected with Zataboy-2.4Ghz, channel 13
    WIFI OK
    Connecting to MQTT
    MQTT OK
    Init temperature sensor..
    I (24082) wifi: pm open, type:0, st: 0, sending null data: 0
    
    E (306439) wifi: error malloc bcn_param
    
    E (306542) wifi: error malloc bcn_param
    
    E (306644) wifi: error malloc bcn_param
    
    E (306747) wifi: error malloc bcn_param
    
    E (306849) wifi: error malloc bcn_param
    
    E (306951) wifi: error malloc bcn_param
    
    E (308078) wifi: error malloc bcn_param
    
    E (308180) wifi: error malloc bcn_param
    
    E (308283) wifi: error malloc bcn_param
    
    E (308385) wifi: error malloc bcn_param
    
    E (308487) wifi: error malloc bcn_param
    
    E (308590) wifi: error malloc bcn_param
    
    E (310023) wifi: error malloc bcn_param
    
    E (310126) wifi: error malloc bcn_param
    
    E (310228) wifi: error malloc bcn_param
    
    E (311355) wifi: error malloc bcn_param
    
    E (311457) wifi: error malloc bcn_param
    
    [[[ EDIT : a lot more of this message]]]
    
    E (413652) wifi: error malloc bcn_param
    
    E (413754) wifi: error malloc bcn_param
    
    E (414983) wifi: error malloc bcn_param
    
    E (415085) wifi: error malloc bcn_param
    
    E (415188) wifi: error malloc bcn_param
    
    E (415290) wifi: error malloc bcn_param
    
    E (415392) wifi: error malloc bcn_param
    
    E (416621) wifi: error malloc bcn_param
    
    E (416724) wifi: error malloc bcn_param
    
    E (416826) wifi: error malloc bcn_param
    
    E (416928) wifi: error malloc bcn_param
    
    E (417031) wifi: error malloc bcn_param
    
    I (417142) wifi: state: run -> auth (2c0)
    I (417142) wifi: n:13 0, o:13 0, ap:255 255, sta:13 0, prof:6
    I (417144) wifi: pm close, st: 7, sending null data: 0, total sleep time: 0/393061512
    
    I (417264) wifi: n:13 0, o:13 0, ap:255 255, sta:13 0, prof:6
    I (417264) wifi: state: auth -> auth (b0)
    I (417267) wifi: state: auth -> assoc (0)
    I (417270) wifi: state: assoc -> run (10)
    I (417283) wifi: connected with Zataboy-2.4Ghz, channel 13
    Traceback (most recent call last):
      File "<stdin>", line 39, in <module>
      File "mqtt/mqtt_simple.py", line 110, in publish
    OSError: [Errno 113] EHOSTUNREACH
    >
    MicroPython v1.8.6-264-gadacebe on 2016-12-22; LoPy with ESP32
    Type "help()" for more information.
    >>> I (427271) wifi: pm open, type:0, st: 0, sending null data: 0   
    

    I've just upgraded to firmware version 1.6.1.b1. It still crashes, with no message on the console.

    Here is the code of the full application:

    from network import WLAN
    from mqtt.mqtt_simple import MQTTClient
    
    import machine
    import time
    import sys  
    sys.path.insert(0, '/flash/DS18B20')
    import ds18x20, machine
    
    print("Connecting to WIFI...")
    
    wlan = WLAN(mode=WLAN.STA)
    wlan.connect("Zataboy-2.4Ghz",  auth=(WLAN.WPA2,  "AZERTYUIOP"))
    
    while not wlan.isconnected():
    	time.sleep(1)
    
    print("WIFI OK")
    
    print("Connecting to MQTT")
    
    client = MQTTClient("lopy",  "192.168.1.109",  port=1883)
        
    client.connect()
    
    print("MQTT OK")
    
    print("Init temperature sensor..")
    
    temp = ds18x20.DS18X20(machine.Pin('G22',machine.Pin.OPEN_DRAIN, machine.Pin.PULL_UP))
    
    
    tempValue = 0.0
    while True:
        tempValue = temp.read_temp()
        client.publish("/test",  str(tempValue))
        time.sleep(1)
    

    I copied the code for onewire/ds18b20 in my Github repo : https://github.com/JF002/lopy-snippets/tree/master/DS18B20.
    And the MQTT library : https://github.com/JF002/lopy-snippets/tree/master/mqtt

    Do any of you experience this kind of error? What could I try in order to fix this?

    Thanks for your help!



  • The freeze with gc.collect seems to be a bug in the firmware. @daniel is already working on it : https://forum.pycom.io/post/4219



  • @livius Yes, there is a resistor between 3.3V and the data pin.



  • @JF002
    did you use also pullup resistor?



  • It's a DS18B20. It looks like a transistor, and is not waterproof. Here is an example : https://tushev.org/images/electronics/arduino/ds18x20/DS18B20.jpg

    EDIT : after a closer look, it's a DS1820 (w/o the B). Does it change anything?

    I tried to connect the DATA pin on G17 (P10) and G22, with no luck.

    Did you try the gc.collect thing on this version of the firmware?



  • @JF002
    yes, it work for me
    which sensor do you have - waterproof?
    but i use both and work for me

    example data i got:

    [UID=24:0a:c4:00:6e:c2] 2162, 22, 35, 0
    [UID=24:0a:c4:00:6e:c2] 2162, 23, 35, 0
    [UID=24:0a:c4:00:6e:c2] 2162, 24, 35, 0
    [UID=24:0a:c4:00:6e:c2] 2162, 25, 35, 0
    [UID=24:0a:c4:00:6e:c2] 2162, 26, 35, 0
    

    first is from ds18X20 waterproof
    21,62 C



  • Thank you @livius for you help. I didn't know there was an example for this in the repo from Pycom.

    Anyway, I tried the OneWire and DS18X20 modules, but it always returns 60.25 or -7.5... while the ambient temperature should be around 19°C for now... Is this example working for you?

    As for the memory, I used gc.mem_free() to see the available RAM memory, and as you said, it dropped very quickly. So, gc.collect() could be a workaround, but it just seem to freeze the Lopy... (it does not respond and I have to reset it with the reset button).

    I'm using firmware version 1.6.1.b1.



  • @JF002 said in Onewire, Wifi, MQTT, crash...:

    for ds18b20 look here:
    https://github.com/pycom/pycom-libraries/tree/master/examples/onewire

    there are few fixes (some mine ;-) ) e.g. for negative temperature
    and working with many roms
    also look at not merged pull request
    https://github.com/pycom/pycom-libraries/pull/2/files

    you then can worok with driver with many roms and with float or integer calculations

    I recommend use gc.collect() because mqtt use more big amout of memory
    and i suppose you go into 0 free mem
    look at free mem:
    gc.mem_free()



Pycom on Twitter