Deepsleep stops execution of main



  • Hello Pycom

    I am currently working on reducing the amount of power that my SiPy uses.

    I am making periodic readings and uploading to a database using MQTT and a wifi connection. Once done uploading i want to have my device in deepsleep for a period of time before redoing the whole proces again.

    The code functions like i want it to with one exception: After completing a deepsleep my SiPy stops executing main.py in >95% of the cases, but every now and then it functions properly, restarting main.py once it has finished a deepsleep.

    According to the documentation deepsleep is supposed to: "resume execution from the main script just as with a reset" Which i assume means that it will execute the script from the top starting over.

    Most of the time, my result is that it runs the code once and then does nothing.
    Below is my code for main.py and boot.py and at the bottom is the output from the Pymakr console.

    Main.py

    import machine
    from machine import I2C
    import time
    import pycom
    from simple import MQTTClient
    from network import WLAN
    
    DISCONNECTED = 0
    CONNECTING = 1
    CONNECTED = 2
    DEVICE_ID = "4D2A4D"
    HOST = "xxxxxxxxx.iot.eu-west-1.amazonaws.com" #not real endpoint!
    TOPIC = "myThingName"
    WLANNAME = "SDU-GUEST"
    state = DISCONNECTED
    connection = None
    deepSleep = 5000
    counter  = 0
    
    wlan = WLAN(mode=WLAN.STA)
    pycom.heartbeat(False)
    
    i2c = I2C(0)
    i2c.init(I2C.MASTER,  baudrate=9600,  pins=("P9", "P19"))
    
    def run():
        global deepSleep
        global state
        global connection
        global counter
    
        while True:
            while state != CONNECTED:
                while not wlan.isconnected():
                    nets = wlan.scan()
                    for net in nets:
                        if net.ssid == 'SDU-GUEST':
                            print('Network found!')
                            wlan.connect(net.ssid, auth=(net.sec, ''), timeout=30000)
                            while not wlan.isconnected():
                                machine.idle() # save power while waiting
                            print('WLAN connection succeeded!')
                            break
                try:
                    state = CONNECTING
                    print("CONNECTING TO MQTT")
                    connection = MQTTClient(client_id=DEVICE_ID, server=HOST, port=8883, keepalive=10000, ssl=True, ssl_params={"certfile":"/flash/cert/cert.pem", "keyfile":"/flash/cert/private.key", "ca_certs":"/flash/cert/ca.pem"})
                    connection.connect()
                    print("MQTT CONNECTED")
                    state = CONNECTED
                except:
                    print('ERROR')
                    time.sleep(2)
                    continue
    
            while state == CONNECTED:
                counter += 1
                if counter >= 10:
                    deepSleep = 1800000
                print("COUNTER: %i"%(counter)) #Does it save values in ram during deepsleep? I dont think so.
                i2c.writeto_mem(112, 0x00, b'\x51')
                # wait for the sensor to complete its reading
                time.sleep(0.1)
                x = i2c.readfrom_mem(112, 0x02, 2)
                time.sleep(0.01)
                dist = x[1] | x[0] << 8
                print("DISTANCE: %i"%(dist))
                msg = '{"device":"%s", "DeepsleepDuration":"%i" ,"Data":"%s"}'%(DEVICE_ID,deepSleep,  dist)
                connection.publish(topic=TOPIC, msg=msg, qos=0)
                print('SENDING: %s'%(msg))
                time.sleep(5.0)
                print("DEEPSLEEPING FOR: %i"%(deepSleep))
                machine.deepsleep(deepSleep)
                print("continue?")
    time.sleep(5)
    run()
    

    Boot.py

    known_nets = [('AndroidAP', 'zvxs8927'), ('SDU-GUEST', '')] # change this line to match your WiFi settings
    
    import machine
    import os
    
    uart = machine.UART(0, 115200) # disable these two lines if you don't want serial access
    os.dupterm(uart)
    
    if machine.reset_cause() != machine.SOFT_RESET: # needed to avoid losing connection after a soft reboot
        from network import WLAN
        wl = WLAN()
    
        # save the default ssid and auth
        original_ssid = wl.ssid()
        original_auth = wl.auth()
    
        wl.mode(WLAN.STA)
    
        available_nets = wl.scan()
        nets = frozenset([e.ssid for e in available_nets])
    
        known_nets_names = frozenset([e[0] for e in known_nets])
        net_to_use = list(nets & known_nets_names)
    
        try:
            net_to_use = net_to_use[0]
            pwd = dict(known_nets)[net_to_use]
            sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
            wl.connect(net_to_use, (sec, pwd), timeout=10000)
        except:
            wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)
    

    Here is a dump of what pymakr gets over the UART connection when i run my code.

    Running main.py
    I (1425042) wifi: sleep disable
    CONNECTING TO MQTT
    MQTT CONNECTED
    COUNTER: 1
    DISTANCE: 0
    SENDING: {"device":"4D2A4D", "DeepsleepDuration":"5000" ,"Data":"0"}
    DEEPSLEEPING FOR: 5000
    I (1439918) wifi: state: run -> init (0)
    I (1439918) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:6
    I (1439919) wifi: pm stop, total sleep time: 0/1424973621
    
    E (1439922) wifi: esp_wifi_connect 806 wifi not start
    ets Jun  8 2016 00:22:57
    
    rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0x00
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:QIO, clock div:2
    load:0x3fff9010,len:8
    load:0x3fff9018,len:248
    load:0x40078000,len:4056
    load:0x4009fc00,len:920
    entry 0x4009fde4
    I (1541) wifi: wifi firmware version: 2a22b2d
    I (1557) wifi: pp_task_hdl : 3ffd6bfc, prio:23, stack:8192
    I (1557) wifi: Init lldesc rx mblock:10
    I (1557) wifi: Init lldesc rx ampdu len mblock:7
    I (1559) wifi: Init lldesc rx ampdu entry mblock:4
    I (1564) wifi: sleep disable
    I (2552) wifi: frc2_timer_task_hdl:3ffdcc4c, prio:22, stack:2048
    I (2556) wifi: mode : softAP (24:0a:c4:00:f6:1d)
    I (2610) wifi: mode : sta (24:0a:c4:00:f6:1c)
    I (2610) wifi: sleep disable
    MicroPython v1.8.6-493-g99ac80fe on 2017-03-03; SiPy with ESP32
    Type "help()" for more information.
    >>> I (4940) wifi: n:1 0, o:6 0, ap:255 255, sta:1 0, prof:6
    I (4941) wifi: state: init -> auth (b0)
    I (4942) wifi: state: auth -> assoc (0)
    I (4945) wifi: state: assoc -> run (10)
    I (4945) wifi: connected with SDU-GUEST, channel 1
    I (14945) wifi: pm start, type:0
    

    It looks like it boot.py always runs after a deepsleep, as the wifi connection gets reset after deepsleeping.

    If anyone knows how to make sure that main.p resumes after deepsleeping i would very much appreciate the help.

    Thanks for reading :)



  • @_peter
    this is not the issue.
    Deepsleep work like "reset" with one exception - you can use pin hold to "remember pin state" but whole programm start as fresh
    boot.py then main.py



  • @YoungChul i have the same problem on LoPy board


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.