Deepsleep hangs system



  • Hi,

    After getting my aplication to work on USB power I tried starting battery powered operation. This was (and is) not a big success to say the least....
    I am using a Lopy , a deepsleep shield and tried both, a version 2 and 3 expansion board.
    I use the code below to setup the lopy:
    As soon as the last line of this code is called the unit stops, independent of the code following this line......
    As said: this only happens when on battery power not on USB power.
    If I comment out this line (and any other ds related comments) the unit connects to TTN, no problem.
    I am lost please help!!!

    Arno

    import machine
    from network import WLAN
    from network import LoRa
    from machine import Pin, Timer, UART, PWM, I2C
    from deepsleep import DeepSleep
    import deepsleep
    import struct
    import binascii
    import socket
    import pycom
    import time
    import _thread
    import utime
    import array
    
    # Turn off hearbeat LED
    pycom.heartbeat(False)
    ds = DeepSleep()
    
    """i2c = I2C(0, pins=(Pin.module.P21, Pin.module.P20)) # SCL=A8, SDA=C9
    print(i2c.scan())
    
    import at24c32n
    eeprom = at24c32n.AT24C32N(i2c)
    # 80 == 0x50 == AT24C32N EEPROM
    # 104 == 0x68 == DS1307
    
    import ds1307
    dst = ds1307.DS1307(i2c)
    print(dst.datetime())
    dst.halt(False) """
    
    
    
    
    
    off = 0x000000
    red = 0xff0000
    green = 0x00ff00
    blue = 0x0000ff
    pycom.rgbled(0xf0000f)
    
    
    # wake up the unit with P17 or P18
    ds.enable_pullups('P17')
    ds.enable_pullups('P18')
    # set a signal to detect the progress
    pycom.rgbled(green)
    time.sleep(3.0)
    # get the wake reason and the value of the pins during wake up
    wake_s = ds.get_wake_status()code_text
    


  • @robert-hh Good piece of information posted here.



  • Hi
    Thanks for the advise. It would be useful if it would bring me a bit further.
    Updating the Fw to version 1.18.2.r6 did not help me much. I removed the deepsleep board and dowloaded and installed the pycoproc.py file. Now I get the (infamous) Exception: Board not detected: I2C bus error.... Searched the forum for a solution to no avail..
    Working on it for 3 hours but still do not have a solution..



    1. If I look at the Pycoproc code it seems only for the pysense and pytrack boards. There is no reference to the expansion board. Will it still work and will it bring my combination (with deepsleep shield) into deepsleep / low power mode?

    Shall work. They moved the common part into pycoproc when they planned additional boards with the PIC (pysense, expension 3.x).

    1. Do I still need the deepsleep shield with the version 3.1 expansion board?

    No. Maybe this is your current problem. On both is a PIC microcontroler which shall handle the deepsleep. Talking to both at the same time may issue a lot of problems.



  • @crumble

    Thanks for the clarification. I will update the firmware on the Lopy and try again.
    Maybe you can also answer some related questions:

    1. If I look at the Pycoproc code it seems only for the pysense and pytrack boards. There is no reference to the expansion board. Will it still work and will it bring my combination (with deepsleep shield) into deepsleep / low power mode?
    2. Do I still need the deepsleep shield with the version 3.1 expansion board?
    3. Is it possible to extract the elapsed or remaining time when the wakeup is through a pin level change? This was not possible with the previous combination with the version 2 expansion board but would help me a lot...

    An update on the docs.pycom.io site would also help.....



  • @Arno

    Your LoPy firmware is quite old. pycom has unified the board access a few versions ago. They use now the class Pycoproc on all boards with a PIC. Deepsleep is now splitted into two functions. setup_sleep and go_to_sleep.



  • Hi My Lopy firmware version is 1.15.0.b1.
    the firmware version of the 3.1 board is:0.0.11 (the latest version
    I add the complete code of which most is commented out. In this shape the code just sets up OTAA connection not more.

    # main.py -- put your code here!!
    import machine
    from network import WLAN
    from network import LoRa
    from machine import Pin, Timer, UART, PWM, I2C
    from deepsleep import DeepSleep
    import deepsleep
    import struct
    import binascii
    import socket
    import pycom
    import time
    import _thread
    import utime
    import array
    
    # Turn off hearbeat LED
    pycom.heartbeat(False)
    ds = DeepSleep()
    
    """i2c = I2C(0, pins=(Pin.module.P21, Pin.module.P20)) # SCL=A8, SDA=C9
    print(i2c.scan())
    
    import at24c32n
    eeprom = at24c32n.AT24C32N(i2c)
    # 80 == 0x50 == AT24C32N EEPROM
    # 104 == 0x68 == DS1307
    
    import ds1307
    dst = ds1307.DS1307(i2c)
    print(dst.datetime())
    dst.halt(False) """
    
    
    
    
    
    off = 0x000000
    red = 0xff0000
    green = 0x00ff00
    blue = 0x0000ff
    pycom.rgbled(0xf0000f)
    
    
    # wake up the unit with P17 or P18
    ds.enable_pullups('P17')
    ds.enable_pullups('P18')
    # get the wake reason and the value of the pins during wake up
    pycom.rgbled(green)
    time.sleep(3.0)
    wake_s = ds.get_wake_status()
    # print(wake_s)
    # ds.enable_wake_on_raise('P10')
    # ds.enable_wake_on_fall('P17')
    # ds.enable_wake_on_fall('P18')
    
    # ds.go_to_sleep(10)  # go to sleep for 10 seconds
    # if wake_s["P10"] == 1 : sys.exit()
    
    # print("P17= ", wake_s["P17"],"  P18= ",wake_s["P18"])
    
    # print(ds.get_wake_status())
    
    
    
    
    
    
    timing1 = [[0,-1],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
    # define intermediate array to store time of entry, could be one dimensional
    timing2 = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
    
    
    """to be used with switch pulling to ground
    p_in_G30 = Pin(Pin.exp_board.G30, mode=Pin.IN, pull=Pin.PULL_UP)
    p_in_G31 = Pin(Pin.exp_board.G31, mode=Pin.IN, pull=Pin.PULL_UP)
    """
    #if wake_s['wake'] == deepsleep.PIN_WAKE:
        #retreive timing1 and 2 from EEPROM
    """ str1 = eeprom.read(0,245)
        str1 = str1.decode('ascii')
        l_str1 = str1.find("]]")+2
        timing1 = list(eval(str1[0:l_str1]))
    
        str2 = eeprom.read(250,500)
        str2 = str2.decode('ascii')
        l_str2 = str2.find("]]")+2
        timing2 = list(eval(str2[0:l_str2]))
        i1 = timing1[0][1]
    
        i2 = timing1[1][1]
        # here needs to be a while loop
        print(timing1)
        print(timing2)
        print("----------------------")
        # i1 = number of ows inside the box
        #timing1  contains the time stamp of the latest passage related to the number of ows inside the box
        # timing2 contains the reside time and number of passages for 1,2,3.. ows in box
        #timing2 is the array to be transmitted
    
         Not needed anymore as we set datetime to 0 at end of routing
        for i in range(0,len(timing2)):
            timing1[i][0] = dst.datetime()[5]*60 + dst.datetime()[6]
        time1 = dst.datetime()[5]*60 + dst.datetime()[6]
    
    
    
        # while dst.datetime()[5]*60 + dst.datetime()[6] < 20:
        # while i2 < 6:
        time1 = dst.datetime()[5]*60 + dst.datetime()[6] """
    """    if wake_s["P17"] == 0: # so pin pulled to zero
            #chrono.start()
            i1 += 1
    
            pycom.rgbled(blue)
    
    
        if wake_s["P18"] == 0: #so pin pulled to zero
            #chrono.stop()
            pycom.rgbled(red)
    
            ds.go_to_sleep(10) """
    
    
    
    
    """
    elif wake_s['wake'] == deepsleep.TIMER_WAKE:
    
        print("time expired")
    
        pycom.rgbled(off) """
    
    """
        #   write to SD card
        f = open('/sd/test.txt', 'a')
        f.write(' '.join(str(s) for s in timing2) + '\n')
        f.close()
        f = open('/sd/test.txt', 'r')
        print('outcome is:')
        print(f.readall())
        f.close()
    
    
        b = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        for i in range(0,len(timing2)-1,1):
             eerste element opdelen in 2 bytes door //256 en % 256
            b[i*3] = timing2[i][0] // 256
            b[i*3+1] = timing2[i][0] % 256
            b[i*3+2] = timing2[i][1]
    
        l_b = 0
        for i in range(0,len(b),1):
            if b[i] != 0: l_b = i
    
        B1 = bytes(array.array('B',b[0:l_b+1])) """
    """
        B1 = hex(10)
        lora = LoRa(mode=LoRa.LORAWAN)
        lora.nvram_restore()
        s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        s.setblocking(True)
        if lora.has_joined:
            print(lora.has_joined())
            print(B1)
            s.send(B1)
        else:
            # Initialize LoRaWAN radio
            lora = LoRa(mode=LoRa.LORAWAN)
            print(binascii.hexlify(lora.mac()).upper().decode('utf-8'))
            # Set network keys
            dev_eui = binascii.unhexlify('00D85D973D86EE6F')
            app_eui = binascii.unhexlify('70B3D57ED000A657')
            app_key = binascii.unhexlify('4317A37691B9333FC62842FEBADE5F78')
    
            # Join the network
            lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)
            while not lora.has_joined():
                print('Not joined yet...')
                pycom.rgbled(off)
                time.sleep(1.0)
                pycom.rgbled(red)
                time.sleep(1.0)
    
            print('Joined')
            pycom.rgbled(blue)
            print(app_eui)
            s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
            s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
            s.setblocking(True)
            s.send(B1)
    
    
    
        lora.nvram_save()
        # go to sleep for 1800 second = 0.5 hour
        ds.go_to_sleep(28)
    
    
    else:  # deepsleep.POWER_ON_WAKE:
        print("Power ON reset")
    """
    lora = LoRa(mode=LoRa.LORAWAN)
    if not lora.has_joined():
        lora = LoRa(mode=LoRa.LORAWAN)
        print(binascii.hexlify(lora.mac()).upper().decode('utf-8'))
        # Set network keys
        dev_eui = binascii.unhexlify('00D85D973D86EE6F')
        app_eui = binascii.unhexlify('70B3D57ED000A657')
        app_key = binascii.unhexlify('4317A37691B9333FC62842FEBADE5F78')
    
        # Join the network
        lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)
        while not lora.has_joined():
            print('Not joined yet...')
            pycom.rgbled(off)
            time.sleep(1.0)
            pycom.rgbled(red)
            time.sleep(1.0)
    
        print('Joined')
        pycom.rgbled(blue)
        print(app_eui)
        s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        s.setblocking(True)
        lora.nvram_save()
    
    


  • @Arno

    I noticed two problems with deepsleep over the PIC. i2c can fail or the board never goes to sleep, so it will not wake up. I assume it is as well a communication problem between ESP and PIC, but without an exception.

    The internal watchdog can get you out of both states. Deepsleep works best with latest firmware versions. Some report that other stuff may not work, so you have to try that for your own configuration.

    Please add beside the missing source part the the firmware versions of LoPy and PIC.



  • @Arno The code you post seems incomplete. At least the last line looks strange.


Log in to reply
 

Pycom on Twitter