logo elektroda
logo elektroda
X
logo elektroda

Arduino R4 WiFi and ArduinoHttpServer - fixes, launch, examples of use

p.kaczmarek2 3390 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • Arduino R4 WiFi board with ArduinoHTTPServer text
    Here I will present the fixes, launch and development of the ArduinoHttpServer library on the Arduino R4 WiFi platform. The purpose of the topic will be to facilitate our operations related to the HTTP protocol, and more specifically, parsing HTTP requests (processing the header, resource path and GET arguments) and sending responses to these requests. By the way, I will also show a simple HTTP authorization system offered by this library.

    The topic assumes that the user has a basic understanding of what HTTP is, what a GET request is, what a server is, an IP address, and so on.

    Project motivation
    Arduino R4 WiFi has some ready-made examples of network projects:
    Screenshot of Arduino menu with the WiFi WebServer example selected.
    But after opening them, to our surprise, it turns out that HTTP parsing is implemented in a very simplified way and is not packed into separate classes:
    Arduino code snippet showing HTTP request handling.
    So I started looking for a library that would offer it to me.
    I was interested in QuickSander job:
    https://github.com/QuickSander/ArduinoHttpServer
    However, as it soon turned out, there are a few problems with it:
    - does not compile under Arduino R4 WiFi
    - the examples are quite poor
    - does not support parsing GET arguments, i.e. a link like:
    
    https://www.elektroda.pl/rtvforum/search.php?search_id=egosearch&user=1234
    

    The link above contains two arguments: one with the key "search_id" and the value "egosearch", and the other with the key "user" and the value "1234". It would be nice to be able to get them easily from this address....
    In this topic, I will try to fix all three issues.

    ArduinoHttpServer installation and fixes
    We download ArduinoHttpServer from Libraries Manager, by the way, you also need to install one dependency, namely Base64:
    ArduinoHttpServer library with a popup window of missing dependencies
    Then you can try to compile their example from Github but it will fail:
    Code: C / C++
    Log in, to see the code

    We will get a compilation error like this:
    Screenshot of Arduino R4 WiFi compilation error.
    The pgmspace.h header is missing. We'll fix it right away. It is not actually needed in this case.
    We open the library code, for me it is under:
    
    C:\Users\Admin\Documents\Arduino\libraries\Base64\src\Base64.cpp
    

    Just comment out this block (or add define for R4):
    Edited source code in the Base64.cpp file with a highlighted conditional block.
    Now the example should work, but if we sometimes get a message like this:
    Error 400 message: Timeout occurred while waiting for data.
    It's worth taking a look at:
    
    C:\Users\Admin\Documents\Arduino\libraries\ArduinoHttpServer\src\internals\StreamHttpRequest.hpp
    

    The problem is this piece of code:
    A snippet of ArduinoHttpServer code with a loop checking for data availability in the stream.
    It just detects a timeout error. Initially, I wanted to increase the value of the MAX_RETRIES_WAIT_DATA_AVAILABLE constant:
    Code snippet in a text editor with a highlighted line about the constant MAX_RETRIES_WAIT_DATA_AVAILABLE.
    but in the end I just commented out the whole block. From then on, the whole thing started to work a little more stable.
    C++ code with a commented-out block checking for timeout.
    I don't know how correct this approach is, but after this change, this library works a bit more stable for me.

    Usage example from the authors of ArduinoHttpServer
    Let's take a moment to test the sample ArduinoHttpServer code provided by the author of the library. This code offers a simple HTTP authorization based on a mechanism built into the browser:
    Code: C / C++
    Log in, to see the code

    Indeed, after entering the site, we have a window asking for data:
    HTTP login prompt on a local page requesting username and password.
    After entering the wrong data in the log, we get:
    Screenshot of console messages about client connection and authorization errors.
    However, after entering the correct ones:
    Console screen with logs of Arduino program operation
    Authorization may be useful, but strangely enough, I don't see anything more fundamental anywhere, i.e. the ability to extract arguments from a GET request. We'll take care of it right away.

    Argument parsing implementation
    A short analysis of the library code shows that the HttpResource class is responsible for parsing the resource address. The author in this class only keeps one string and splits it up as needed:
    Code: C / C++
    Log in, to see the code

    The [] operator allows you to get to specific folders in the address of the resource:
    Code: C / C++
    Log in, to see the code

    All the magic happens in the snippet with the variable textField . The code checks if there is a GET argument with that name, and if so, it includes a message in the HTML displaying the textField value. If not, the display skips. In both cases, I continue to create an HTML form with a field named textField so that the user can easily send a GET request with the new value of this variable.
    After opening the page we see:
    Web page in a browser with a text input form and a Submit button.
    After typing the text "ArduinoFan" and sending:
    HTML page with a form field and user greeting.
    My GET argument handling seems to be working.

    Hint - ArduinoHttpServer building and streams
    Now I would like to point out a useful feature of the discussed library resulting from object-oriented and inheritance available in the environment used.
    Consider the StreamHttpReply constructor that represents the HTTP response:
    Code: C / C++
    Log in, to see the code

    The first argument is an object of the Stream class, i.e. a stream. Various classes inherit from the Stream class, including network stream. Using StreamHttpReply then looks like this:
    Code: C / C++
    Log in, to see the code

    It was in the example you posted. But a stream is also an object Series , which is the Arduino serial port. This allows us to construct the following:
    Code: C / C++
    Log in, to see the code

    It may seem strange, but nothing prevents you from using the HTTP protocol on the UART stream, instead of the TCP stream. Let's consider a larger example:
    Code: C / C++
    Log in, to see the code

    Result in the console:
    Screenshot of HTML results and HTTP header in console.
    That's right, this code just printed the sent HTML with the HTTP header appended. This very nicely shows the hermetic and modular structure of this library.

    Summary
    Quite a nice and useful library. There were no big problems with porting it to R4, which is not surprising, because its mechanisms are based on the Stream class, which can be a common interface for many streams.
    A bit strange that at the moment of writing this topic, this library did not support easy access to GET arguments, but it was enough to add one function to change it.
    I'll test my code a bit more and then I'll open it pull request on Github to suggest my changes and improvements, maybe the author of the library will accept them and add them to his code.
    That's it for now. Has anyone of the readers already created an HTTP server on Arduino R4? Do you see any potential uses for this library?
    PS: Below is the patch I sent to the author of the library: https://github.com/QuickSander/ArduinoHttpServer/pull/23

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11842 posts with rating 9937, helped 566 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21098963
    ripnetru
    Level 4  
    Hello,

    I have R4 Uno and when compile get error

    Compilation error: 'const class ArduinoHttpServer::HttpResource' has no member named 'getArgument'

    Added after 3 [minutes]:

    String ssid = httpRequest.getResource().getArgument("ssid");
    String pass = httpRequest.getResource().getArgument("pass");
    String ap = httpRequest.getResource().getArgument("ap");
  • ADVERTISEMENT
  • #3 21098991
    p.kaczmarek2
    Moderator Smart Home
    This is because you need to use my version of the library. You can see the code here:
    https://github.com/QuickSander/ArduinoHttpServer/pull/23/files
    You can just copy and paste it. Let me know if you need futher assistance.
    Helpful post? Buy me a coffee.
  • #4 21099044
    ripnetru
    Level 4  
    Good, now working, i have error 400 too and fix as you write. I setup wifi credits and it working, after reboot
    beginSTA with
    SSID
    pass

    Its ok, but i don't understand how working link return on AP mode, if i alleady put wifi ssid and password, how i can reset to default without Arduino IDE. They try to connect and AP mode not working as i see. Thank you.

    Added after 7 [minutes]:

    I see new ip and its ok, Sorry.

    [WiFiManager]: starting AP

    Added after 6 [minutes]:

    Thank you very much. It very good.
  • ADVERTISEMENT
  • #5 21100974
    ripnetru
    Level 4  
    Help please if it possible at all.

    In form we have ssid and pass, and i want if ssid and pass saved to show it on web form in input field.

    " <label for=\"ssid\">Enter SSID:</label><br>\n"
    " <input type=\"text\" id=\"ssid\" name=\"ssid\"><br><br>\n"

    how to add set->inner.sta_ssid in input value. I search examples but not found how make it.
  • #6 21101023
    p.kaczmarek2
    Moderator Smart Home
    Can you please provide the full code you are working on currently?

    If you are referring to the HTML page generation, then you can do something like this:
    Code: HTML, XML
    Log in, to see the code

    So basically just add a 'value' field to the input tag.
    Helpful post? Buy me a coffee.
  • #7 21101041
    ripnetru
    Level 4  
    I no have code, working with your original code https://www.elektroda.com/rtvforum/topic4000214.html

    i want something like this:

    " <label for=\"ssid\">Enter SSID:</label><br>\n"
    " <input type=\"text\" id=\"ssid\" name=\"ssid\" value=\"123\"><br><br>\n"

    but 123 change to real value from eeprom. How to properly connect С++ code with html.
  • #8 21101095
    p.kaczmarek2
    Moderator Smart Home
    I see, so this is my old code snipper:
    Code: C / C++
    Log in, to see the code

    We can modify it, first we need to get old SSID, as a C string pointer:
    Code: C / C++
    Log in, to see the code

    Then plug it into HTML:
    Code: C / C++
    Log in, to see the code

    Please note that it's just an example and it may require futher tuning, I didn't compile it on my side. You just basically need to access old SSID string and plug it into the new HTML, into the already mentioned value field.
    Helpful post? Buy me a coffee.
  • #9 21101142
    ripnetru
    Level 4  
    I really appreciate it.

    I just checked, it works as I wanted. Thank you!
  • #10 21101373
    p.kaczmarek2
    Moderator Smart Home
    Sure, let me know if you need any futher help
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #11 21105141
    ripnetru
    Level 4  
    Hello,

    Yes, i have one more question, when he working in STA mode, some times he loses connect, maybe it's interference (2,4 GHz)

    (00:43:00.872 -> Going to read settings...
    00:43:00.872 -> Settings correctly loaded!
    00:43:00.872 -> [WiFiManager]: starting STA
    00:43:00.872 -> beginSTA with
    00:43:00.872 -> SSID
    00:43:03.168 -> IP Address: 192.168.222.229
    00:43:03.216 -> signal strength (RSSI):-48 dBm

    As i understand void beginSTA() uses only once.
    And after loses connection with router he not try reconnected. How to add this option?
  • #12 21106192
    ripnetru
    Level 4  
    I try to make this:

    void loop() {
    int status = WL_IDLE_STATUS;
    while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println("Pixel");
    status = WiFi.begin("Pixel", "pass");
    }
    }

    It working, but it static, if i use
    status = WiFi.begin(set->inner.sta_ssid, set->inner.sta_pass);
    he get error when compile code. void loop don't know about inner.sta_pass and inner.sta_ssid

    I try to add

    const char *oldSsid = set->inner.ap_ssid;
    const char *oldPass = set->inner.ap_pass;

    but he get error on set...
  • #13 21115631
    ripnetru
    Level 4  
    I figured it out, I didn’t know that variables can be taken higher than loop and setup and I made them similar to your example

    #include <ArduinoHttpServer.h>

    const char *ssidglobal;
    const char *passglobal;

    ..
    in class
    ..
    ssidglobal = set->inner.sta_ssid;
    passglobal = set->inner.sta_pass;
    ...
    And now working in loop how i want.
    ...
    status = WiFi.begin(ssidglobal, passglobal);
    ...

    I'm just experimenting because I really like it. Sorry if I distracted you.

Topic summary

The discussion focuses on the Arduino R4 WiFi and the ArduinoHttpServer library, addressing issues related to HTTP request parsing and response handling. Users encountered compilation errors, particularly with the 'getArgument' method, which were resolved by using a specific version of the library. The conversation also covers setting up WiFi credentials, displaying saved SSID and password in a web form, and handling connection stability in STA mode. Solutions included modifying HTML input fields to show stored values and ensuring the device attempts to reconnect to WiFi after losing connection. Users shared code snippets and troubleshooting tips to enhance functionality.
Summary generated by the language model.
ADVERTISEMENT