Go Down

Topic: Writing the value in an ESP8266 variable from a JavaScript variable (Read 367 times) previous topic - next topic

caue

Hello everybody.

I have a question about the use of html input value.

How can I get a value from a JavaScript or HTML variable and write this value in a ESP-8266 variable?

I putted in red the method that I trying use.

Follow part of code:

// Load Wi-Fi library
#include <ESP8266WiFi.h>

float temp_dia[32] = {0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0, 11.1, 12.2, 13.3, 14.4, 15.5, 16.6, 17.7, 18.8, 19.9, 20.0, 21.1, 22.2, 23.3, 24.4, 25.5, 26.6, 27.7, 28.8, 29.9, 30.0, 31.1 }; //array que guarda os valores das temperaturas por dia
String temp_dia_html[32];

float contagem = 10.4;
float temp_atual = 11.3;
float temp_ref = 15.0;
int dia = 1;
float tolerancia = 0.5;
String data_html = "15/8/18";
String hora_html = "22:03";
String status_resfriando_html = "OFF";
String status_aquecendo_html = "OFF";  
int status_iniciar = 0;
int status_gravar = 0;

// Replace with your network credentials
const char* ssid     = "testeonoff";
const char* password = "12345678";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

//Função para converter temperatura dos dias de float para string

void converter_float_para_html()
{
   for (int contador=1; contador<=31; contador ++)
   {
     temp_dia_html[contador]=String (temp_dia[contador],1);
   }  
}



void loop(){

   contagem = contagem + 0.1;
   if (contagem > 100){contagem =0.0;}
   String temperatura_html =  String(contagem, 1);
   String temp_atual_html = String(temp_atual, 1);
   String temp_ref_html = String(temp_dia[dia], 1);  
   //String temp_ref_html = String(temp_ref, 1);
   String dia_html = String(dia);
   String tolerancia_html = String(tolerancia, 1);  
 
 WiFiClient client = server.available();   // Listen for incoming clients

 if (client) {                             // If a new client connects,
   Serial.println("New Client.");          // print a message out in the serial port
   String currentLine = "";                // make a String to hold incoming data from the client
   while (client.connected()) {            // loop while the client's connected
     if (client.available()) {             // if there's bytes to read from the client,
       char c = client.read();             // read a byte, then
       Serial.write(c);                    // print it out the serial monitor
       header += c;
       if (c == '\n') {                    // if the byte is a newline character
         // if the current line is blank, you got two newline characters in a row.
         // that's the end of the client HTTP request, so send a response:
         if (currentLine.length() == 0) {
           // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
           // and a content-type so the client knows what's coming, then a blank line:
          client.println("HTTP/1.1 200 OK");
           client.println("Content-type:text/html");
           client.println("Connection: close");
           client.println();
           
           // turns the GPIOs on and off
           if (header.indexOf("GET /iniciar") >= 0) {
             Serial.println("iniciar");
             status_iniciar =1;
             status_resfriando_html = "ON";
                        if (status_iniciar == 1){
                           client.println("<script>");
                           client.println("window.location.href = '/';</script>");
                           status_iniciar = 2;
                        }          
           } else if (header.indexOf("GET /5/off") >= 0) {
             Serial.println("GPIO 5 off");
             status_iniciar =0;          
             status_resfriando_html = "OFF";
           } else if (header.indexOf("GET /gravar") >= 0) {
             Serial.println("gravar");
             status_gravar =1;
                        if (status_gravar == 1){
                          client.println("<script>");
                            client.println("window.location.href = '/';</script>");
                               client.println("<script>");
                               //client.println(temp_dia[1]=strtof("document.getElementById('dia_28').value;"),NULL);
                               client.println(" temp_dia[1] = parseFloat(document.getElementById('dia_28').value;);");
                              //client.println( temp_dia[1]"=parseFloat(document.getElementById('dia_28').value;);");
                             //client.println( temp_dia[1]=+("parseFloat(document.getElementById('dia_28').value;);"));
                               //temp_dia[1]= 99.9;
                               converter_float_para_html();
                               client.println("</script>");

                           status_gravar = 0; //2
                           dia++;
                           if (dia>31){dia=1;}
                           if (status_aquecendo_html == "ON"){status_aquecendo_html = "OFF";} else
                             {
                               status_aquecendo_html = "ON";
                             }
                        }              
                   }
           // Display the HTML web page
           client.println("<!DOCTYPE html><html>");
           client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
           client.println("<meta http-equiv=\'content-type\' content=\'text/html' charset=\'UTF-8\'>");            
           client.println("<link rel=\"icon\" href=\"data:,\">");

         

Juraj

you know that the script executes in browser on a different device?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up