Sub topic 4
Connecting to your network
This chapter covers joining a Wi-Fi network with a WiFiNINA board, such as the MKR WiFi 1010 or Nano 33 IoT. It assumes you can upload sketches and read Serial output. It also assumes you know what an SSID, an IP address and DHCP are, and that your credentials are in an arduino_secrets.h file as described in the previous chapter. You need a 2.4 GHz network you are allowed to use.
What happens when a board connects
A WiFiNINA board has two processors. Your sketch runs on the main microcontroller. The Wi-Fi radio and the network stack run on a separate module. The WiFi object in your sketch sends commands to that module and reads its answers.
WiFi.begin(ssid, pass) asks the module to do four things:
- Scan for an access point with the given SSID.
- Authenticate, using WPA or WPA2 with the passphrase you supplied.
- Associate with the access point.
- Ask the router's DHCP server for an IP address, a subnet mask and a gateway.
The call blocks for several seconds while this happens. It returns a status code. WL_CONNECTED means the module is on the network. Any other value means the attempt failed or has not finished.
The credentials file
The sketch reads the network name and password from arduino_secrets.h. It contains two macros:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"In the Arduino IDE, create this file as a second tab in the sketch folder. Exclude it from version control, for example by adding arduino_secrets.h to .gitignore. Commit a copy with placeholder values so others know which macros to define.
A complete connection sketch
#include <WiFiNINA.h>
#include "arduino_secrets.h"
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.print("Connecting to ");
Serial.println(SECRET_SSID);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(10000);
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Signal strength (RSSI): ");
Serial.println(WiFi.RSSI());
}
void loop() {}Each part of the sketch has a reason:
while (!Serial && millis() < 5000) {}waits up to five seconds for the Serial Monitor on boards with native USB. Without it, the first messages are lost. The time limit keeps the sketch running when no computer is attached.WiFi.status() == WL_NO_MODULEdetects that the module did not answer. A wrong board selection in the IDE can cause this, and so can a board without a WiFiNINA module. Retrying cannot fix it, so the sketch halts withwhile (true) {}.statusstarts asWL_IDLE_STATUS, which is neither connected nor an error. This lets thewhileloop run at least once.- The
delay(10000)after each failed attempt keeps you from hammering the access point. It also gives DHCP time to finish on a slow router. WiFi.localIP()returns the address the router assigned.WiFi.RSSI()returns the signal strength in dBm.
A successful run prints something like this:
Connecting to HomeNet
IP address: 192.168.1.42
Signal strength (RSSI): -58Reading the result
RSSI is a negative number. Values closer to zero are stronger. These ranges are rules of thumb, not guarantees:
| RSSI (dBm) | Typical meaning |
|---|---|
| -30 to -60 | Strong. Reliable for any use in this tutorial. |
| -60 to -70 | Adequate. Occasional retries are possible. |
| -70 to -80 | Weak. Expect slow requests and dropped connections. |
| Below -80 | Barely usable. Move the board or the access point. |
The IP address is almost always in a private range such as 192.168.x.x or 10.x.x.x. Other devices on the same network can reach the board at that address. Devices on the internet cannot reach it directly. The router's NAT (network address translation) hides it. The board can start connections to the internet, but the internet cannot start connections to the board.
DHCP leases are temporary. The router may give the board a different address after a reboot or when the lease expires. If you need a stable address, reserve one for the board's MAC address in the router's settings. Do not hard-code a static IP in the sketch unless you have chosen an address outside the router's DHCP pool.
Status codes you will see
WiFi.status() returns the current state of the module. These are the values you will meet most often:
| Constant | Meaning |
|---|---|
| `WL_NO_MODULE` | The module did not respond. Check the board selection. |
| `WL_IDLE_STATUS` | The module is idle and no attempt has finished. |
| `WL_NO_SSID_AVAIL` | The module cannot see the SSID. |
| `WL_CONNECT_FAILED` | The attempt failed, often because of a wrong password. |
| `WL_CONNECTION_LOST` | An established connection dropped. |
| `WL_DISCONNECTED` | The module is disconnected from the network. |
| `WL_CONNECTED` | The module is connected and has an address. |
The next chapter uses these codes in more detail. For now, print the value when a connection fails, because it tells you where to look.
Variations
WiFi.begin() has overloads for other network types:
WiFi.begin(ssid)joins an open network with no password. You will rarely want this, because open networks send your data unencrypted.WiFi.begin(ssid, keyIndex, key)joins a WEP network. WEP is obsolete and insecure, so use WPA2 or WPA3 on your router if you can.WiFi.begin(ssid, pass)handles WPA and WPA2 personal networks. This is the one you will use.
Enterprise networks that use a username and password per user, with 802.1X authentication, are not covered by these calls. Neither are captive portals, where you accept terms in a browser before getting access. Hotel and campus networks often work this way. A board cannot click through a web page, so use a separate network for the device if you can.
Keep the firmware current
The Wi-Fi module has its own firmware, separate from your sketch. Old firmware causes odd failures, especially with TLS. You can check it from the sketch:
#include <WiFiNINA.h>
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
String installed = WiFi.firmwareVersion();
Serial.print("Installed firmware: ");
Serial.println(installed);
Serial.print("Latest bundled with library: ");
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
if (installed < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Update the firmware with the IDE's firmware updater tool");
}
}
void loop() {}The update itself is done with the firmware updater tool in the Arduino IDE, not from the sketch.
Common errors
Most failures at this stage come from a short list of causes:
- A 5 GHz network. The module supports only 2.4 GHz. If your router uses one name for both bands, the board still works. If the 2.4 GHz band is disabled, the board reports
WL_NO_SSID_AVAIL. - A typo in the SSID or password. Both are case sensitive. A trailing space in the header file is enough to break the match.
- Missing quotes in
arduino_secrets.h. The macros must expand to string literals. If the compiler complains, check the quotes first. - Wrong board or library.
WL_NO_MODULEusually means the selected board does not match the hardware. - MAC filtering or a guest network with client isolation. The router may accept the connection and still block traffic to other devices on the network.
- Weak signal. The board connects at -85 dBm, but later requests time out. Check RSSI before you debug the sketch.
- A sketch that waits forever on Serial. If you remove the five second limit, the sketch never runs when no computer is attached.
Limits of this approach
The sketch in this chapter blocks in setup(). While it waits, the board does nothing else, which is fine for a first test. It is a poor design for a device that must keep reading sensors or driving outputs. It also gives up on recovery: after a successful connection, nothing notices if the access point restarts. A later chapter, "Staying connected without blocking", replaces the blocking loop with a check inside loop() that uses millis().
Exercise
A sketch prints "Connecting to HomeNet" every ten seconds and never reaches the IP address line. You have confirmed the SSID and password are correct. Give two further causes you would check, and one change to the sketch that would help you find the cause.
Show answer
Possible causes: the router's 2.4 GHz band is disabled or the board is out of range (so the SSID is not visible, giving WLNOSSIDAVAIL), the router uses MAC filtering or an unsupported security mode such as WPA2 Enterprise or a captive portal, or the signal is too weak. Helpful change: store the value returned by WiFi.begin() and print it as a number each time, then compare it with the status constants (for example WLNOSSIDAVAIL versus WLCONNECTFAILED) to see whether the SSID is missing or authentication is failing.
Exercise
Why does the connection sketch check for WLNOMODULE and halt, instead of retrying like it does for a failed connection?
Show answer
WLNOMODULE means the Wi-Fi module did not respond at all, usually because the wrong board is selected or the hardware has no module. Retrying WiFi.begin() cannot change that, so halting with a clear message is more useful than looping.