srwi / ESPEssentials

Essentials to get you started with your Arduino projects using the ESP8266 and ESP32.
GNU Lesser General Public License v2.1
26 stars 5 forks source link

Need help calling a HTTP request #11

Closed huckes closed 7 months ago

huckes commented 9 months ago

Hi,

I'n a HTTP-dummy, so please forgive me if this question is too silly.

in a sketch using ESPEssentials I try to call a function via a HTTP request. I stored an index.htm file with a button which returns a request to the server: href="/adjust_fine_forward"

In the setup-section of the sketch I initalized this request with these lines:

ESPEssentials::WebServer.on("/adjust_fine_forward", HTTP_GET, [&]() { fine_forward(); Serial.println("'adjust_fine_forward' found"); });

"fine_forward" is the function I call.

The problem: When I click the button, the function is called repetitively about every second until the connection is interrupted or I stop loading the Web-Page within the browser. So I assume that there is annother line needed in the initialization which stops loaden the Web-page.

Any help highle appreciated, Hans

srwi commented 9 months ago

Hi Hans!

It looks like in your callback function you are not sending a response back to the web browser. I could imagine the browser is waiting for a response from the ESP and thus retrying to open the URL.

In the readme you will find an example that sends the plain text response with success code 200 back to the browser: https://github.com/srwi/ESPEssentials#webserver

Once you copied that over into your sketch, does that resolve the issue of repeated function calls?

huckes commented 9 months ago

Hi Stephan,

thanks for the fast reply. Yes, you're right: If I send the succes code back, the function is called only once. But in the browser the index page disappears and the text of the success message appears. Even if I add no text into the success code, the index page disappears. This is why I didn't send the success message.

So how can I send a success message which leaves the index page on the browser's screen?

srwi commented 9 months ago

I think usually these GET requests are not meant to be used that way, but you could make it work like this:

<button onclick="document.getElementById('hiddenIframe').src='http://<device-ip>/adjust_fine_forward';">Adjust fine forward</button>
<iframe id="hiddenIframe" style="display: none;"></iframe>

It will open the link in an invisible iframe (a webpage within your webpage) so that the user won't see the page change. This requires you to add the webserver response as mentioned above.

srwi commented 9 months ago

Actually, there is a more elegant way:

<button onclick="fetch('http://<device-ip>/adjust_fine_forward')">Adjust fine foward</button>

This doesn't require the iframe. The only requirement for this to work is that the index page is on the same url/ip as the url you are sending the request to. For your application that should be the case.

huckes commented 9 months ago

Hi, thanks for your advice. I'll try it in the next days, as I'm busy with some other tasks at home too.