Hi all, my first post in this forum
Have an ESP32 and running everything from Arduino IDE.
Using ESPAsyncWebServer and trying to use forms to store some data locally using SPIFFS.
Everything works except storing the email address, resulting in panic and reboot of the ESP.
If anybody could assist me in resolving the issue about storing the email address, I would be quite happy
/*********
 Rui Santos
 Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
Â
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files.
Â
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
*********/
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
#define ESP_getChipId()Â ((uint32_t)ESP.getEfuseMac())
String espID = String(ESP_getChipId(), HEX);
Â
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* PARAM_AP_SSID = "ap_ssid";
const char* PARAM_AP_PWD = "ap_pwd";
const char* PARAM_EMAIL = "email";
const char* PARAM_AMOUNT = "amount";
// HTML web page to handle 3 input fields (email, inputInt, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
 <!DOCTYPE HTML>
 <html>
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
 Â
  <style>
   div.center {text-align: center;}
  </style>
 Â
 Â
Â
  <script>
   function submitMessage() {
    alert("Saved value to ESP SPIFFS");
    setTimeout(function(){ document.location.reload(false); }, 500);Â
   }
  </script>
  </head>
  <body>
   <div class="container">
    <form action="/get" target="hidden-form">
     <div class="form-group">
      <label for="ap_ssid">Skriv inn aksesspunkt SSID for internett (nåværende: %ap_ssid%)</label>
      <input type="text" class="form-control" id="ap_ssid" placeholder="SSID" name="ap_ssid">
     </div>
    <button onclick="submitMessage()" type="submit" class="btn btn-primary">Lagre</button>
    </form>
   </div>
Â
   <div class="container">
    <form action="/get" target="hidden-form">
     <div class="form-group">
      <label for="pwd">Passord for aksesspunkt (nåværende: %ap_pwd%)</label>
      <input type="password" class="form-control" id="ap_pwd" placeholder="Passord" name="ap_pwd">
     </div>
    <button onclick="submitMessage()" type="submit" class="btn btn-primary">Lagre</button>
    </form>
   </div>
Â
   <div class="container">
    <form action="/get" target="hidden-form">   Â
     <div class="form-group">
      <label for="email">Skriv inn epost adressen som skal motta varsler (nåværende: %email%)</label>
      <input type="email" class="form-control" id="email" placeholder="Din epostadresse" name="email">
     </div>
    <button onclick="submitMessage()" type="submit" class="btn btn-primary">Lagre</button>
    </form>
   </div>
Â
   <div class="container">
    <form action="/get" target="hidden-form">
     <div class="form-group">
      <label for="amount">Skriv inn antall ML i dosering - 1, 2 eller 3 (nåværende: %amount%)</label>
      <input type="number " class="form-control" id="email" placeholder="1, 2 eller 3" name="amount">
     </div>
    <button onclick="submitMessage()" type="submit" class="btn btn-primary">Lagre</button>
    </form>
   </div>
   <iframe style="display:none" name="hidden-form"></iframe>
  </body>
  </html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
 request->send(404, "text/plain", "Not found");
}
//Â Â Â <input type="submit" value="Lagre" onclick="submitMessage()">
String readFile(fs::FS &fs, const char * path){
 Serial.printf("Reading file: %s\r\n", path);
 File file = fs.open(path, "r");
 if(!file || file.isDirectory()){
  Serial.println("- empty file or failed to open file");
  return String();
 }
 Serial.println("- read from file:");
 String fileContent;
 while(file.available()){
  fileContent+=String((char)file.read());
 }
 Serial.println(fileContent);
 return fileContent;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
 Serial.printf("Writing file: %s\r\n", path);
 File file = fs.open(path, "w");
 if(!file){
  Serial.println("- failed to open file for writing");
  return;
 }
 if(file.print(message)){
  Serial.println("- file written");
 } else {
  Serial.println("- write failed");
 }
}
// Replaces placeholder with stored values
String processor(const String& var){
 //Serial.println(var);
 if(var == "ap_ssid"){
  return readFile(SPIFFS, "/ap_ssid.txt");
 }
 else if(var == "ap_pwd"){
  return readFile(SPIFFS, "/ap_pwd.txt");
 }
 else if(var == "email"){
  return readFile(SPIFFS, "/email.txt");
 }
 else if(var == "amount"){
  return readFile(SPIFFS, "/amount.txt");
 }
 return String();
}
void setup() {
 Serial.begin(115200);
 // Initialize SPIFFS
 if(!SPIFFS.begin(true)){
  Serial.println("An Error has occurred while mounting SPIFFS");
  return;
 }
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.println("WiFi Failed!");
  return;
 }
 Serial.println();
 Serial.print("IP Address: ");
 Serial.println(WiFi.localIP());
 // Send web page with input fields to client
 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/html", index_html, processor);
 });
 // Send a GET request to <ESP_IP>/get?email=<inputMessage>
 server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
  String inputMessage;
 Â
  // GET email value on <ESP_IP>/get?ap_ssid=<inputMessage>
  if (request->hasParam(PARAM_AP_SSID)) {
   inputMessage = request->getParam(PARAM_AP_SSID)->value();
   writeFile(SPIFFS, "/ap_ssid.txt", inputMessage.c_str());
  }
 Â
  // GET inputInt value on <ESP_IP>/get?ap_pwd=<inputMessage>
  else if (request->hasParam(PARAM_AP_PWD)) {
   inputMessage = request->getParam(PARAM_AP_PWD)->value();
   writeFile(SPIFFS, "/ap_pwd.txt", inputMessage.c_str());
  }
 Â
  // GET inputFloat value on <ESP_IP>/get?email=<inputMessage>
  else if (request->hasParam(PARAM_EMAIL)) {
   inputMessage = request->getParam(PARAM_AMOUNT)->value();
   writeFile(SPIFFS, "/email.txt", inputMessage.c_str());
  }
  // GET inputFloat value on <ESP_IP>/get?amount=<inputMessage>
  else if (request->hasParam(PARAM_AMOUNT)) {
   inputMessage = request->getParam(PARAM_AMOUNT)->value();
   writeFile(SPIFFS, "/amount.txt", inputMessage.c_str());
  } Â
  else {
   inputMessage = "No message sent";
  }
  Serial.println(inputMessage);
  request->send(200, "text/text", inputMessage);
 });
 server.onNotFound(notFound);
 server.begin();
}
void loop() {
 // To access your stored values on email, inputInt, inputFloat
 String your_ap_ssid = readFile(SPIFFS, "/ap_ssid.txt");
 Serial.print("*** Ditt Aksesspunkt: ");
 Serial.println(your_ap_ssid);
 String your_ap_pwd = readFile(SPIFFS, "/ap_pwd.txt");
 Serial.print("*** Ditt AP passord: ");
 Serial.println(your_ap_pwd);
Â
 String your_email = readFile(SPIFFS, "/email.txt");
 Serial.print("*** Din epost: ");
 Serial.println(your_email);
Â
 int your_amount = readFile(SPIFFS, "/amount.txt").toInt();
 Serial.print("*** Ditt tall: ");
 Serial.println(your_amount);
Â
 delay(5000);
}