logo elektroda
logo elektroda
X
logo elektroda

How to read data from an HTML page in the ESP32 via HTTP?

korteksik 2226 7
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18773191
    korteksik
    Level 10  
    Welcome,

    The issue is that I have built myself a device that records some parameters and sends them to a SQL database on a server. I have used tutorials and readymades available on the internet. I have a simple script written in PHP
    which handles this from the server side. I use the ESP32 module, http and the GET method (written in Arduino) to send data.
    I have recently included a Fibocom G510 GSM module so that if WiFi is not available, data will still be sent. Sending works correctly and stably.
    Now I wanted to be able to change certain parameters of the module remotely using a page that would also be hosted on the same server. I searched the net and all the tutorials on sending data from a page to the ESP concern pages generated by the esp itself. I have not found any information on how to do this through an html page generated on the server.

    So now I have two questions:
    1) How do I read the information from the page in the ESP32? I'm not quite sure if the ESP is supposed to query the page if there are any changes, or if it will be informed - if so, how?

    My PHP script, after a successful write of data to the sql database, in the event of a successful record write, returns that the record was successful. My code for sending the data:

    Code: C / C++
    Log in, to see the code
    .

    I understand that I get the ifnormation back thanks to the method "int httpCode = http.GET();". Please forgive me for the layman's question - in that case, if I want to read the information from the server (without sending the data), do I have to keep sending the query
    "int httpCode = http.GET();". ?

    2) How to implement the same when using a GSM modem G510? In the current code, I use the AT+HTTPREAD command to receive a response from the server directly after sending data with the at+httpset and AT+HTTPACT commands.
    Do I use the same command to query the server to read the parameter change?

    If I have messed something up, please forgive me, as this is a new topic for me and I may not fully understand how it should work :) .
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • Helpful post
    #2 18774037
    TvWidget
    Level 38  
    You establish a connection to the server periodically and send data to it. You probably do this via HTTP GET or HTTP POST.
    When doing this, the server may only send back an OK or a command to change some parameter on the device. The advantage of such a solution is its simplicity and the disadvantage is that parameter changes will not take place immediately.
    The page on the server you mention is used to create and save the file containing the command I mentioned earlier. A PHP script peels the data from the ESP, sends back the created file and then deletes it.
  • ADVERTISEMENT
  • Helpful post
    #3 18774076
    khoam
    Level 42  
    korteksik wrote:
    How does the ESP32 read the information from the page? I'm not quite sure if the ESP is supposed to query the page if there have been any changes, or if it will be informed - if so, in some way?
    .
    It is possible to handle (asynchronously) POST requests that will come from an "external" server to the server (AsyncWebServer) on the ESP32. Example:
    https://techtutorialsx.com/2018/10/12/esp32-http-web-server-handling-body-data/
    This uses the ESPAsyncWebServer library:
    https://github.com/me-no-dev/ESPAsyncWebServer/
    You need to have the latest version of Arduino Core for ESP32 installed.
  • #4 18774252
    korteksik
    Level 10  
    TvWidget wrote:
    You establish a periodic connection to the server and send data to it. You probably do this via HTTP GET or HTTP POST.
    When doing so, the server may only send back an OK or a command to change some parameter on the device. The advantage of such a solution is its simplicity and the disadvantage is that parameter changes will not take place immediately.
    The page on the server you mention is used to create and save the file containing the command I mentioned earlier. A PHP script peels the data from the ESP, sends back the created file and then deletes it.
    .

    Thanks for the reply. That's how I guessed that you could include the parameter change in the response in a PHP script, but I preferred to ask if there wasn't some other - read simpler :) solution. As you write - this has the advantage of not requiring changes to the current server connection handling code. The immediate change of parameters in this case is not relevant, so it even suits me :D .

    Added after 14 [minutes]:

    khoam wrote:
    korteksik wrote:
    How do I read the information from the page in the ESP32? I'm not quite sure if the ESP is supposed to query the page if there have been any changes, or if it will be informed - if so, in some way?
    .
    It is possible to handle (asynchronously) POST requests that will come from an "external" server to the server (AsyncWebServer) on the ESP32. Example:
    https://techtutorialsx.com/2018/10/12/esp32-http-web-server-handling-body-data/
    This uses the ESPAsyncWebServer library:
    https://github.com/me-no-dev/ESPAsyncWebServer/
    You need to have the latest version of Arduino Core for ESP32 installed.



    Thanks for the material. My only concern is that it won't work with a GSM modem, which I also care about - as it makes me independent of WiFi.
    I'm going to try it initially with sending commands in the server response, as it seems simpler to implement and I don't need to change my current code :D .
  • ADVERTISEMENT
  • #5 18774958
    khoam
    Level 42  
    korteksik wrote:
    I'm just afraid it won't work with a GSM modem
    .
    It will not work :) .

    korteksik wrote:
    which I also care about - because it makes me WiFi independent.

    The procedure for switching between networks you probably have to write yourself anyway, and AsyncWebServer can only be activated when there is a connection over WiFi.

    korteksik wrote:
    and I don't need to change my current code
    .
    There is no defence against such an argument ;) .
  • ADVERTISEMENT
  • #6 18776551
    kaczakat
    Level 34  
    It is best to use websockets to transfer information from the HTML page to the ESP. This works fast enough to transfer the lighting effects calculated by the PC program in real time (someone posted a cool video on YOUTUBE). I usually upload the html code to the ESP, either in code or on SPIFFS. But the site can be fired from anywhere by stripping the example code of the html server altogether, there may be an html server used for something completely different, you just swap the lines:
    Code: HTML, XML
    Log in, to see the code
    .
    Example .
    I see the same thing when I paste 192.168.1.30 into my browser, because there I have the same idex.htm with the first line active.
    The first one is for the websockets server inside the ESP, the second one I just fire up from a PC drive, tell it to connect to the websockets server on the ESP on the local network with IP 31 and port 81. You can of course redirect the port in the router to that IP with that port and access that ESP from anywhere in the world. Any server can then do the job for the PC, of course.
    What it looks like:
    How to read data from an HTML page in the ESP32 via HTTP? .
    From the esp, information stored in text fields arrives in the HTML page, possibly actions called on a page opened in another browser (the websockets server in the ESP supports 5 connections at the same time), the result of buttons, sliders, possibly whatever you can come up with using html and JS can be sent to the ESP.
    Helpful post? Buy me a coffee.
  • #7 18776616
    TvWidget
    Level 38  
    kaczakat wrote:
    .
    You can, of course, redirect the port on your router to this IP with this port and access this ESP from anywhere in the world.
    .
    The enquirer has a GSM modem.
  • #8 18776965
    kaczakat
    Level 34  
    There is WIFI, there is a GSM modem as a fallback, in total, transfers can be made both ways, but for the GSM modem the connection initiation must be from within the network, and there is also a websockets client.
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around how to read data from an HTML page hosted on a server using an ESP32 module via HTTP. The user has successfully implemented data transmission to a SQL database using the ESP32 and a Fibocom G510 GSM module for fallback connectivity. They seek to remotely change parameters of the ESP32 through a server-hosted HTML page. Responses suggest using HTTP GET/POST methods for parameter changes, with a PHP script to handle commands. The ESPAsyncWebServer library is recommended for asynchronous handling of requests. However, concerns are raised about the compatibility of these methods with the GSM modem, as the AsyncWebServer requires a WiFi connection. An alternative solution proposed is using WebSockets for real-time communication, although it may require adjustments for GSM connectivity.
Summary generated by the language model.
ADVERTISEMENT