Memory allocation issues with urequests.get() #13293
-
Hi, I'm using a WROOM32 to fetch and display electricity prices on a ILI9341 display. I'm trying to fetch the electricity prices from this URL in .json format: Since I'm running both wifi and display driver on the esp32 I seem to be running out of memory with Firmware: import urequests
import micropython
micropython.mem_info()
response = urequests.get("http://www.elprisetjustnu.se/api/v1/prices/2023/12-28_SE3.json")
How should I approach this problem? Since I'm trying to fetch .json formated text, is it possible to fetch only part of the .json and not everything at once? Should i try to implement |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You do not have to use You may want to try the following script: import time, network, socket, ssl
# Connect to WiFI
network.WLAN(network.AP_IF).active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(pm=0) # disable power management
wlan.config(txpower=20)
wlan.connect("YOUR_SSID", "YOUR_PWD")
time.sleep(5)
print(wlan.ifconfig())
srv = 'www.elprisetjustnu.se'
port = 443 # https port number
addr = socket.getaddrinfo(srv, port)[0][-1]
# Connect to htps
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect(addr)
time.sleep(1)
s = ssl.wrap_socket(ss)
# Send request and get reply
s.write(b'GET /api/v1/prices/2023/12-28_SE3.json HTTP/1.1\r\nHost: www.elprisetjustnu.se\r\n\r\n')
data = s.read()
for d in data.decode().split('\r\n'):
print(d)
# Close sockets
s.close()
ss.close()
# Done! Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Thank you for you reply @shariltumin , your script worked great. Unfortunately I still have memory allocation issues if I run your script after setting up the graphical interface for the ILI9341 display.
I have also tried to invoke micropython.mem_info()
gc.collect()
micropython.mem_info()
api = ElectricityPriceAPI()
# Output:
stack: 656 out of 15360
GC: total: 111936, used: 108176, free: 3760, max new split: 22528
No. of 1-blocks: 1443, 2-blocks: 113, max blk sz: 2400, max free sz: 189
stack: 656 out of 15360
GC: total: 111936, used: 92960, free: 18976, max new split: 22528
No. of 1-blocks: 542, 2-blocks: 88, max blk sz: 2400, max free sz: 388 Here is my class for fetching the prices: import config
import utime
import time
import socket
import ssl
class ElectricityPriceAPI:
def __init__(self):
self.ntp_time = utime.localtime()
def get_prices(self, day):
if day == "today":
day_target = self.ntp_time
elif day == "tomorrow":
day_target = self.ntp_time(utime.time() + 86400)
year, month, date = "{:04}".format(day_target[0]), "{:02}".format(day_target[1]), "{:02}".format(day_target[2])
addr = socket.getaddrinfo(config.url, config.port)[0][-1]
print(f"Fetching from URL: {config.url}")
# Connect to https
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect(addr)
time.sleep(1)
s = ssl.wrap_socket(ss)
request = f"GET {config.api}{year}/{month}-{date}_{config.zone}.json HTTP/1.1\r\nHost: {config.url}\r\n\r\n"
s.write(request.encode())
data = s.read()
price = data.decode().split('\r\n')
s.close()
ss.close()
return price Is it possible that the display driver is allocating to much memory for my project? |
Beta Was this translation helpful? Give feedback.
Note that ssl uses memory when in use. So that could be the problem here.
I assume you only fetch the spot prices once a day? I will try this way:
This way you will not be using ssl when memory is tight. It is worth a try if your project allows you to do things in parts like the above.