Sub topic 2
Network basics the board relies on
This chapter covers the networking concepts that every later chapter uses: IP addresses, DHCP, TCP, ports, and DNS. It assumes you can write a basic sketch with setup, loop, and Serial output. It also assumes you can install boards and libraries in the Arduino IDE or CLI, and that you know what an SSID and a router are. You do not need to connect a board yet. The code here only shows how these concepts appear in the Wi-Fi library calls you will use later.
Why these five ideas matter
When a connection fails, the board rarely says why. It usually reports a status code or false. To read those results you need a working model of what the board does between "join the Wi-Fi network" and "receive a web page". That sequence has five parts:
- The board joins the Wi-Fi network, which gives it a link to the router.
- The board gets an IP address, usually through DHCP.
- The board turns a host name into an IP address with DNS.
- The board opens a TCP connection to an address and a port.
- The board sends application data over that connection, such as HTTP or MQTT.
Each step depends on the one before it. A failure at step 2 looks different from a failure at step 4, and the fix is different too.
IP addresses
An IP address identifies a device on a network. An IPv4 address is four numbers from 0 to 255 separated by dots, such as 192.168.1.42. The WiFiNINA library you will use on boards like the MKR WiFi 1010 and Nano 33 IoT works with IPv4 addresses.
Your board receives an address from your router. That address is almost always a private address. Private ranges are reserved for local networks and are not reachable directly from the internet. The common ranges are:
| Range | Typical use |
|---|---|
| `192.168.0.0` to `192.168.255.255` | Home and small office routers |
| `10.0.0.0` to `10.255.255.255` | Larger or managed networks |
| `172.16.0.0` to `172.31.255.255` | Some corporate and virtual networks |
Several things follow from this:
- Your board's address is only meaningful inside your local network. A phone on mobile data cannot open
192.168.1.42. - Your router uses network address translation (NAT) to share one public address among all your devices. Outgoing connections work without any setup. Incoming connections from the internet are blocked unless you configure the router.
- The address can change. Unless you reserve it in the router, the board may get a different address after a reboot.
The Wi-Fi library lets you read the addresses the board has been given:
#include <WiFiNINA.h>
void printNetworkInfo() {
Serial.print("Board IP: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
}The subnet mask tells the board which addresses are on the same local network. A mask of 255.255.255.0 means that addresses sharing the first three numbers, such as 192.168.1.x, are local. The gateway is the router's address. The board sends any traffic for addresses outside the local network to the gateway.
DHCP
The Dynamic Host Configuration Protocol (DHCP) is how a device receives its network settings automatically. After the board joins the Wi-Fi network, it broadcasts a request. The router's DHCP server replies with a package of settings:
- An IP address for the board.
- The subnet mask.
- The gateway address.
- One or more DNS server addresses.
- A lease time, which is how long the board may keep the address before renewing it.
The WiFiNINA library runs DHCP for you inside WiFi.begin(). You do not write DHCP code. This explains a common symptom: the board reports that Wi-Fi is connected, but WiFi.localIP() prints 0.0.0.0. The link to the router exists, but DHCP has not finished or has failed. Network calls will fail until a real address appears.
If you need a predictable address, for example so you can bookmark the board's web page, you have two options:
- DHCP reservation in the router. The router always hands the same address to the board's MAC address. This is the better choice because the board's code stays unchanged.
- Static address in the sketch with
WiFi.config(). You then supply the address, DNS server, gateway, and subnet mask yourself. If you pick an address that another device already uses, or one outside your router's subnet, the board will connect but nothing will work.
#include <WiFiNINA.h>
// Call this before WiFi.begin() to use a fixed address.
void useStaticAddress() {
IPAddress ip(192, 168, 1, 50);
IPAddress dns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, dns, gateway, subnet);
}The MAC address
Every Wi-Fi radio has a MAC address, a six-byte hardware identifier written like A4:CF:12:34:56:78. It identifies the radio on the local link, and routers use it for DHCP reservations and access control lists. You can read it from the board:
#include <WiFiNINA.h>
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
byte mac[6];
WiFi.macAddress(mac);
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) Serial.print("0");
Serial.print(mac[i], HEX);
if (i > 0) Serial.print(":");
}
Serial.println();
}
void loop() {}The library stores the bytes in reverse order, so the loop counts down to print the address in the usual reading order. This sketch does not need a Wi-Fi connection. You can run it as soon as the board is powered.
Ports
An IP address gets data to the right device. A port gets it to the right program on that device. A port is a number from 0 to 65535. A server listens on a port, and a client names the port it wants to reach. A web browser and an email program can both talk to the same server because they use different ports.
Some ports are conventional for particular services:
| Port | Service | Used in this tutorial |
|---|---|---|
| 80 | HTTP | HTTP requests and the board's web page |
| 443 | HTTPS (HTTP over TLS) | Encrypted requests |
| 1883 | MQTT, unencrypted | Publishing and subscribing |
| 8883 | MQTT over TLS | Encrypted MQTT |
| 53 | DNS | Used by the library behind the scenes |
These are conventions, not rules. A server can listen on any port, and you can run a test web server on port 8080 if you want. If you connect to the wrong port, the connection is refused or hangs, which is a frequent cause of "Connection failed" messages.
A connection is identified by four values: the client address, the client port, the server address, and the server port. When your board connects out, the client port is chosen automatically. You only choose the server port.
TCP
The Transmission Control Protocol (TCP) provides a reliable, ordered stream of bytes between two programs. HTTP, HTTPS, and MQTT all run on top of TCP. The library hides most of the detail, but three properties affect how you write sketches.
A connection has a setup phase. Before any data moves, the two sides exchange a few packets to agree to talk. This is the handshake. In code, client.connect(host, port) performs the handshake and returns true or false. It blocks while it waits. If the server is unreachable, the call can take several seconds to give up, and your loop() does not run during that time.
The data is a stream, not a set of messages. TCP does not preserve the boundaries of your writes. If the server sends 1000 bytes, you might receive them as one chunk or several. client.available() tells you how many bytes are waiting right now, and that number can be anywhere from 0 to the full response. Code that assumes a whole reply has arrived the moment available() becomes nonzero will break intermittently.
The connection can end at any time. Either side can close it, and Wi-Fi can drop it. client.connected() reports whether the connection is still open. The pattern you will use again and again is to keep reading while data is available or the connection is open:
#include <WiFiNINA.h>
// Reads everything the server sends until the connection closes.
void readAll(WiFiClient &client) {
while (client.connected() || client.available()) {
while (client.available()) {
Serial.write(client.read());
}
}
}The condition has two parts for a reason. A server may send its last bytes and close immediately. At that moment connected() is false, but unread bytes can still sit in the buffer. Checking available() as well keeps you from losing the end of the reply.
TCP and UDP
The other common transport is the User Datagram Protocol (UDP). UDP sends independent packets with no handshake, no retransmission, and no ordering guarantee. DNS lookups and time requests (NTP) use it. This tutorial uses TCP for everything you write yourself, because HTTP and MQTT need its reliability.
DNS
The Domain Name System (DNS) translates a host name such as example.com into an IP address. Machines connect by address, so a name must be resolved first. The DHCP reply gave your board the address of a DNS server, usually your router, which forwards the question onward.
When you call client.connect("example.com", 80), the library resolves the name and then opens the TCP connection. If the name lookup fails, connect() returns false, and the message looks the same as a refused connection. You can separate the two cases by resolving the name yourself:
#include <WiFiNINA.h>
#include "arduino_secrets.h"
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) {
delay(5000);
}
IPAddress resolved;
if (WiFi.hostByName("example.com", resolved)) {
Serial.print("example.com resolves to ");
Serial.println(resolved);
} else {
Serial.println("DNS lookup failed");
}
}
void loop() {}This sketch uses the arduino_secrets.h file from the next chapter. If the lookup fails while Wi-Fi is connected, the cause is usually a missing or wrong DNS server, or a network that blocks outside DNS. Connecting to a literal IP address such as 93.184.215.14 skips DNS entirely, which makes it a useful test. Do not rely on a hard-coded address in finished code, because services change addresses and many share one address among several names.
Putting the layers together
The table summarizes which layer a symptom points to. Later chapters return to it.
| Symptom | Layer to suspect | First check |
|---|---|---|
| `WiFi.begin()` never returns `WL_CONNECTED` | Wi-Fi link | SSID, password, 2.4 GHz band, signal strength |
| Connected, but IP shows `0.0.0.0` | DHCP | Router DHCP server, address pool not exhausted |
| Name lookup fails, literal IP works | DNS | DNS server address, network filtering |
| `connect()` returns false for an IP address | TCP or port | Port number, server running, firewall |
| Connection opens, then data is garbled | Application protocol | HTTP format, TLS versus plain port |
Edge cases to know about
- Guest networks and client isolation. Many routers have a guest mode that stops devices from seeing each other. A board on such a network can reach the internet but cannot be reached by your laptop, so a web page served from the board appears dead.
- Captive portals. Hotel, school, and cafe networks often require a browser login before they pass traffic. A board cannot complete that login, so it joins the Wi-Fi network but gets no real access.
- 2.4 GHz only. The radios on the boards in this tutorial do not support the 5 GHz band. If your router merges both bands under one name, the board normally still joins the 2.4 GHz side, but some routers handle this poorly.
- Lease expiry. A board that stays on for weeks renews its DHCP lease automatically. If the router restarts and gives out different addresses, a long-running connection can break and must be re-opened.
Exercise
Your board prints "Connected to Wi-Fi", but WiFi.localIP() shows 0.0.0.0, and client.connect("example.com", 80) returns false. Which layer is the likely cause, and which two tests would you run next?
Show answer
The likely cause is DHCP, because the board has a link to the router but no IP address, and name lookup and TCP both need an address. First, check the router's DHCP server and address pool, and wait or restart the join. Second, once an address appears, call WiFi.hostByName("example.com", resolved) to test DNS separately, then try client.connect() with a literal IP address to test TCP and the port without DNS.
Exercise
A server sends its reply and closes the connection. Why does the read loop use while (client.connected() || client.available()) instead of only client.connected()?
Show answer
When the server closes the connection, connected() becomes false, but bytes it sent just before closing may still be in the receive buffer. Checking available() as well reads those remaining bytes, so the end of the reply is not lost.
Exercise
You want your board's web page to always be at the same address on your home network. Name two ways to do this and say which one you prefer and why.
Show answer
You can reserve an address for the board's MAC address in the router's DHCP settings, or set a static address in the sketch with WiFi.config(). A DHCP reservation is preferable because the router stays the single source of network settings, so there is no risk of a clash with another device and the sketch does not change if you move to a different network.