Why does this crash after 5 activations?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Why does this crash after 5 activations?

Post by brett » Tue Feb 28, 2017 12:36 am

Forgive me for posting this again as a new topic.

Ok…I’m still struggling. The program will send both a help request and an emergency request. Where it gets weird is when you make 5 consecutive activations of combinations of help and emergency requests.

I’ve tried these combinations:

Help, help, help, help, help
Emergency, help, help, help, help
Help, emergency, emergency, emergency, emergency
Emergency, help, Emergency, help, Emergency
Help, emergency, help, emergency, help


After these combinations I get the following error message:

Traceback (most recent call last):
File "main.py", line 61, in <module>
File "urequests.py", line 104, in post
File "urequests.py", line 51, in request
OSError: -2

After the second emergency request I get the following error message:

ets Jan 8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 32028, room 16
tail 12
chksum 0x40
ho 0 tail 12 room 4
load 0x3ffe8000, len 1092, room 12
tail 8
chksum 0x17
load 0x3ffe8450, len 3000, room 0
tail 8
chksum 0x56
csum 0x56

Any thoughts and suggestions? Here is the current code:

Code: Select all

import machine
import network
import time
import urequests
from machine import Timer

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('xxxxxxxxxxxx', 'xxxxxxxxxx')

button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
redled = machine.Pin(5, machine.Pin.OUT)
yelled = machine.Pin(12, machine.Pin.OUT)
grnled = machine.Pin(4, machine.Pin.OUT)

url_01 = 'http://api.pushingbox.com/pushingbox?devid=xxxxxxxxxxxxxxx'
url_02 = 'http://api.pushingbox.com/pushingbox?devid=xxxxxxxxxxxxxx'

buttonCounter = 0
helpFlag = False


def helpRequest():
    if buttonCounter == 1:
        global helpFlag
        helpFlag = True
    elif buttonCounter == 2:
        global helpFlag
        helpFlag = True

def emergencyRequest():
    from machine import Timer
    print('Emergency')
    resp = urequests.post(url_02)
    redled(1)
    tim2 = Timer(1)
    tim2.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:redled(0))

def buttonReset(self):
    helpRequest()
    global buttonCounter
    buttonCounter = 0
    print('reset')
    print(buttonCounter)


while True:
    first = button.value()
    time.sleep(0.05)
    second = button.value()
    if first and not second:
        print('Button pressed!')
        buttonCounter = buttonCounter + 1
        print(buttonCounter)
        tim3 = Timer(2)
        tim3.init(period=2000, mode=Timer.ONE_SHOT, callback=buttonReset)
    if buttonCounter >= 3:
        emergencyRequest()
        buttonCounter = 0
    if helpFlag == True:
        resp = urequests.post(url_01)
        helpFlag = False
        print('Help 2')
        yelled(1)
        tim = Timer(-1)
        tim.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:yelled(0))


    if wlan.isconnected() == True:
        grnled(1)
    elif wlan.isconnected() == False:
        grnled(0)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why does this crash after 5 activations?

Post by pythoncoder » Wed Mar 01, 2017 7:14 am

I can't see anything obviously wrong, although I do think the code has scope for simplification.

I'd concentrate on the simplest case which is a failure after five help requests. The question which comes to mind is whether this results from the WiFi comms. To check this I'd write a simple loop which issues a number of help requests:

Code: Select all

for _ in range(10):
    urequests.post(url_01)
    time.sleep(3)
If this crashes it proves that the fault is in the comms rather than the program logic. Is there a server issue? Is the WiFi flaky?

If it doesn't crash then the fault is presumably in the program logic. I'd then comment out the urequests, replacing them with print statements, and set about debugging the logic.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why does this crash after 5 activations?

Post by pythoncoder » Wed Mar 01, 2017 7:51 am

I couldn't resist simplifying this ;) The following is untested so may have bugs, but at least it gives the gist of how the code might be simplified.

Code: Select all

button_counter = 0
run_flag = False

def run(t):
    global run_flag
    run_flag = True

while True:
    global run_flag, button_counter
    first = button.value()
    time.sleep(0.05)
    if first and not button.value():
        print('Button pressed!')
        button_counter += 1
        print(button_counter)
        tim3 = Timer(-1)
        tim3.init(period=2000, mode=Timer.ONE_SHOT, callback=run)

    if run_flag:
        run_flag = False 
        if button_counter >= 3:
            print('Emergency')
            resp = urequests.post(url_02)
            redled(1)
            tim2 = Timer(-1)
            tim2.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:redled(0))
        else:  # At least one button must have been pressed to get here
            print('Help')
            resp = urequests.post(url_01)
            yelled(1)
            tim = Timer(-1)
            tim.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:yelled(0))
        button_counter = 0
    grnled(wlan.isconnected())
Note that I'm using virtual timers throughout.
Peter Hinch
Index to my micropython libraries.

Post Reply