Here is my sketch :
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
#include "FS.h"
const char* ssid = "myssid";
const char* password = "mypass";
const byte led_gpio = 22;
#define TIME_TO_SLEEP yourInputString
AsyncWebServer server(80);
IPAddress local_IP(192, 168, 0, 8);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
const char* PARAM_STRING = "inputString";
const char* PARAM_INT = "inputInt";
const char* PARAM_FLOAT = "inputFloat";
// HTML web page to handle 3 input fields (inputString, inputInt, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function submitMessage() {
alert("Saved value to ESP SPIFFS");
setTimeout(function(){ document.location.reload(false); }, 500);
}
</script></head><body>
<form action="/get" target="hidden-form">
inputString (current value %inputString%): <input type="text" name="inputString">
<input type="submit" value="Submit" onclick="submitMessage()">
</form><br>
<iframe style="display:none" name="hidden-form"></iframe>
</body></html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
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());
for (int i = 1; i <= 10; i++) {
String line = file.readStringUntil('n');
Serial.println(line);
}
}
file.close();
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");
}
file.close();
}
// Replaces placeholder with stored values
String processor(const String& var) {
//Serial.println(var);
if (var == "inputString") {
return readFile(SPIFFS, "/inputString.txt");
}
else if (var == "inputInt") {
return readFile(SPIFFS, "/inputInt.txt");
}
else if (var == "inputFloat") {
return readFile(SPIFFS, "/inputFloat.txt");
}
return String();
}
void setup() {
///////////////////////////////
SPIFFS.begin();
pinMode(led_gpio, OUTPUT);
digitalWrite(led_gpio, HIGH);
Serial.begin(115200);
// Initialize SPIFFS
#ifdef ESP32
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
#else
if (!SPIFFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
#endif
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?inputString=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {
String inputMessage;
// GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
if (request->hasParam(PARAM_STRING)) {
inputMessage = request->getParam(PARAM_STRING)->value();
writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
}
// GET inputInt value on <ESP_IP>/get?inputInt=<inputMessage>
else if (request->hasParam(PARAM_INT)) {
inputMessage = request->getParam(PARAM_INT)->value();
writeFile(SPIFFS, "/inputInt.txt", inputMessage.c_str());
}
// GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
else if (request->hasParam(PARAM_FLOAT)) {
inputMessage = request->getParam(PARAM_FLOAT)->value();
writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
request->send(200, "text/text", inputMessage);
});
// Receive an HTTP GET request
server.on("/H", HTTP_GET, [] (AsyncWebServerRequest * request) {
digitalWrite(led_gpio, HIGH); // GET /H turns the LED on
delay(500);
digitalWrite(led_gpio, LOW);
delay(500);
digitalWrite(led_gpio, HIGH);
delay(500);
digitalWrite(led_gpio, LOW);
delay(500);
digitalWrite(led_gpio, HIGH);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
esp_deep_sleep_start();
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/L", HTTP_GET, [] (AsyncWebServerRequest * request) {
digitalWrite(led_gpio, LOW); // GET /L turns the LED off
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/O", HTTP_GET, [] (AsyncWebServerRequest * request) {
digitalWrite(led_gpio, HIGH); // GET /L turns the LED off
request->send(200, "text/plain", "ok");
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
// To access your stored values on inputString, inputInt, inputFloat
String yourInputString = readFile(SPIFFS, "/inputString.txt");
Serial.print("*** Your inputString: ");
Serial.println(yourInputString);
//int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
// Serial.print("*** Your inputInt: ");
// Serial.println(yourInputInt);
// float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
// Serial.print("*** Your inputFloat: ");
// Serial.println(yourInputFloat);
delay(5000);
}
And my error is :
Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"
C:\Users\Zet\AppData\Local\Temp\arduino_modified_sketch_284202\sketch_nov04a.ino: In lambda function:
sketch_nov04a:11:23: error: 'yourInputString' was not declared in this scope
#define TIME_TO_SLEEP yourInputString
^
C:\Users\Zet\AppData\Local\Temp\arduino_modified_sketch_284202\sketch_nov04a.ino:171:35: note: in expansion of macro 'TIME_TO_SLEEP'
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
^
Multiple libraries were found for "WiFi.h"
Used: C:\Users\Zet\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi
Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
'yourInputString' was not declared in this scope
Thank you!