Hello mates,
I'm new esp32 and webserver and have a little knowledge about data parsing from the webserver. What I'm trying to achieve is trying to get the RGB values in variable in esp32 so that i can use them to change my matrix display color.
Here is my code, may be this will give a clear idea.
//Libraries to configure esp32 with wifi and
//to establish connection b/w esp32 and webpage
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <AutoConnect.h>
#include <WiFiManager.h>
#include "SPIFFS.h"
#include "FS.h"
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
char ssid[] = "Rnd";
char pass[] = "XHBACDFH";
//SERVER PORTION
const char* PARAM_STRING = "inputString";
const char* PARAM_COLORVALUE = "inputColorValue";
// HTML web page to handle 2 input fields (inputString, inputColorValue)
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>
//This custom text function is woring very fine
<!--Custom Text-->
<form action="/get" target="hidden-form">
inputString (current value %inputString%): <input type="text" name="inputString">
<input type="submit" value="Submit" onclick="submitMessage()">
</form><br>
//But this is getting error (Error is : failed to open file or writing, the value when i choose from server it shows at the Serial.print() but it is not writing in the SPIFFS and shows "failed to open file or writing")
<!--Color picker-->
<form action="/get" target="hidden-form">
inputColorValue (current value %inputColorValue%):
<h2>Color Picker</h2>
<input type="color" name="inputColorValue">
<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());
}
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");
}
}
String processor(const String& var){
//Serial.println(var);
if(var == "inputString"){
return readFile(SPIFFS, "/inputString.txt");
}else if(var == "inputColorValue"){
return readFile(SPIFFS, "/inputColorValue.txt");
}
return String();
}
//Set up code
void setup(){
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("");
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
if (request->hasParam(PARAM_STRING)) {
inputMessage = request->getParam(PARAM_STRING)->value();
writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
}
else if(request->hasParam(PARAM_COLORVALUE)){
inputMessage = request->getParam(PARAM_COLORVALUE)->value();
writeFile(SPIFFS, "inputColorVlaue.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(){
String yourInputString = readFile(SPIFFS, "/inputString.txt");
Serial.print("*** Your inputString: ");
Serial.println(yourInputString); //here it prints what i input at server and prints the text
String yourInputColorVlaue = readFile(SPIFFS, "/inputColorVlaue.txt");
Serial.print("*** Your inputColorVlaue: ");
Serial.println(yourInputColorVlaue); //but here i dont get anything what input at server
}
Can anyone tell me where I'm doing wrong or pls if you have any solution pls assist, I'll be grateful..
Thanks and Regards..