logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

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

p.kaczmarek2  16 4215 Cool? (+5)
📢 Listen (AI):

TL;DR

  • ArduinoHttpServer gets fixed and adapted for Arduino R4 WiFi to simplify HTTP request parsing, response handling, browser authentication, and GET-argument extraction.
  • The port relies on the library’s Stream-based design, so the same HTTP reply code works over network streams and even over the Arduino Serial UART stream.
  • A sample GET link like `search_id=egosearch&user=1234` demonstrates the new argument parsing, and the author also shows browser Basic-style authorization working.
  • Two fixes make the library compile and run better on R4 WiFi: comment out the unnecessary `pgmspace.h` block in Base64 and disable a timeout check in `StreamHttpRequest.hpp`.
  • The original library lacked easy access to GET arguments and showed occasional timeout errors, so the author patched it and planned a GitHub pull request.
Generated by the language model.
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

About Author
p.kaczmarek2
p.kaczmarek2 wrote 14627 posts with rating 12646 , helped 655 times. Been with us since 2014 year.

Comments

ripnetru 28 May 2024 19:42

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... [Read more]

p.kaczmarek2 28 May 2024 20:17

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... [Read more]

ripnetru 28 May 2024 21:05

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... [Read more]

ripnetru 30 May 2024 17:10

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" "... [Read more]

p.kaczmarek2 30 May 2024 17:35

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: <input type="text" id="ssid" name="ssid"... [Read more]

ripnetru 30 May 2024 17:42

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" "... [Read more]

p.kaczmarek2 30 May 2024 18:16

I see, so this is my old code snipper: reply += " <form>\n" " <label for=\"ssid\">Enter SSID:</label><br>\n" " <input... [Read more]

ripnetru 30 May 2024 18:49

I really appreciate it. I just checked, it works as I wanted. Thank you! [Read more]

p.kaczmarek2 30 May 2024 21:27

Sure, let me know if you need any futher help [Read more]

ripnetru 02 Jun 2024 23:54

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... [Read more]

ripnetru 03 Jun 2024 20:54

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",... [Read more]

ripnetru 11 Jun 2024 19:37

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... [Read more]

electromechpro 15 Jul 2025 22:24

Thank you for the response. I was able to get the example to compile and download, but the R4 did not show the ssid I had entered in the code. Per Post #3 21098991 28 May 2024 20:17, I am stuck trying... [Read more]

p.kaczmarek2 15 Jul 2025 22:30

What is your current code? Post #3 shows my changes to HTTP library. You can apply them manualy to HttpResource code. If you don't want to manually patch that library, here is my version (already... [Read more]

electromechpro 15 Jul 2025 23:00

The patched library worked! It is not that I did not want to patch the existing library, but that I did not understand how to. I first deleted the existing library subdirectory from the libraries directory,... [Read more]

electromechpro 17 Jul 2025 19:15

Thank you again for the help with the library and the code example you sent. I have been studying how to implement it in some of my instrumentation projects. In the ArduinoR4DIYWiFiManagerExample.ino... [Read more]

FAQ

