I am currently trying to make a webserver using ESP8266 (NodeMCU). I have been having issues with what I believe is running out of heap. In order to save on heap, I have been trying to implement PROGMEM into the core of my webserver. I have been trying to deploy the following chunks of code where rname1 is a String.
In the Root page of the webserver I have the following code
if (Device>=1){
String rname1= read_String(110); // Read EEPROM address 110 for content of rname1
content += DEVICES1;
}
What results are two html buttons one green and one red (as expected) on my webserver, but the text within them are just "rname1" as opposed to the actual content of the string. Is there a way to correct this?
I previously successfully implemented this code just by doing the following
However, as mentioned too much heap is consumed. When Device is greater than 10 my webpage will either be glitchy or will restart after compilation. Was hoping the PROGMEM implementation would help on heap. Any ideas to resolve the issue with seeing "rname1" as opposed to the actual content of the rname1 String when using PROGMEM?
There is no EEPROM on ESP8266 – Arduino framework code is a dummy to maintain compatibility.
PROGMEM can save a little bit of memory on ESP8266, but not much really – less than 10% – it will not solve the issue of running out of RAM.
How to really solve it? Split your response generation into multiple HTTP requests. Serve CSS separtly from HTML. Same JS. Keep HTML simple, basic template only, nothing else. Move as much code as you can into JS –> and JS can be split into multiple modules. 10 HTTP requests with small amounts of data in response are much less likely to use up all your RAM than one big response.
it actually stores the string literals in the "flash" [irom], instead of loading it to RAM at the boot. So, it is only loaded to RAM per usage, and not being there for all the time => hence my <10% RAM savings.
Thank you for this suggestion, it worked. Prior to implementing the code below, my heap was around 5000 (and acting glitchy) with 10 devices. Once implemented my heap improved to over 8000 (no glitches) with 10 devices. Able to successfully run 16 devices (my goal) with over 7500 heap available with no glitches.
You should have created a separate resource. A CSS file. For example named f.css
And link the CSS file in your HTML file (so the browser requests that CSS file also.
You even don't need to duplicate most of the attributes in the CSS. Just make a separate class with the differences and use that class for the "active" button.
in that example all "button" would be styled 300%, darkgray, red, with border radius, and ones with class "active" will get green.
one more advantage is, that most browsers will cache the CSS, and so you will reduce even more bandwidth.
If that is still a miracle, you might try my example of a Webserver.
It should work on ESP8266 and ESP32 out of the box and shows how I meant to style the page with CSS.
The issue is when I run the code all of my buttons are green. I experimented by adding inactive in various places within the main code where button is written, but nothing seemed to do the trick. How exactly do you modifiy the code to differentiate the green from the red buttons?
I'm coming back to my proposal of modifying the button element and adding a class for the difference (in color). You missed to give the green button a different class.
That's what I meant:
two comments on your last attempt:
don't name the class by an attribute content. One day you decide to use a different color scheme and then you don't have green or red anymore, but maybe a grey and and a bold black but you have spread the class='red' all over in your code and you will have fun to adopt that.
even on the ESP I would try to spare bytes and reduce HTML and CSS to a minium. Therefore the styling of the html elements to avoid the need of assigning a class to each element.