Sub topic 13
Troubleshooting connection problems
This chapter gives you a way to find out why a Wi-Fi board fails to connect or to talk to a server. It assumes you have read the earlier chapters. You should know how to connect with WiFi.begin, read WiFi.status(), make HTTP and HTTPS requests with client objects, and use MQTT. The examples use the WiFiNINA library (MKR WiFi 1010, Nano 33 IoT), but the method applies to other Wi-Fi boards.
Work through the layers in order
A network request passes through several layers. Each one depends on the one before it. A failure at a lower layer looks like a failure at every layer above it, so always test from the bottom up.
The sections below follow this order.
Layer 1: the module does not respond
If WiFi.status() returns WL_NO_MODULE, the board cannot talk to its Wi-Fi chip. Common causes:
- You selected the wrong board in the IDE, or compiled with a library that does not match the board.
- The Wi-Fi module firmware is very old. You can update it with the IDE's firmware updater tool, the same tool that manages the certificate store.
- The sketch ran before the module finished starting, which is rare. A power cycle clears it.
Print the firmware version and compare it with the version the library expects:
String fv = WiFi.firmwareVersion();
Serial.println(fv);
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}Layer 2: the network is not visible
Most Wi-Fi boards support only the 2.4 GHz band. If your router merges 2.4 GHz and 5 GHz under one name, many routers handle this fine, but some push the board toward a band it cannot use. Scan to see what the board can actually hear. The scan shows the SSID, signal strength, and encryption type of every network in range.
If your network does not appear in the scan, check these in order:
- The router broadcasts on 2.4 GHz, and the channel is one the board supports. Channels 1 to 11 work everywhere, and 12 to 14 are not available in all regions.
- The board is within range. Walls and metal enclosures reduce signal sharply.
- The SSID is not hidden. Hidden networks can still work with
WiFi.begin, but they make debugging harder, so test with the SSID visible first.
Layer 3: joining the network fails
When the network is visible but WiFi.begin does not return WL_CONNECTED, the status code narrows the cause.
| Status | Meaning | Usual cause |
|---|---|---|
| `WL_NO_MODULE` | No Wi-Fi module found | Wrong board selected, old firmware |
| `WL_IDLE_STATUS` | Module is waiting | Connection attempt still in progress |
| `WL_NO_SSID_AVAIL` | The SSID was not found | Typo in SSID, 5 GHz only network, out of range |
| `WL_CONNECT_FAILED` | The join was rejected | Wrong password, unsupported security mode |
| `WL_CONNECTION_LOST` | An established link dropped | Weak signal, router restart |
| `WL_DISCONNECTED` | Not connected | Never joined, or the router disconnected the board |
| `WL_CONNECTED` | Joined with an IP address | None |
The most frequent causes are small. The SSID and password are case-sensitive. A trailing space in arduino_secrets.h will break the match. WPA3-only networks and enterprise (802.1X) networks are not supported by WiFi.begin(ssid, pass), so move the board to a WPA2 personal network or a guest network.
A join can also take several seconds. If you check the status immediately after WiFi.begin, you may read WL_IDLE_STATUS and wrongly conclude it failed. The earlier chapters used a delay or a retry loop for this reason.
Layer 4: joined, but no internet
A board can hold a valid IP address and still have no route to the internet. The router gave it an address, but the router itself may have lost its uplink, or a guest network may block outbound traffic or require a login page (a captive portal). Captive portals are a frequent problem on hotel, school, and office networks, because the board cannot click through a web page.
To separate a local problem from an internet problem, print the gateway address and test name resolution. A resolved name means DNS works. A name that fails to resolve while the link is up points at DNS or the uplink.
A diagnostic sketch
This sketch tests every layer and prints a result for each. Run it first whenever a project will not connect. It uses example.com for the name test.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
const char* statusName(int s) {
switch (s) {
case WL_NO_MODULE: return "WL_NO_MODULE";
case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
case WL_CONNECTED: return "WL_CONNECTED";
case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
case WL_DISCONNECTED: return "WL_DISCONNECTED";
default: return "UNKNOWN";
}
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
// Layer 1: module
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("FAIL layer 1: no Wi-Fi module");
while (true) {}
}
Serial.print("Firmware: ");
Serial.println(WiFi.firmwareVersion());
// Layer 2: scan
int found = WiFi.scanNetworks();
Serial.print("Networks in range: ");
Serial.println(found);
for (int i = 0; i < found; i++) {
Serial.print(" ");
Serial.print(WiFi.SSID(i));
Serial.print(" RSSI ");
Serial.print(WiFi.RSSI(i));
Serial.print(" encryption type ");
Serial.println(WiFi.encryptionType(i));
}
// Layer 3: join
int status = WL_IDLE_STATUS;
for (int attempt = 1; attempt <= 3 && status != WL_CONNECTED; attempt++) {
Serial.print("Join attempt ");
Serial.println(attempt);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(8000);
status = WiFi.status();
Serial.print(" status: ");
Serial.println(statusName(status));
}
if (status != WL_CONNECTED) {
Serial.println("FAIL layer 3: could not join");
while (true) {}
}
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
// Layer 4: DNS
IPAddress resolved;
if (WiFi.hostByName("example.com", resolved) != 1) {
Serial.println("FAIL layer 4: DNS lookup failed");
while (true) {}
}
Serial.print("example.com is ");
Serial.println(resolved);
// Layer 5: TCP
WiFiClient client;
if (client.connect(resolved, 80)) {
Serial.println("PASS layer 5: TCP connection opened");
client.stop();
} else {
Serial.println("FAIL layer 5: TCP connection refused or blocked");
}
}
void loop() {}The first line that says FAIL shows the layer to fix. Do not debug anything above that layer until it passes.
Reading signal strength
RSSI is the received signal strength, reported in dBm. Values are negative, and a number closer to zero is stronger. These ranges are rough guides:
| RSSI | Typical meaning |
|---|---|
| -30 to -60 dBm | Strong and stable |
| -60 to -70 dBm | Usable for most tasks |
| -70 to -80 dBm | Marginal. Expect slow responses and dropouts |
| Below -80 dBm | Unreliable. Connections fail often |
If RSSI is marginal, move the board or the access point, or add an external antenna if your board supports one, before blaming your code. Intermittent failures that disappear when you hold the board next to the router are a signal problem.
Layer 5: server connection failures
client.connect(host, port) returns 1 on success and 0 on failure, and it does not say why. Narrow it down with these checks:
- Wrong port. HTTP uses 80, HTTPS uses 443, and MQTT without TLS uses 1883. A
WiFiClienton port 443 will connect at the TCP level, but the server will reject the plain-text request. - Firewall or private address. A server on your own computer may block incoming connections. Allow the port in the firewall, and use the computer's LAN address, never
localhost. On the board,localhostmeans the board itself. - Name versus address. If connecting by IP address works and connecting by name fails, the fault is DNS.
- Server down. Test the same address from your computer with a browser or a command-line tool before you suspect the board.
HTTPS and certificate failures
With WiFiSSLClient, a failed connection to a host that works over plain HTTP usually points at certificates. The Wi-Fi module can only verify servers whose root certificate is in its certificate store. If the site uses a root certificate the store lacks, the connection fails. Use the IDE's firmware updater tool to add the site's root certificate to the module. Also check the following:
- The board's clock does not need to be right for the connection itself in most cases, but a wrong clock can break certificate validity checks on some stacks.
- Some sites have changed their certificate authority over time, so a site that worked last year may need a new root certificate today.
MQTT and protocol-level failures
If TCP connects but the protocol fails, the fault is above the network layer.
mqttClient.connectreturning false: printmqttClient.connectError()and look up the number in the library's documentation. Typical causes are a wrong broker address, a wrong port, or a broker that requires a username and password.- Messages published but never received: check that the topic strings match exactly, including case and slashes.
- Connection dies after a short time: the sketch is not calling
mqttClient.poll()often enough, so the keep-alive messages stop. Longdelay()calls cause this. - HTTP replies that look like garbage: you may be reading a chunked or compressed response. Request
Connection: closeand plain text, as in the earlier examples.
Problems that appear over time
These failures work at first and then stop:
- Dropped link. Routers restart, and signal changes. Use the non-blocking reconnect pattern from the earlier chapter, and check
WiFi.status()every pass throughloop. - Stale client objects. After a drop, a client may stay in a half-open state. Call
client.stop()before you reconnect. - Memory. Heavy use of
Stringcan fragment memory on small boards and cause crashes after hours. Prefer fixed character buffers for long-running sketches. - Blocking code. A
delay(10000)inloopcan starve the network stack and cause timeouts.
A short checklist
- Print the status name, not just a number.
- Scan, and confirm the network is visible on 2.4 GHz.
- Retype the SSID and password, and check for trailing spaces.
- Print the IP and gateway. A
0.0.0.0address means DHCP did not finish. - Resolve a hostname.
- Connect by IP address and by name, on the right port.
- Compare with a working device on the same network.
Exercise
Your diagnostic sketch prints a valid IP address and gateway, and WiFi.hostByName("example.com", resolved) fails. What does this tell you, and what do you check next?
Show answer
The board has joined the network and DHCP worked, so layers 1 to 3 are fine. The failure is at the DNS layer, which means either the router's DNS service is not working or the network has no internet access (for example, a captive portal or a lost uplink). Check whether another device on the same network can reach the internet. Then try connecting to a known server by IP address. If that works, the problem is DNS only.
Exercise
A sketch using WiFiSSLClient fails to connect to a site on port 443, but the same request works with WiFiClient on port 80 to a different host. Name two possible causes and say how you would tell them apart.
Show answer
One cause is a missing root certificate in the Wi-Fi module's store. The other is a wrong or blocked port or host, since the two tests used different hosts. To tell them apart, test the same host over both protocols if it supports both, or test a host known to work over HTTPS, such as example.com. If the known host works, the board and TLS are fine, and the first site needs its root certificate added with the firmware updater tool.
Exercise
The status prints WL_NO_SSID_AVAIL although the router is in the same room. List the first three things you check.
Show answer
First, check that the SSID in arduino_secrets.h matches the network name exactly, including case and spaces. Second, run a scan and see whether the network appears, because the board may be unable to see a 5 GHz-only network. Third, check that the router's 2.4 GHz radio is enabled and on a channel the board supports.