Page 1 of 1

Calling an API in a loop is crashing urequests

Posted: Mon Mar 30, 2020 5:28 pm
by evan55
Hello,
I have some simple code to call an api, looping every 5 seconds.
It successfully calls the API 4 times, but crashes after the 4th time. Hardware is a NodeMCU flashed with latest micropython
Here is the code

Code: Select all

import urequests 
from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
    response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
    if response.status_code == 200:        
        led.value(not led.value())
    sleep(5)
The stacktrace is

Code: Select all

Traceback (most recent call last):
  File "main.py", line 5, in <module>
  File "zoomlight.py", line 18, in <module>
  File "urequests.py", line 149, in get
  File "urequests.py", line 82, in request
OSError: -2
MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
I have tried longer intervals, and also utime. Neither made a difference. Is there some garbage cleanup that isnt happening which is filling up memory every time I call the API?
The line numbers dont seem to correspond to the line numbers in the files (I guess due to the imports? Im a bit of a python noob) so its hard to tell whats actually causing the error

thanks for the help.

Re: Calling an API in a loop is crashing urequests

Posted: Tue Mar 31, 2020 12:23 am
by evan55
well, it appears to be a hardware or flash issue. I uploaded the exact same code to a wemos d1 mini that I had laying around, and it works fine.

pretty bizarre and a bit scary if you ask me.

Re: Calling an API in a loop is crashing urequests

Posted: Tue Mar 31, 2020 12:41 am
by cbenson
I'm getting a similar error. It started with what looks like a crash and reboot when trying to call a thingspeak api. But I can repeat your exact error simply by running:
r = urequests.get('http://www.google.com')
5 times in the repl.

I have a D1 Mini flashed with V1.12

Re: Calling an API in a loop is crashing urequests

Posted: Thu Apr 02, 2020 11:03 pm
by evan55
Scratch my earlier comment, moving to a D1 Mini just made it take longer until the problem occurred

Re: Calling an API in a loop is crashing urequests

Posted: Fri Apr 03, 2020 4:06 am
by tve
I would put my money on a memory allocation failure... See also https://github.com/micropython/micropython/issues/5783 which sounds very similar to me. I try not to say this too often, but the money saved on an esp8266 vs. an esp32 is money you'll spend on aspirin to cure the headaches it causes you... Not saying everything is peachy on the esp32, but at least it's not as miserable.

Re: Calling an API in a loop is crashing urequests

Posted: Fri May 22, 2020 2:16 am
by HabibJ
Hey Evan55,

Is the code to connect to internet located your 'main.py' file? I was getting the same error with a get request to an API using the D1 mini. Sometimes it would work, but only 1 in every 10 tries or so.

I fixed it by copying the code that I used to connect to wifi into the file where I used the get request. Try copying the code that you use to connect to wifi and pasting it into your "zoomlight.py" file.

For me the code I copied over was:

Code: Select all

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.scan()
sta_if.connect('Your SSID', 'Your Password')