Calling an API in a loop is crashing urequests

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
evan55
Posts: 3
Joined: Mon Mar 30, 2020 4:35 pm

Calling an API in a loop is crashing urequests

Post by evan55 » Mon Mar 30, 2020 5:28 pm

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.

evan55
Posts: 3
Joined: Mon Mar 30, 2020 4:35 pm

Re: Calling an API in a loop is crashing urequests

Post by evan55 » Tue Mar 31, 2020 12:23 am

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.

cbenson
Posts: 1
Joined: Tue Mar 31, 2020 12:27 am

Re: Calling an API in a loop is crashing urequests

Post by cbenson » Tue Mar 31, 2020 12:41 am

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

evan55
Posts: 3
Joined: Mon Mar 30, 2020 4:35 pm

Re: Calling an API in a loop is crashing urequests

Post by evan55 » Thu Apr 02, 2020 11:03 pm

Scratch my earlier comment, moving to a D1 Mini just made it take longer until the problem occurred

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Calling an API in a loop is crashing urequests

Post by tve » Fri Apr 03, 2020 4:06 am

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.

HabibJ
Posts: 1
Joined: Fri May 22, 2020 12:17 am

Re: Calling an API in a loop is crashing urequests

Post by HabibJ » Fri May 22, 2020 2:16 am

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')

SukruGokk
Posts: 1
Joined: Sun Jun 13, 2021 2:38 pm

Re: Calling an API in a loop is crashing urequests

Post by SukruGokk » Sun Jun 13, 2021 2:40 pm

I got the same error at deneyap kart. Im trying to send telegram message with api but after first try i got this error;
[Errno 12] ENOMEM

Post Reply