Programming WiFi on Arduino with the WiFi101 library


The WiFi101 library allows you to programmatically use WiFi technology on Arduino systems powered by Arduino WiFi Shield 101 or directly using the Arduino MKR1000 board that has integrated WiFi. Needless to say, this library is one of the key building blocks for the development and use of Arduino as a IoT solution.

In this article you will see how the WiFi101 library is structured, its commands and functions, through the creation of simple programs. These practical examples will highlight the basic concepts of the library in a gradual manner thus allowing you to become familiar with this library.

Arduino and the WiFi101 Library

Using WiFi on Arduino

At the current time, when the Internetet of Things (IoT) is increasingly becoming a pressing reality to which it is not possible to abstain, it seems impossible to continue working with the Arduino boards without taking advantage of WiFi technology and all that entails.

The ability to connect to WiFi networks not only allows the exchange of data between the various devices that make up our project without using wires or cables, but makes our project more appropriate and in line with current prerogatives. So in order not to fail in this regard modules have been developed that allow the easy integration of WiFi technology on all Arduino systems.

Arduino WiFi Schield 101
Arduino WiFi Schield 101

For systems already present and established on the market, such as Arduino UNO and Arduino MEGA, a special shield has been developed that integrates perfectly with them: Arduino WiFi Shield 101. While the latest Arduino boards, such as Arduino MKR1000 and Arduino YUN they are already produced with the Integrated WiFi system.

Arduino MKR1000 dimensioni
Arduino MKR1000

But for you to use WiFi technology, not only the hardware part is enough, but also a series of tools that allow us to exploit it programmatically within our sketches in the Arduino IDE development environment.

In this regard, the WiFi101 library has been released, allowing you to fully exploit and manage WiFi through a series of commands and functions to be included in our programs.

Arduino Shield WiFi101

We said that the Arduino boards can use the potential of Wifi networks, using the Arduino WiFi shield 101. This shield is mounted directly above the Arduino boards using the SPI bus.

On the Arduino UNO boards and the like, the pins used are the digital PINs 11,12,13 while on Arduino Mega the PIN 50,51,52. While as SSI we have on Arduino Uno and similar PIN 10 while on the Mega the PIN 53 will have to be kept as output otherwise there will be problems of malfunctioning of the system.

Arduino WiFi101 shield - pinout
Arduino WiFi101 shield – pinout

In addition to the PINs that have just been indicated, other PINs cover other necessary features and therefore can not be used for other purposes.

  • PIN 7 is used as a pin handshake between the Arduino board and the WiFi shield
  • PIN 6 is connected to the button integrated on the board
  • PIN 5 corresponds to the RESET line of the WINC1500 module integrated on the module.

WiFi101 and the network configuration

Before starting to see the library in detail, it is necessary to make a small clarification on the configurations of the Wifi network. In fact, there are different types of WiFi networks. Their type varies depending on the cryptography system used. As for the WiFi101 library and therefore all the Arduino systems in general, all WiFi connections are available that make use of WEP and WPA2 Personal encryption systems. You will not be able to make connections that use WPA2 Enterprise as a cryptographic system.

Regardless of the type of WiFi used, the purpose of the WiFi101 library is to make any type of connection as simple as possible. However, a minimum configuration is required in which both the SSID and the password will be specified. If the connection is WEP then as password will need to pass a hexadecimal string that is equivalent to the key. A WEP network can have up to 4 different keys, each of them assigned to a corresponding Key Index value. So in this case it will be necessary to specify in the configuration not only the SSID and the key (password) but also the key number.

The WiFi101 library

The WiFi101 library is very similar to the Ethernet and WiFi libraries, which are also present as standard libraries in the Arduino IDE development environments. In fact, many functions that perform the same operations on these last two libraries have retained the name, in order to make sketch development with Arduino easier and more coherent.


The WiFi101 library requires that the firmware installed on the cards or shield is in line with it. When updating the library to a newer version, the corresponding firmware must also be updated. To do this, it is highly recommended that you use the Library Manager in the Arduino IDE to monitor both versions and update them if necessary.

WiFi101 Arduino IDE library Manager and firmware version
Library Manager

The classes of the WiFi101 library

The WiFi101 library has the following classes:

  • WiFi – initializes the Ethernet library and network settings
  • IPAddress – provides information about the network configuration
  • Server – creates WiFi servers that send and manage data packets from clients
  • Client – connects to a Wifi server to send data packets
  • UPD – manages UPD messages to send and receive

The WiFi class

The WiFi class is the main class of the library and plays the basic role. In fact every object of the WiFi class corresponds to a WiFi connection.

The library will then provide a series of functions that allow us to manage a connection. For example the functions

  • begin()
  • end()

they correspond to the opening and closing of a WiFi connection. The begin() function initializes the WiFi class and can have different arguments depending on the type of WiFi network to which we want to connect.

WiFi.begin(ssid, pass);
WiFi.begin(ssid, keyIndex, key);

To better understand this function, let’s see how it fits into an Arduino sketch with a small example.

In this case you want to connect our Arduino to a WiFi network with WPA encryption and then you need to specify an SSID network (the network name) and password. Then you will use the begin() function of the WiFi class inside setup(), because it is used only once: once the connection is opened (initialization) you will pass to its use (inside loop()).

