Page 1 of 1

HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 11:25 am
by filipESP
I test the http_server example, named 'simple" and I would like to ask about the possibility to edit the content of the page.
Eg. I want to place the edit tekst field in the page body to change the name of any variable.

Re: HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 11:44 am
by PeterR
Suggest you do some background reading on AJAX.

In essence Javascript embedded in your HTML will request another page from the ESP.
You will write a custom handler on the ESP which programmatically returns an XML or JSON 'file' when the Javascript requested file comes in. I put all my AJAX 'files' under path '/api/v1'
Basically you will printf() your variables to a string in XML or JSON format and return that string along with HTTP headers.
Your Javascript receives the XML/JSON file and uses the DOM (or JQuery) to find HTML property/class.
Once found you may then change the HTML object as you wish.

It sounds harder than it is.
https://www.w3schools.com/xml/ajax_intro.asp covers the HTML side and the HTTP Server theory.
There are plenty of ESP32 custom handler posts/links in this forum.

There are other approaches but AJAX is simplest I think.
Good luck.

Re: HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 12:17 pm
by filipESP
Thanks for your response but I need to place the edit text field to be able to set eg. a password to my program.

Re: HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 12:34 pm
by gecko242
What PeterR has suggested will work.
You can also pass data around in an HTML Form, using POST/GET requests.
Handling it is relatively easy ESP side.
I suggest you read up on how websites/webservers talk. There are plenty of examples around doing what you want to achieve.

Re: HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 3:01 pm
by filipESP
I don't want to change the variable value in the program, I want change it when I connect with the server on my smartphone.

Re: HTTP Server with option to edit content of page.

Posted: Wed Dec 19, 2018 4:13 pm
by PeterR
You need to be clearer ...

If you want to send from Webpage to ESP then (as suggested) look at POST/GET form actions. You can make your HTML form POST/GET from to any address. The ESP32 can then 'catch' the data because the ESP knows (you code that part) that POST/GET to '/my-location' is special and is a data exchange not a page request.

Now you could supply a page with fresh data. This is known as SSI. I think you would have to code that yourself on the ESP though & besides form actions and/or AJAX are quite easy & very, very well documented.

In all instances the Webpage must request refresh either full page refresh (not good) or by asking for new data.
In HTTP world once you have been given your webpage that's it. You don't get that page again unless you request it - which you could do.
Better just to exchange the data & non-static parts though.

Re: HTTP Server with option to edit content of page.

Posted: Thu Dec 20, 2018 11:45 am
by filipESP
Okey, I edit the page in the .user_ctx variable (in /hello uri) and now I want to intercept the data in the /echo uri.
I get this LOG:

W (21076) httpd_parse: parse_block: response uri/header too big
W (21076) httpd_txrx: httpd_resp_send_err: 431 Request Header Fields Too Large -
Header fields are too long for server to interpret
W (21096) httpd_parse: parse_block: incomplete (0/65) with parser error = 16
W (21096) httpd_txrx: httpd_resp_send_err: 400 Bad Request - Server unable to un
derstand request due to invalid syntax

and this is the body of the page I made:

.user_ctx = "<h1>ZMIANA LOGINU</h1>"
"<form method=\"POST\" action = \"/echo\" >"
"<p>login<p/>"
"<input name=\"login\"/>"
"<p>pass<p/>"
"<input name=\"haslo\"/>"
"<button type=\"submit\">ZMIEN DANE</button>"
"<form/>"

Re: HTTP Server with option to edit content of page.

Posted: Thu Dec 20, 2018 12:47 pm
by filipESP
I changed the CONFIG_HTTPD_MAX_URI_LEN makro on higher and it is working :)))
Thx you for help.

Re: HTTP Server with option to edit content of page.

Posted: Fri Dec 21, 2018 6:50 pm
by filipESP
I have next issue with WiFi.
I set ESP as AP and when I try to connect with it a get this log in terminal: "wifi: max connection, deauth!"
What is the reason of this statement?

Re: HTTP Server with option to edit content of page.

Posted: Sat Dec 22, 2018 10:29 am
by chegewara
Check AP config, there is max connected clients option.