urequests post stopped working

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

urequests post stopped working

Post by brett » Sun Jan 15, 2017 3:56 am

I'm very new to micropython and python. I've been trying to get the ESP8266 to work with pushingbox. The following code worked for awhile but stopped working today.

[code]import machine
import time
import urequests

url_14 = 'http://api.pushingbox.com/pushingbox?de ... xxxxxxxxxx'


# Buttons

button14 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
led = machine.Pin(2, machine.Pin.OUT)
while True:
first = button14.value()
time.sleep(0.01)
second = button14.value()
if first and not second:
print('Button pressed!')
led(0)
elif not first and second:
print('Button released!')
led(1)
resp = urequests.post(url_14)[/code]

I get the following error when I run the script:

File "<stdin>", line 22, in <module>
File "urequests.py", line 104, in post
File "urequests.py", line 51, in request
OSError: -2

Any help would be greatly appreciated.

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: urequests post stopped working

Post by brett » Tue Jan 17, 2017 12:20 am

I'm an idiot...I forgot that I reflashed the board but didn't reconnect it to the wiFi :roll:

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: urequests post stopped working

Post by kfricke » Tue Jan 17, 2017 8:56 am

Side note: This is an example where the assumption that the ESP automagically will connect to a WLAN can be a bad design decision.
Considering the need to erase the flash when upgrading makes it worse.

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: urequests post stopped working

Post by brett » Mon Jan 23, 2017 1:40 am

Ok...thanks to pythoncoder I got a working program, but I'm having urequests issues again. The post under emergencyRequest works fine but the posts under helpRequest just pauses for a second or two and then the function completes. It seems like it is trying to send it but it doesn't go through. I've tested the url by entering url_01 under emergencyRequest and it works. I've also tried getting rid of everything in the helpRequest function except the urequests post and it still doesn't work. Any thoughts on what is going on?

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('xxxxxxxxxxx', 'xxxxxxxx')

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=xxxxxxxxxxxxxx'
url_02 = 'http://api.pushingbox.com/pushingbox?devid=xxxxxxxxxxxxxx'

buttonCounter = 0


def helpRequest():
    from machine import Timer
    if buttonCounter == 1:
        resp = urequests.post(url_01)
        print('Help 1')
        yelled(1)
        tim = Timer(-1)
        tim.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:yelled(0))
    elif buttonCounter == 2:
        resp = urequests.post(url_01)
        print('Help 2')
        yelled(1)
        tim = Timer(-1)
        tim.init(period=10000, mode=Timer.ONE_SHOT, callback=lambda t:yelled(0))

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 wlan.isconnected() == True:
        grnled(1)
    elif wlan.isconnected() == False:
        grnled(0) 

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: urequests post stopped working

Post by deshipu » Mon Jan 23, 2017 10:38 am

I don't think you can do requests from inside of interrupts.

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: urequests post stopped working

Post by brett » Mon Jan 23, 2017 11:29 am

I was thinking that but the emergency one works. Is that an interrupt?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: urequests post stopped working

Post by deshipu » Mon Jan 23, 2017 12:39 pm

The emergency one is being called from the main loop. The help one is called from a timer interrupt.

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: urequests post stopped working

Post by brett » Mon Jan 23, 2017 12:43 pm

Ahhhh....OK...I will see what I can do about that this evening. Thanks for helping this newb learn!!

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: urequests post stopped working

Post by brett » Thu Feb 23, 2017 2:03 am

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)

Post Reply