Another interesting feature of the WiFi class is config().

This function allows us to specify the configuration to be used by defining a static IP address, and the DNS, gateway and subnet addresses of the WiFi module (both shield and integrated) on Arduino. Generally you should use it before calling the begin() function.

WiFi.config(ip, dns);
WiFi.config(ip, dns, gateway);
WiFi.config(ip, dns, gateway, subnet);

Now you can see the use of this function with an example sketch. First you must define a static IP address and we can do this by instantiating an IPAddress object, which is still part of the WiFi library.

In this example, after making the connection (if all went well), the static IP address of the Arduino WiFi module will be displayed on the terminal. This information is obtained through the function localIP() which returns a value of the IPAddress class. To display it in a classical way, ie type “” it is necessary to add an IpAddress2String() function not present in the library. The implementation code of this function is at the end of the sketch.

There is also a very useful set of functions, especially when you want to scan the available WiFi networks.

  • scanNetworks()
  • SSID()
  • RSSI()
  • encryptionType()

The scanNetworks() function scans the available WiFi networks and returns the number. So we realize that 7 networks have been detected, one of these numbers will be assigned to each detected network. The SSID(), RSSI(), and encryptionType() functions, providing this number as an argument, will provide the network name, signal strength, and cryptography type, respectively.

There is no better way to learn these functions than to use them in an example. Here is a sketch that uses all of them.

Other features that can be useful:

  • ping()
  • status()
  • macAddress()
  • BSSID()

The ping() function allows you to check the presence on the network of a device with a specific IP address.

The status() function checks and returns the current state of the WiFi connection. This function is used as it is very useful within the conditions during programming. The possible states are as follows

  • WL_CONNECTED: assigned when connected to a WiFi network;
  • WL_AP_CONNECTED : assigned when a device is connected in Access Point mode;
  •  WL_AP_LISTENING : assigned when the listening for connections in Access Point mode;
  • WL_NO_SHIELD: assigned when no WiFi shield is present;
  • WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);
  • WL_NO_SSID_AVAIL: assigned when no SSID are available;
  • WL_SCAN_COMPLETED: assigned when the scan networks is completed;
  • WL_CONNECT_FAILED: assigned when the connection fails for all the attempts;
  • WL_CONNECTION_LOST: assigned when the connection is lost;
  • WL_DISCONNECTED: assigned when disconnected from a network;

Finally, the macAddress() function returns the Mac Address value of the WiFi module on the Arduino.


as an argument it accepts an array of 6 bytes that is enhanced by the function.

The BSSID() function returns the mac address of the router to which you are connected.

There is online a very interesting sketch that includes all the previous functions and is very representative of the use of this library. Here is the code.

The IPAddress class

The IPAddress class, which you have already used in the previous examples, is strictly connected to the previous class and its functions have the sole purpose of returning the IP addresses set in the current configuration:

  • localIP()
  • subnetMask()
  • gatewayIP()

The Server class

This class allows you to manage the Arduino module as a WiFi server. Then it will provide a series of functions suitable for the purpose of sending and receiving data from the various connected clients.

Let’s see a brief example of starting a WiFi server listening on port 80.

As you can see from the example after starting the WiFi connection, the WiFi server is activated using the begin() function.
For writing and sending data, the class provides these functions:

  • write()
  • print()
  • println()

Extend the previous example so that messages received from a client are read back to all listening clients.

The Client class

In the same way as the Server class, there is a Client class that manages the sending and receiving of data packets.

Here, too, the functions of reading and writing data are carried out by a set of functions:

  • write()
  • println()
  • print()
  • read()
  • flush()

The flush() function deletes all data that has been received by the client but has not yet been read.

The UDP class

As for the sending and receiving of data, these can be sent via the UDP (User Datagram Protocol) and unlike TCP packets, they work in connectionless mode. That is, the packets are sent without checking their correct reception on the other side (there is no re-sending in case of an error) and there is no package ordering system. On the other hand, this protocol is very fast.

The UDP class takes care of the management of UDP messages to be sent or received. The beginPacket () function initializes a connection for sending a UDP packet.

WiFiUDP.beginPacket(hostName, port);
WiFiUDP.beginPacket(hostIp, port);

while the endPacket() function terminates the UPD package and sends it.

The class provides a series of functions for writing, reading and managing the data to be sent.

  • write()
  • peek()
  • read()
  • flush()

Let’s see an example in which a string is sent and received via UDP messages

Connecting to a WEP network

A type of WiFi network that uses a different cryptography system is the WEP WiFI network. This type of network requires a hexadecimal key of variable length (from 40 to 128 bits) associated with a key number.

The example sketch will show how to connect to a WEP network called “yourNetword” that for key “ABBADEAF01” and as key index “0”.


In this article you have seen how the WiFi101 library allows you to take full advantage of the capabilities of Arduino systems to interface to a WiFi network. You have thoroughly analyzed the library by looking in detail at the classes it is composed of and their relative functions, along with some small examples. Finally, some sketches gave you a general overview of how this library can be used and how Arduino can be integrated into the development of IoT projects.[:]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.