having two types of variables , one is INPUT by FORM , the second input by AO esp Nodemcu.
First one is OK working OK but i do not know how to view the second in the web page.
Attached below is my code , I really appreciate any Help
/*********
Rui Santos
Complete project details at Input Data on HTML Form ESP32/ESP8266 Web Server Arduino IDE | Random Nerd Tutorials
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 <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <Hash.h>
#include <FS.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char *ssid = "Gef";
const char *password = "fgtrecTE142";
const char PARAM_INT = "TempRef";
#define analogPin A0 / ESP8266 Analog Pin ADC0 = Temp /
const int ADC_pin = A0;
int Led1 = 5; / GPIO1 (D5) is connected to LED /
int Led2 = 4;
//int adcValue = 0; / Variable to store Output of ADC */
int adcValue = analogRead(ADC_pin);
// adcValue = adcValue / 33.92;
// HTML web page to handle 3 input fields (inputString, TempRef, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
La Cava De POLYLa Cava de POLY
Temperatura de REFERENCIA %TempRef%°C:
<p></>
<input type="submit" value="Ingresar" onclick="submitMessage()">
<p></>
Temperatura Actual %adcValue%°C
</>
)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());
}
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 == "TempRef") {
return readFile(SPIFFS, "/TempRef.txt");
}
return String();
}
void setup() {
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
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT); // Initialize the LED pin as an output
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=
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) {
String inputMessage;
// GET inputString value on <ESP_IP>/get?inputString=
if (request->hasParam(PARAM_INT)) {
// GET inputInt value on <ESP_IP>/get?inputInt=<inputMessage>
inputMessage = request->getParam(PARAM_INT)->value();
writeFile(SPIFFS, "/TempRef.txt", inputMessage.c_str()); //write parameter to spif
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
request->send(200, "text/text", inputMessage);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
adcValue = analogRead(ADC_pin);
adcValue = adcValue / 33.92;
// To access your stored values on inputString, TempRef
int TempRef = readFile(SPIFFS, "/TempRef.txt").toInt();
Serial.print("Referencia: ");
Serial.println(TempRef);
Serial.println(adcValue);
if (adcValue < TempRef) {
digitalWrite(Led1, HIGH);
digitalWrite(Led2, LOW);
} else if (adcValue > TempRef) {
digitalWrite(Led1, LOW);
digitalWrite(Led2, HIGH);
} else if (adcValue = TempRef) {
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
char(adcValue);
}
delay(5000);
}