0

I have a variable in my ESP32 code. I also have a webserver that displays a HTML page, and in this page I can type a value in a form and send it to that variable via HTTP GET request.

The HTML of this page, together with a CSS and a JS file are on the ESP32 internal file system, they were uploaded via SPIFFS.

Is there any way I can get the value stored in that ESP32 variable and use it on my JS file? I want to create a function where the JS will get the value and apply it on the innerHTML of the form, so that the webpage will show by default the value already stored in the variable.

Here's my setup(). The variables are parameter1, 2, 3 and 4.The JS file is empty.

void setup(){
  Serial.begin(115200);

  if(!SPIFFS.begin()){
     Serial.println("An Error has occurred while mounting SPIFFS");
     return;
     }

  WiFi.softAP(ssid, password);

  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());

    server.on("/index", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", "text/html");

    const int paramsNr = request->params();

    for (int i=0; i<paramsNr; i++){
      AsyncWebParameter* p = request->getParam(i);

      if (i==0){
        parameter1 = p->value().toInt();
      }
      else if (i==1){
        parameter2 = p->value().toInt();
      }
      else if (i==2){
        parameter3 = p->value().toInt();
      }
      else {
        parameter4 = p->value().toInt();
      }
    }
  });

  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/style.css", "text/css");
  });
  server.on("/logo.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/logo.jpg", "image/jpeg");
  });
  server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/script.js", "text/javascript");
  });

  server.begin();
}
  • 2
    I would have a separate URL that when called returns the variable in some useable format, like JSON. Then use AJAX to request that variable from Javascript. – Majenko Jul 15 at 14:41

Your Answer

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

Browse other questions tagged or ask your own question.