One of the MKR WiFi 1010's core strengths is the ability to connect to a Wi-Fi network. In this tutorial, we will focus on achieving this with the help of the WiFiNINA library. We will first connect to a Wi-Fi network and then look at some functions that can help us gather information such as, the IP address of our board, encryption type, RSSI strength and the name of the network we are connecting to.
This tutorial is a great starting point for any maker interested in making applications connected to the Internet.
This tutorial requires no circuit. We just need a Micro USB cable to connect our MKR WiFi 1010 board to a computer.
The goal with this tutorial is to be able to connect our MKR WiFi 1010 board to a Wi-Fi network, and print relevant information regarding the connection.
The following steps are needed in order to create this program:
1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits ARM Cortex M0+) and install it.
2. Now, we need to install the library needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for WiFiNINA and install it.
3. Then, we need to create a new header file, to store our credentials. We do this, so we don't accidentally store our credentials in a code we may share on the Internet. Depending on what editor you are using, the process is slightly different, but we can follow the instructions below:
If you are using the offline editor, simply click on the arrow pointing downwards on the top right corner, following the picture below.
This will open up a yellow field in the bottom of the editor, where we need to name our file "arduino_secrets.h". When we have named it, click "OK" and a new tab will open.
Inside this tab, we need to enter the following code. Simply replace
"yourNetwork"
and "yourPassword"
with the credentials of your Wi-Fi network. Remember that it is case-sensitive!1#define SECRET_SSID "yourNetwork"2#define SECRET_PASS "yourPassword"
In the online editor, we need to click the downward arrow next to our sketch tab, and click "Add Secret Tab. This will create a tab called Secret.
We now need to head back to the original sketch file, and enter the following code:
1char ssid[] = SECRET_SSID;2char pass[] = SECRET_PASS;
This will automatically create two fields in the Secret tab. If we go back to this tab, we can enter our credentials there.
Note: This section is optional, you can find the complete code further down on this tutorial.
We have now finished creating our secret credential tab in either the offline or online editor, so let's get started with the code! In this section, we will go through the code, step by step, to get familiar with the different programming concepts we are using.
First, we need to include the WiFiNINA library to access its functionalities. We then need to include the arduino_secrets.h file we created (not required in online editor). After that, we need to create two
char
variables, one to store our network name, another to store our network password. If you are using the online editor, we have already pasted this in the code in the previous step. We then set the initial Wi-Fi radio's status to idle.1#include <WiFiNINA.h>2
3#include "arduino_secrets.h"4
5char ssid[] = SECRET_SSID; // your network SSID (name)6char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)7int status = WL_IDLE_STATUS; // the Wifi radio's status
In the
setup()
, we start serial communication, followed by while(!Serial);
, which basically means that unless we open the Serial Monitor, the program will not run. We then create a while loop that checks if we are not connected to Wi-Fi, to begin connecting to it. We then use status = WiFi.begin(ssid, pass);
to start connecting to WiFi, and a delay of 10 seconds to give it enough time to connect.Finally, we will print a simple line, and call on the
printData()
function. This function contains information regarding the connection.1void setup() {2 //Initialize serial and wait for port to open:3 Serial.begin(9600);4 while (!Serial);5
6 // attempt to connect to Wifi network:7 while (status != WL_CONNECTED) {8 Serial.print("Attempting to connect to network: ");9 Serial.println(ssid);10 // Connect to WPA/WPA2 network:11 status = WiFi.begin(ssid, pass);12
13 // wait 10 seconds for connection:14 delay(10000);15 }16
17 // you're connected now, so print out the data:18 Serial.println("You're connected to the network");19
20 Serial.println("----------------------------------------");21 printData();22 Serial.println("----------------------------------------");23}
The loop is very easy: it only executes the
printData()
function every 10 seconds, and then prints a line to separate the information in the Serial Monitor.1void loop() {2 // check the network connection once every 10 seconds:3 delay(10000);4 printData();5 Serial.println("----------------------------------------");6}
The final step of this code is to create the
printData()
function, that we have used in both the setup and loop.The function is quite basic, it simply prints out three different types of information:
This information is then printed in the Serial Monitor.
1void printData() {2 Serial.println("Board Information:");3 // print your board's IP address:4 IPAddress ip = WiFi.localIP();5 Serial.print("IP Address: ");6 Serial.println(ip);7
8 Serial.println();9 Serial.println("Network Information:");10 Serial.print("SSID: ");11 Serial.println(WiFi.SSID());12
13 // print the received signal strength:14 long rssi = WiFi.RSSI();15 Serial.print("signal strength (RSSI):");16 Serial.println(rssi);17
18}
If you choose to skip the code building section, the complete sketch can be found below. Upload the sketch to the board.
Note: You will still need to create a secrets tab yourself and add your credentials if you copy this code.
1#include <WiFiNINA.h>2
3///////please enter your sensitive data in the Secret tab/arduino_secrets.h4char ssid[] = SECRET_SSID; // your network SSID (name)5char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)6int status = WL_IDLE_STATUS; // the Wifi radio's status7
8void setup() {9 //Initialize serial and wait for port to open:10 Serial.begin(9600);11 while (!Serial);12
13 // attempt to connect to Wifi network:14 while (status != WL_CONNECTED) {15 Serial.print("Attempting to connect to network: ");16 Serial.println(ssid);17 // Connect to WPA/WPA2 network:18 status = WiFi.begin(ssid, pass);19
20 // wait 10 seconds for connection:21 delay(10000);22 }23
24 // you're connected now, so print out the data:25 Serial.println("You're connected to the network");26 27 Serial.println("----------------------------------------");28 printData();29 Serial.println("----------------------------------------");30}31
32void loop() {33 // check the network connection once every 10 seconds:34 delay(10000);35 printData();36 Serial.println("----------------------------------------");37}38
39void printData() {40 Serial.println("Board Information:");41 // print your board's IP address:42 IPAddress ip = WiFi.localIP();43 Serial.print("IP Address: ");44 Serial.println(ip);45
46 Serial.println();47 Serial.println("Network Information:");48 Serial.print("SSID: ");49 Serial.println(WiFi.SSID());50
51 // print the received signal strength:52 long rssi = WiFi.RSSI();53 Serial.print("signal strength (RSSI):");54 Serial.println(rssi);55
56 byte encryption = WiFi.encryptionType();57 Serial.print("Encryption Type:");58 Serial.println(encryption, HEX);59 Serial.println();60}
Note: You will still need to create a secrets tab yourself and add your credentials if you copy this code.
When the sketch has been successfully uploaded, open the Serial Monitor and the results should match the following images. Note that the network information in this example is greyed out for security reasons.
If you are using the OFFLINE editor, it will look like this:
If you are using the ONLINE editor, it will look like this:
Congratulations, you have not only connected your board to your Wi-Fi network, but you have also retrieved information from the network. One particularly good feature is the signal strength (rssi). One experiment you can do, is to try to move your MKR WiFi 1010 board close to the router and observe the Serial Monitor.
If the code is not working, there are some common issues we can troubleshoot:
In this tutorial, we have learned how to simply connect to a Wi-Fi network by using the credentials in the code. We also learned how to obtain specific information regarding our connection, such as signal strength, IP address and name of our network.
Now that you have learned a little bit how to use the WiFiNINA library, you can try out some of our other tutorials for the MKR WiFi 1010 board. You can also check out the WiFiNINA library for more examples and inspiration for creating wireless projects!