Memory problems while serving web pages
Posted: Mon Mar 08, 2021 11:26 am
My project is to build a general purpose relay controller based on input from various sensors. I want to see the current values of the sensors and be able to enter "on" and "off" values for the various sensors via web server. All of it runs on an esp32. And it now works -- reasonably well (with the current limitation that I can only activate one sensor at a time). My problem is that I get "MemoryError: memory allocation failed, allocating 7103 bytes" after some time of playing around with the webinterface. I have around 67000 of free memory and it seems rather stable until it fails. When I get the error it invariably happens when I build the html string to reload the full the web page. The page is very simple -- basically a table with sensor values and on off values -- but because of <style> instructions and javascripts the page is around 8kB to load.
As I have no real experience in how to do these things I'm just inquiring about possible best practice. From posts on the forum I understand that my problem probably is due to fragmentation of memory so that there is no 8kB of consecutive memory available.
Any advice or pointers would be much appreciated.
/Fredrik
As I have no real experience in how to do these things I'm just inquiring about possible best practice. From posts on the forum I understand that my problem probably is due to fragmentation of memory so that there is no 8kB of consecutive memory available.
- To reduce my html string, I was thinking that I should put the java scripts and style sheet in separate files that gets called from my html file (like I would on an apache server) but I have not found how to do this with my websocket in micropython.
- As an alternative to the above, I also wonder if it would be better (since my visible page is quite small) to move the javascript logic to the python code instead to reduce the html string accordingly.
- But I would like to keep the use of js ajax to get the updated sensor readings (so I don't get a flickering web page). Is this polling a likely source of the problem. My own testing is very inconclusive.
- The relay itself gets updated periodically with a machine.timer. Is this something that can clash with the websocket handler?
Any advice or pointers would be much appreciated.
/Fredrik