Consider this Arduino sketch snippet that makes an HTTP GET request to a server and prints the response code. What will be printed to the Serial Monitor?
WiFiClient client;
HTTPClient http;
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
http.begin(client, "http://example.com");
int httpCode = http.GET();
Serial.println(httpCode);
http.end();
}
void loop() {}HTTP status code 200 means success. The code variable holds the HTTP response code.
The http.GET() function returns the HTTP status code from the server. If the request is successful, it returns 200. Other values indicate errors or no connection.
You want to send HTTP GET and POST requests from your Arduino sketch. Which library provides the HTTPClient class to do this?
Look for the library that has 'HTTP' in its name and provides HTTP methods.
The HTTPClient class is part of the HTTPClient.h library, which simplifies HTTP requests on Arduino.
Examine the code below. It tries to send JSON data via HTTP POST but the server never receives it. What is the main reason for failure?
WiFiClient client;
HTTPClient http;
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
http.begin(client, "http://example.com/api");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST("{\"temp\": 25}");
Serial.println(httpCode);
http.end();
}
void loop() {}Check the WiFi connection status before making HTTP requests.
If WiFi is not connected, HTTP requests will fail. The code waits for connection, so this is unlikely. But if the wait loop is skipped or WiFi fails, POST will fail.
Find the option that corrects the syntax error in this code snippet:
http.begin(client, "http://example.com") int code = http.GET() Serial.println(code)
Arduino C++ requires statement terminators.
Each statement in Arduino C++ must end with a semicolon. Missing semicolons cause syntax errors.
Given this Arduino code sending a JSON payload via HTTP POST, how many bytes are sent in the request body?
String payload = "{\"sensor\":\"temp\",\"value\":23}";
http.begin(client, "http://example.com/data");
http.addHeader("Content-Type", "application/json");
int code = http.POST(payload);
http.end();Count all characters including quotes and braces in the JSON string.
The payload string is exactly 28 characters long, so 28 bytes are sent in the POST body.