How can I use urequests module?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

How can I use urequests module?

Post by EDW26 » Mon Feb 04, 2019 4:30 pm

I am using ESP32 and it seems to me that there is a memory error. I believe it's due to the library / lib I see when I use the command ampy --port / dev / ttyUSB0 ls. I'm trying to use the following code:
import urequests as re
r = re.get ('site where I get get')
and appears:
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "ureque.py", line 139, in get
   File "ureque.py", line 122, in request
   File "ureque.py", line 66, in request
OSError: [Errno 12] ENOMEM

GryKyo
Posts: 9
Joined: Sat Dec 15, 2018 12:43 am

Re: How can I use urequests module?

Post by GryKyo » Sun Feb 10, 2019 10:47 pm

I am having similar memory related problems using the urequests module.

I am very new but surprised myself by getting this to work at all! I am parsing a JSON string which I receive in a MQTT callback function. Within this function I check the topic and do A or B
B is to index a key's value from my dictionary (parsed json) and add it to a Google URL that will write the value to a Google sheets file on Google Drive.
This works once, writes my parsed value to Google Sheets, no problem. The next time I receive the MQTT string I crash with an OS related Memory related error. I have to restart the ESP32 Ctrl+D in the console works to clear the memory issue or it crashes every time.

I don't need the response (which is a screen full of text) so is there a way to disregard or dump the response? Can I use gc.collect() to purge memory or is there a conventional way to avoid memory issues like this?

Any help much appreciated!

Below is the urequest.get and the console error...

Code: Select all


204 response = urequests.get(credentials.google_string + str(log_data))

Traceback (most recent call last):
  File "main.py", line 258, in <module>
  File "main.py", line 255, in <module>
  File "main.py", line 250, in <module>
  File "umqtt/simple.py", line 204, in check_msg
  File "umqtt/simple.py", line 191, in wait_msg
  File "main.py", line 201, in sub_cb
  File "urequests.py", line 108, in get
  File "urequests.py", line 96, in request
  File "urequests.py", line 60, in request
OSError: [Errno 12] ENOMEM
MicroPython v1.10 on 2019-01-25; ESP32 module with ESP32
Type "help()" for more information.
>>> 

GryKyo
Posts: 9
Joined: Sat Dec 15, 2018 12:43 am

Re: How can I use urequests module? [SOLVED]

Post by GryKyo » Mon Feb 11, 2019 10:16 pm

I spotted the fix for this earlier. The memory issue I was having resulted from leaving the request open.

My code had the call:

Code: Select all

response = urequests.get(credentials.google_string + str(log_data))
... in a mqtt callback used to log something to a google sheets file.

I added:

Code: Select all

response.close()
... to the end of the function so it is executed after the data is submitted to the google server. All seems good now.

Post Reply