Sub topic 2
Checking status, IP address, and signal strength
This chapter assumes you know basic Arduino sketches, can install the WiFiNINA library, and have a board such as the MKR WiFi 1010 or Nano 33 IoT. You should also know what an IP address, port, and SSID are. Your credentials live in arduino_secrets.h, which defines SECRET_SSID and SECRET_PASS.
Reading connection status
WiFi.begin() returns a status code. WL_CONNECTED means the board has joined the network and holds an IP address. WL_NO_MODULE means the Wi-Fi radio is missing or failed.
Signal strength
WiFi.RSSI() returns the received signal strength in dBm. Values closer to 0 are stronger. Around -50 is strong, and below -80 is weak and may drop packets.
Making an HTTP request
HTTP is a text protocol sent over a TCP connection. A GET request asks a server for a page. The sketch below connects to port 80, sends the request, and reads the status line of the response.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiClient client;
const char host[] = "example.com";
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) {
delay(5000);
}
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print(" RSSI: ");
Serial.println(WiFi.RSSI());
if (!client.connect(host, 80)) {
Serial.println("Connection failed");
return;
}
client.println("GET / HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
while (client.connected() && !client.available()) {}
String statusLine = client.readStringUntil('\n');
statusLine.trim();
Serial.println(statusLine);
client.stop();
}
void loop() {}Moving to HTTPS
HTTPS wraps HTTP in TLS, which encrypts the traffic. To switch, replace WiFiClient with WiFiSSLClient and connect to port 443. The board checks the server's certificate against trusted root certificates stored in the Wi-Fi module. If that check fails, connect() returns false.
Reading and parsing the response
A response starts with a status line such as HTTP/1.1 200 OK, followed by headers, a blank line, and the body. Characters 9 to 11 of the status line hold the three-digit code.
int code = statusLine.substring(9, 12).toInt();
Serial.print("Status code: ");
Serial.println(code);Exercise
The status line reads "HTTP/1.1 404 Not Found". What value does code hold after the parsing step, and what does a 404 mean?
Show answer
code holds 404. A 404 means the server found no resource at the requested path.