Sub topic 5
Checking Status, IP Address, and Signal Strength
Assume you know how to upload an Arduino sketch, use setup() and loop(), and understand basic terms such as SSID, IP address, and router. This chapter uses a Wi-FiNINA board, such as the MKR WiFi 1010 or Nano 33 IoT, with the WiFiNINA library.
Connecting to Wi-Fi is only the first step. A reliable sketch should also check whether the connection is still active, print the board's IP address, and measure the received signal strength.
What the status values mean
WiFi.status() returns a status constant. The most useful values are:
| Status | Meaning |
|---|---|
| `WL_CONNECTED` | The board is connected to Wi-Fi. |
| `WL_IDLE_STATUS` | The Wi-Fi hardware is idle or still working. |
| `WL_NO_MODULE` | The Wi-Fi module was not detected. |
| `WL_CONNECTION_LOST` | The connection was lost. |
| `WL_CONNECT_FAILED` | The connection attempt failed. |
A connected board usually receives an IP address from the router through DHCP. DHCP is the process that automatically assigns network settings to devices. WiFi.localIP() returns the address assigned to the board.
The signal strength is returned by WiFi.RSSI(). RSSI means received signal strength indicator. It is measured in dBm, a logarithmic unit. RSSI values are normally negative:
- Around
-30 dBm: very strong - Around
-60 dBm: usually good - Around
-70 dBm: usable but less reliable - Around
-80 dBmor lower: weak
RSSI is useful for diagnosing problems, but it is not a guarantee of network performance. Interference, congestion, and router configuration can also cause failures.
Print connection information
The following sketch connects to Wi-Fi and prints the status, IP address, and RSSI once every five seconds.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
void printConnectionInfo() {
int status = WiFi.status();
Serial.print("Status: ");
switch (status) {
case WL_CONNECTED:
Serial.println("connected");
break;
case WL_NO_MODULE:
Serial.println("Wi-Fi module not found");
break;
case WL_CONNECTION_LOST:
Serial.println("connection lost");
break;
case WL_CONNECT_FAILED:
Serial.println("connection failed");
break;
default:
Serial.println("not connected");
break;
}
if (status == WL_CONNECTED) {
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
}
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.println("Connecting...");
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(10000);
}
printConnectionInfo();
}
void loop() {
printConnectionInfo();
delay(5000);
}The arduino_secrets.h file must define the two credentials used by the sketch:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"The sketch checks for a missing Wi-Fi module before trying to connect. After connecting, it prints network information only when the status is WL_CONNECTED. This prevents the sketch from displaying an invalid or incomplete IP address.
Read the output
The Serial Monitor might show output like this:
Status: connected
IP address: 192.168.1.42
RSSI: -58 dBmThe address 192.168.1.42 is a private address on the local network. Other devices on that same network can usually use it to communicate with the board. The address may change after the board reconnects or after the router's DHCP lease expires, so do not assume it is permanent.
Exercise
The board reports WL_CONNECTED, an IP address of 192.168.1.42, and an RSSI of -82 dBm. What does this tell you, and what should you investigate first?
Show answer
The board has joined the network and received an IP address, but its signal is weak. First check the board's distance from the router, obstacles, interference, and whether the network is the expected 2.4 GHz network.