HTTP requests from Arduino - Time & Space Complexity
When an Arduino sends HTTP requests, it performs steps repeatedly to communicate with a server.
We want to understand how the time it takes grows as the number of requests increases.
Analyze the time complexity of the following code snippet.
#include <WiFi.h>
#include <HTTPClient.h>
void sendRequests(int n) {
for (int i = 0; i < n; i++) {
HTTPClient http;
http.begin("http://example.com/data");
int code = http.GET();
http.end();
}
}
This code sends n HTTP GET requests one after another to a server.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending an HTTP GET request inside the loop.
- How many times: Exactly
ntimes, once per loop iteration.
Each request takes roughly the same time, so total time grows as we send more requests.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 HTTP requests |
| 100 | 100 HTTP requests |
| 1000 | 1000 HTTP requests |
Pattern observation: The total time grows directly in proportion to the number of requests.
Time Complexity: O(n)
This means if you double the number of requests, the total time roughly doubles too.
[X] Wrong: "Sending multiple HTTP requests happens all at once, so time stays the same no matter how many requests."
[OK] Correct: Each request waits for a response before the next starts, so time adds up with each request.
Understanding how repeated network calls affect time helps you design better programs and explain your choices clearly.
"What if we sent all HTTP requests at the same time using asynchronous calls? How would the time complexity change?"
Practice
HTTPClient in Arduino sketches?Solution
Step 1: Understand the role of HTTPClient
TheHTTPClientlibrary is designed to simplify sending HTTP requests like GET and POST from Arduino.Step 2: Differentiate from other libraries
Connecting to WiFi is done byWiFiClient, notHTTPClient. Reading sensors or controlling pins is unrelated.Final Answer:
To send and receive HTTP requests easily -> Option DQuick Check:
HTTPClient = HTTP requests [OK]
- Confusing HTTPClient with WiFi connection library
- Thinking HTTPClient reads sensors
- Assuming HTTPClient controls pins
HTTPClient in Arduino?Solution
Step 1: Recall HTTPClient method for starting requests
The correct method to initialize a request isbegin()with the URL as argument.Step 2: Check other options
Methods likeconnect(),startGET(), oropen()do not exist in HTTPClient library.Final Answer:
http.begin("http://example.com"); -> Option CQuick Check:
Use begin() to start HTTP request [OK]
- Using non-existent methods like connect() or open()
- Confusing begin() with WiFi connection functions
- Forgetting to pass the URL string
http://example.com/data returns status 200 and body "OK"?
HTTPClient http;
http.begin("http://example.com/data");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error");
}
http.end();Solution
Step 1: Analyze the HTTP GET response code
The code checks ifhttpCode > 0. Since the server returns 200, this condition is true.Step 2: Print status code and response body
The code prints the status code (200) and then the response body ("OK") on separate lines.Final Answer:
200 OK -> Option AQuick Check:
200 status means success, prints code and body [OK]
- Assuming error when status is 200
- Confusing status code with response body
- Not calling http.end() to close connection
HTTPClient http;
http.begin("http://example.com/api");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST("{\"value\": 42}");
if (httpCode == 200) {
Serial.println("Success");
}
http.end();Solution
Step 1: Check prerequisites for HTTP requests
Before usingHTTPClient, Arduino must be connected to WiFi. This code snippet does not show WiFi connection setup.Step 2: Validate headers and method usage
Header name "Content-Type" is correct, POST accepts a string argument, andhttp.end()is correctly called after POST.Final Answer:
Missing WiFi connection before HTTPClient usage -> Option BQuick Check:
WiFi must connect before HTTPClient [OK]
- Skipping WiFi connection setup
- Using wrong header names
- Calling http.end() too early
Solution
Step 1: Establish WiFi connection first
Arduino must connect to WiFi before making HTTP requests.Step 2: Follow correct HTTPClient method order
Start withhttp.begin(url), then set headers, then send POST with data, check response code, and finally callhttp.end().Final Answer:
Connect WiFi -> http.begin(url) -> http.addHeader("Content-Type", "application/json") -> http.POST(jsonData) -> check httpCode -> http.end() -> Option AQuick Check:
WiFi -> begin() -> headers -> POST -> end() [OK]
- Calling POST before begin()
- Adding headers after POST
- Skipping WiFi connection
Ready to prove it?
Pass the test and this pattern moves unknown