TL;DR: For Arduino Uno R4 WiFi users, this fixes 3 concrete issues in ArduinoHttpServer—compile failure, HTTP 400/timeouts, and missing GET parsing. "You need to use my version of the library," the author says, then points to a patched fork and working WiFi manager example for AP/STA switching. [#21608014]

Why it matters: It turns a fragile demo into a reusable HTTP stack for forms, credential capture, EEPROM-backed settings, and browser-based Wi-Fi setup on Uno R4 WiFi.

Option Compiles on Uno R4 WiFi GET args like ssid Reliability notes
Arduino R4 built-in manual examples Yes Manual parsing only Simple, but harder to maintain
Original QuickSander ArduinoHttpServer Not reliably No Missing pgmspace.h path and timeout issues
Patched/forked ArduinoHttpServer Yes Yes Forum users confirmed it working

Key insight: The most valuable fix is not the compile patch; it is extending HttpResource so the library can parse query strings cleanly. That unlocks practical web forms, Wi-Fi provisioning, and mode switching without hand-parsing every URL.

Quick Facts

  • The example HTTP server runs on WiFiServer(80), so the board serves pages over standard HTTP port 80. [#20698211]
  • The serial examples and diagnostics use Serial.begin(115200), which makes HTTP parsing and Wi-Fi status easy to inspect at 115200 baud. [#20698211]
  • The WiFi manager example uses StreamHttpRequest<1024>, giving the request parser a 1024-byte template buffer in the sketch. [#21608014]
  • A posted STA log shows a live connection at 192.168.222.229 with RSSI -48 dBm, confirming the R4 can serve pages in station mode after provisioning. [#21105141]
  • Mode changes in the example are delayed by setupReconnectAfter(3000), so AP↔STA switching is scheduled after about 3000 ms. [#21608014]

How do I get ArduinoHttpServer to compile on the Arduino Uno R4 WiFi when it fails because of a missing pgmspace.h header?

Remove or conditionally disable the pgmspace.h include in the Base64 dependency, because that header is the compile blocker on Uno R4 WiFi. The thread points to Base64.cpp under the Arduino libraries folder and says the block can be commented out, or a board-specific define can be added for R4. After that edit, the ArduinoHttpServer example compiles instead of failing at the dependency stage. [#20698211]

What changes are needed in the Base64 and ArduinoHttpServer libraries to make them work more reliably on Arduino R4 WiFi?

You need two changes: patch Base64 for the missing pgmspace.h path, and relax the request timeout behavior in StreamHttpRequest.hpp. The thread says the Base64 fix is a small source edit, while the ArduinoHttpServer change targets the timeout-detection block and the MAX_RETRIES_WAIT_DATA_AVAILABLE logic. The author first considered increasing the retry count, then simply commented out the whole timeout block because it behaved more stably on R4 WiFi. [#20698211]

Why does ArduinoHttpServer on the Arduino R4 WiFi sometimes return HTTP 400 or timeout-related parsing errors, and how can I fix that?

It returns HTTP 400 because the library can flag a timeout while reading request data, even when the R4 WiFi link is still usable. The thread identifies a timeout-check block in StreamHttpRequest.hpp as the trigger. To fix it, either raise MAX_RETRIES_WAIT_DATA_AVAILABLE or comment out that timeout block. A later user confirmed, "now working, i have error 400 too and fix as you write," which validates the workaround in real use. [#21099044]

How can I add GET argument parsing like getArgument("ssid") or getArgument("pass") to ArduinoHttpServer on Arduino Uno R4 WiFi?

Add a query-string parser to HttpResource, then call httpRequest.getResource().getArgument("ssid") or similar. The original library only split URL path segments such as /api/sensors/on; it did not expose query pairs like ?ssid=x&pass=y. The author implemented a getArgument method, used it in forms, and later shared the patch as GitHub pull request #23. Without that patched code, the call will not compile. [#20698211]

What's the correct way to use the patched ArduinoHttpServer fork instead of the original QuickSander version on Arduino R4 WiFi?

Install the already patched fork instead of the original library if you do not want to edit source files manually. In July 2025, the author linked a forked repository with the fixes already applied and said users could use that version directly. Another user confirmed a practical method: delete the existing Arduino library folder, import the fork ZIP through the Arduino IDE, then compile the example again. [#21608061]

How do I build a simple WiFi manager page on Arduino Uno R4 WiFi that switches between AP mode and STA mode using ArduinoHttpServer?

Build one page that reads GET fields, stores credentials, and schedules a mode change. 1. Start either WiFi.beginAP(...) or WiFi.begin(...) from saved EEPROM settings. 2. Serve a form with ssid, pass, and an ap=1 return link on port 80. 3. After saving values, call setupReconnectAfter(3000) and restart in the selected mode. The shared example stores AP and STA credentials in EEPROM and switches modes through browser requests instead of the IDE. [#21608014]

What is the HttpResource class in ArduinoHttpServer, and how does it parse a URL path and query string?

HttpResource is the library class that stores the requested resource string and exposes parsed pieces of it. In the original code, it kept one String, split path segments by /, and let callers read them with operator[]. The author extended that same class so it could also search the query string and return values for keys such as textField, ssid, or pass. That turns one URL into both path access and simple GET parameter lookup. [#20698211]

What is a pull request on GitHub, and how does it relate to sharing ArduinoHttpServer fixes like the GET argument patch?

A pull request is a GitHub change proposal that asks a project maintainer to review and merge code into the main repository. The author said he planned to test the code more, then open a pull request, and later linked the submitted patch as pull request #23. "Pull request" is a GitHub review request that bundles code changes, shows file diffs, and lets maintainers accept or reject a fix. [#20698211]

How can I prefill an HTML form input on an ArduinoHttpServer page with a saved EEPROM value such as the current SSID?

Insert the saved value into the HTML value attribute when you build the reply string. The thread shows the pattern directly: read the old SSID into a C string pointer, convert it with String(oldSsid), and concatenate it inside <input ... value="...">. A user tested that approach on May 30, 2024 and confirmed it worked as intended with the saved setting shown in the form. [#21101142]

Why does my Arduino Uno R4 WiFi stay in STA mode after saving Wi-Fi credentials, and how can I force it back to AP mode without using the Arduino IDE?

It stays in STA mode because the example saves bStation = true after storing station credentials, so the next boot reconnects as a client. To force AP mode without the IDE, add a browser action that clears or overrides that state. The shared WiFi manager does this with an ap=1 link, openAccessPoint(), and setupSTAMode(false), then schedules reconnection after 3000 ms so the board reopens its AP automatically. [#21608014]

How should I reconnect an Arduino Uno R4 WiFi automatically after it loses STA connection to the router, instead of only calling beginSTA() once?

Check Wi-Fi status in loop() and call WiFi.begin(...) again when the board is no longer connected. A user first proved the reconnection concept with a while (status != WL_CONNECTED) loop and static credentials. Later he fixed variable scope by moving saved SSID and password pointers outside setup() and loop(), then reused them during reconnect attempts. His posted STA log showed normal operation at 2.4 GHz Wi‑Fi with RSSI -48 dBm before dropouts. [#21115631]

Where is the wm.poll() function defined in the ArduinoR4DIYWiFiManagerExample.ino, and how do I use it if I want to assign a button pin for mode switching?

wm.poll() is defined inside the sketch’s custom WiFiManager class, not inside the patched ArduinoHttpServer library. In the shared .ino, poll() handles scheduled reconnects, AP status messages, HTTP client servicing, and a placeholder comment for button handling. To add a hardware mode switch, read your chosen pin in loop() and call wm.openAccessPoint() when the button is pressed; the sketch already shows that hook as if(0) { wm.openAccessPoint(); }. [#21608014]

Can I use ArduinoHttpServer over Serial instead of WiFiClient, and what practical use cases are there for sending HTTP-style replies through a UART stream?

Yes. The library reply class accepts any Arduino Stream, so you can send an HTTP-style response to Serial instead of WiFiClient. The thread demonstrates StreamHttpReply httpReply(Serial, "text/html");, which prints HTML plus HTTP headers to UART. That is useful for debugging generated pages, testing response formatting without networking, or reusing the same output code across TCP and serial transports. [#20698211]

ArduinoHttpServer vs manually parsing HTTP requests in Arduino R4 WiFi examples — which approach is better for maintainability and GET parameter handling?

ArduinoHttpServer is better for maintainability once patched, because it encapsulates parsing into classes instead of scattered string handling. The author started this work because the built-in Arduino R4 WiFi examples parsed HTTP in a very simplified, non-modular way. After patching, the library handled headers, resource paths, authentication, and GET arguments through methods such as getResource() and getArgument(). Manual parsing still works, but it scales poorly when forms and multiple parameters appear. [#20698211]

What could cause an Arduino Uno R4 WiFi sketch with WiFi manager code to upload unreliably or crash at random percentages during flashing?

The thread reports unreliable flashing with the WiFi manager sketch, but it does not isolate a confirmed root cause. One user said the example compiled, yet needed several upload attempts because the R4 crashed at random completion percentages, while other sketches uploaded normally. That makes the failure an observed sketch-specific edge case, not a documented ArduinoHttpServer feature. Treat it as an implementation issue and simplify or test incrementally before assuming the library alone is at fault. [#21608061]
Generated by the language model.
%}