Sub topic 1
Choosing a Wi-Fi board and its library
Prerequisites
This chapter assumes you can write a basic sketch with setup() and loop(), print with Serial, and install boards and libraries in the Arduino IDE or Arduino CLI. You should know the terms IP address, port, router, and SSID (the network name). You need a 2.4 GHz Wi-Fi network that you are allowed to use.
Choosing a Wi-Fi board and its library
A Wi-Fi board has a radio chip built in. Each chip family has its own library, and the function names differ between them. Check the board's documentation before you write code.
| Board family | Wi-Fi chip | Library |
|---|---|---|
| MKR WiFi 1010, Nano 33 IoT | u-blox NINA-W10 | WiFiNINA |
| ESP32-based boards | ESP32 | WiFi (ESP32 board package) |
The code in this chapter uses WiFiNINA.
Network basics the board relies on
Your router assigns the board an IP address through DHCP (Dynamic Host Configuration Protocol). A DNS server turns a name such as example.com into an IP address. Data travels over TCP connections, and each service listens on a port. HTTP uses port 80, HTTPS uses port 443, and MQTT often uses port 1883.
Keeping credentials in a separate file
Do not type your password into the main sketch. Put it in a header file, and exclude that file from version control.
Create arduino_secrets.h beside your sketch:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"Then include it in the sketch with #include "arduino_secrets.h" and use SECRET_SSID and SECRET_PASS in your WiFi.begin() call.
Connecting to your network
This sketch checks that the Wi-Fi module exists, retries until the board connects, and then prints the IP address and signal strength.
#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() {}Exercise
What does WiFi.RSSI() report, and which is the stronger signal: -40 dBm or -80 dBm?
Show answer
It reports received signal strength in dBm. A value closer to zero is stronger, so -40 dBm is the stronger signal.