9

I saw the following code from https://circuits4you.com/2018/03/10/esp8266-jquery-and-ajax-web-server/ but I could not understand it thoroughly...

I looked at the documentation of PROGMEM from this https://www.arduino.cc/reference/en/language/variables/utilities/progmem/ but could not understand the importance in the code on why you should put your variables in flash memory instead of SRAM (the usual memory?)?

const char MAIN_page[] PROGMEM = R"=====(
  <!DOCTYPE html>
  <html>
  <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
  $(document).ready(function(){
      $("p").click(function(){
          $(this).hide();
      });
  });
  </script>
  </head>
  <body>
  
  <p>If you click on me, I will disappear.</p>
  <p>Click me away!</p>
  <p>Click me too!</p>
  <br><hr>
  <a href="https://circuits4you.com">circuits4you.com</a>  
  </body>
  </html>
)=====";

Also, I could not understand the definition of R"====()====", what is meant by this in arduino?

1 Answer 1

12

why you should put your variables in flash memory instead of SRAM

Because the Arduino has only a very small amount of memory. If you have data that will never change it's better to store it in flash (of which there is much more) so as to leave the SRAM free for data that does change.

Also, I could not understand the definition of R"====()====", what is meant by this in arduino?

The R means "Treat everything between these delimiters as a raw string". That is, everything between "=====( and )=====". It's not an Arduino thing, but a general C++ thing (see here). The ===== can be anything you like (within certain bounds) as long as it's the same at both the start and end of the string. So you could use R"-=(This is "text")=-" which would assign the string This is "text". It's a great way of working with long strings that may want to contain " as otherwise you would have to escape each and every " as \". So the string I showed just now would otherwise have to be written as: "This is \"text\"", which can get very messy if there are lots of " in there. The important thing to remember is that )===== mustn't appear in your text anywhere, so make sure it's something really obscure.

3
  • Thank you very much for being patient in helping beginners! Commented Aug 31, 2020 at 13:29
  • This applies to older AVR architectures. Newer one (for example ATMega4809) are mapping flash and the other memories into data space and there is no need for using PROGMEM. The same applies to ARM architecture - there is no PROGMEM, as there is no need to copy const variables into RAM to be accessible directly.
    – KIIV
    Commented Aug 31, 2020 at 19:37
  • 2
    In your example, wouldn't R"-=(This is "text")=-" actually need to be R"-=(This is "text")-="? Or am I not understanding it right? It's almost like the HEREDOC syntax right?
    – bPratik
    Commented Mar 20, 2021 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.