0

I was trying to run a code from this https://circuits4you.com/2018/03/10/esp8266-jquery-and-ajax-web-server/

I am pasting it here.

/*
 * ESP8266 NodeMCU jQuery CDN Demo
 * 
 * https://circuits4you.com
 */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

//ESP Web Server Library to host a web page
#include <ESP8266WebServer.h>

//---------------------------------------------------------------
//Our HTML webpage contents in program 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>
)=====";
//---------------------------------------------------------------

//On board LED Connected to GPIO2
#define LED 2  

//SSID and Password of your WiFi router
const char* ssid = "circuits4you.com";
const char* password = "yourWiFipassword";

//Declare a global object variable from the ESP8266WebServer class.
ESP8266WebServer server(80); //Server on port 80

//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
 Serial.println("You called root page");
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
}
//==============================================================
//                  SETUP
//==============================================================
void setup(void){
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");

  //Onboard LED port Direction output
  pinMode(LED,OUTPUT); 

  //Power on LED state off
  digitalWrite(LED,HIGH);
  
  WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
  
  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
 
  server.on("/", handleRoot);      //Which routine to handle at root location. This is display page

  server.begin();                  //Start server
  Serial.println("HTTP server started");
}
//==============================================================
//                     LOOP
//==============================================================
void loop(void){
  server.handleClient();          //Handle client requests
}

When I deploy this to my nodemcu esp-12e, I a m getting below error

I used the exception decoder

Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
PC: 0x4000bf64
EXCVADDR: 0x4023d581

From the decoder it says the offending line is this one

void handleRoot() {
 Serial.println("You called root page");
 String s = MAIN_page; //<<<<<<<<<<<<<<<<<<<<<< Error on this line
 server.send(200, "text/html", s); 
}

It looks like it cannot read from the variable in PROGMEM..

I replaced that line with this code and converting it into a string and the error was gone.

String MAIN_page = 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>
)=====";

Is there something wrong in the code that I am following on how it reads the variable from the PROGMEM flash memory?

3
0

You cannot assign a PROGMEM string directly to a String object. Besides, the whole concept makes no sense, since String objects store their data in RAM regardless.

If you want to use PROGMEM you need to use const char * not String.

2
  • String s = (const __FlashStringHelper*) MAIN_page; would work and does use RAM only temporary. it could be useful to replace some parts of the string. But the whole approach by OP to this is wrong.
    – Juraj
    Aug 31 '20 at 17:34
  • Yes, I just followed everything from that link.. I am just not sure how they are able to make it work at their end though. Sep 6 '20 at 11:30
-1

The whole point of using PROGMEM is to save the RAM for other uses. If you have a huge string that would overflow your RAM then PROGMEM is essential. However, if the string isn't too large and the program works fine, then just use String instead of PROGMEM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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