Web Server Access Point (AP) Mode with MKR WiFi 1010

Learn how to set up your board as an access point, allowing other devices to connect to it.

Introduction

In this tutorial, we will be creating something called an Access Point. An access point is practically a wireless local area network, which in this tutorial will not be connected to the Internet. This can be a practical setup for projects that have no access to the internet, but we still want to control and monitor our MKR WiFi 1010 board wirelessly. We will be using functionalities from the WiFiNINA library, and we will set up a basic configuration that allows us to control an LED (on or off) and do a reading on one of the analog pins.

If you have never used the WiFiNINA library, you can check out this tutorial that shows how to install the library and how to create a connection to your Wi-Fi network.

Hardware & Software Needed

  • Arduino MKR WiFi 1010
  • Micro USB cable
  • Generic LED
  • 82 ohm resistor
  • Breadboard
  • Jumper wires
  • Arduino IDE (offline and online versions available)
  • Arduino SAMD Board Package installed, follow this link for instructions
  • WiFiNINA library (explained later in this tutorial)

You can also use the built-in LED on the MKR WiFi 1010.

Circuit

Follow the wiring diagram below to connect the LED to the MKR WiFi 1010 board.

Circuit with breadboard, resistor and LED.
Circuit with breadboard, resistor and LED.

Schematic

This is the schematic of our circuit.

Schematic of our circuit.
Schematic of our circuit.

Let's Start

This tutorial barely uses any external hardware: we only need an LED that we will control remotely. But the most interesting aspects lie in the library we are going to use: WiFiNINA. This library can be used for many different connectivity projects, where we can both connect to WiFi, make GET requests and - as we will explore in this tutorial - create an access point (AP) and hosting a web server that we can connect to.

We will go through the following steps in order to create a web server on our MKR WiFi 1010:

  • First, we need to initialize the WiFiNINA library.
  • Then, we need to create the credentials of our access point by entering our SSID (name of network) and PASS (password of network).
  • Once the program is running, it will create an access point, start hosting a server, and wait for a client to connect.
  • On our phone or laptop, we can now find the access point in the list of available Wi-Fi networks. We can connect using the same password we created in the program.
  • Once connected to the Wi-Fi, we can connect to the board's specific IP address using our regular browser (e.g. Chrome, Firefox).
  • As long as we are connected, the program detects it, and enters a
    while()
    loop.
  • In the
    while()
    loop, two links are simply printed in HTML format, which is visible in the browser.
  • These links control the LED we connected to the board, simply "ON" or "OFF".
  • If we press the "ON" link, the program is configured to add an "H" to the end of the URL, or if we press the "OFF" link, we add an "L" to the end of the URL.
  • If the URL ends with "H", it will turn on the LED, and if it ends with "L" it turns it off. H stands for "HIGH" and L stands for "LOW".
  • There is also a field displaying the value read from analog pin 1 (A1). Since we have nothing connected, we will read random values.

And that is the configuration we will be using in this tutorial. There are a few other functionalities, such as checking if we have the latest firmware and that we are using the right board, and these potential errors will be printed in the Serial Monitor.

Code Explanation

NOTE: This section is optional, you can find the complete code further down on this tutorial.

The initialization begins by including the WiFiNINA library, after which we need to enter our credentials to our network. If you want to, you can create a secret tab to store your credentials. This can be useful if you want to share your sketch online, and don't want anyone to see your credentials.

Simply replace

"yourNetwork"
and
"yourPassword"
with a network name and password of your choice.

Note: both

ssid
and
pass
needs to be a minimum of 8 characters long

1#include <WiFiNINA.h>
2
3char ssid[] = "yourNetwork"; // your network SSID (name)
4char pass[] = "yourPassword"; // your network password (use for WPA, or use as key for WEP)
5int keyIndex = 0; // your network key Index number (needed only for WEP)
6
7int led = LED_BUILTIN;
8int status = WL_IDLE_STATUS;
9WiFiServer server(80);

We can then configure the

setup()
. Here we set the Serial Communication to 9600, configure the
pinMode
for our LED, and use the line
while(!Serial);
to only initialize the rest of the program as long as we open the Serial Monitor. We do this since important information is printed in the Serial Monitor, and if we upload it, we may risk missing it.

The command

status = WiFi.beginAP(ssid, pass);
creates an access point with the credentials we entered previously, where we then start hosting a web server by using the command
server.begin();
. The function
printWiFiStatus();
prints out information regarding the connection in the Serial Monitor.

1void setup() {
2 //Initialize serial and wait for port to open:
3 Serial.begin(9600);
4 while (!Serial) {
5 ; // wait for serial port to connect. Needed for native USB port only
6 }
7
8 Serial.println("Access Point Web Server");
9
10 pinMode(led, OUTPUT); // set the LED pin mode
11
12 // check for the WiFi module:
13 if (WiFi.status() == WL_NO_MODULE) {
14 Serial.println("Communication with WiFi module failed!");
15 // don't continue
16 while (true);
17 }
18
19 String fv = WiFi.firmwareVersion();
20 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
21 Serial.println("Please upgrade the firmware");
22 }
23
24 // print the network name (SSID);
25 Serial.print("Creating access point named: ");
26 Serial.println(ssid);
27
28 // Create open network. Change this line if you want to create an WEP network:
29 status = WiFi.beginAP(ssid, pass);
30 if (status != WL_AP_LISTENING) {
31 Serial.println("Creating access point failed");
32 // don't continue
33 while (true);
34 }
35
36 // wait 10 seconds for connection:
37 delay(10000);
38
39 // start the web server on port 80
40 server.begin();
41
42 // you're connected now, so print out the status
43 printWiFiStatus();
44}

In the

loop()
, the first thing we do is to check if a client has connected (the client is someone connecting through the browser), using the the conditional
if(status == WL_AP_CONNECTED)
. The command
WiFiClient client = server.available();
is used to start listening to incoming clients, and if a client is connected, we print it out in the Serial Monitor. If a client connects, the program enters a while loop, as long as it is connected.

We will then create a HTTP response that will be displayed in the browser. This is written in HTML, and simply contains three items: a link to turn an LED ON, a link to turn the LED off, and one to display the value recorded from A1 (a random value). Clicking the ON link adds "/H" to the end of the URL, and OFF adds "/L" to the URL.

Just before the end of the loop, we create two conditionals, that check whether the URL ends with "/H" or "/L", and simply turn the LED ON or OFF as a result. Then we use the command

client.stop();
outside the while loop, so if the client disconnects, the client stops.

1void loop() {
2 // compare the previous status to the current status
3 if(status != WiFi.status()) {
4 // it has changed update the variable
5 status = WiFi.status();
6
7 if(status == WL_AP_CONNECTED) {
8 // a device has connected to the AP
9 Serial.println("Device connected to AP");
10 } else {
11 // a device has disconnected from the AP, and we are back in listening mode
12 Serial.println("Device disconnected from AP");
13 }
14 }
15
16 WiFiClient client = server.available(); // listen for incoming clients
17
18 if (client) { // if you get a client,
19 Serial.println("new client"); // print a message out the serial port
20 String currentLine = ""; // make a String to hold incoming data from the client
21 while (client.connected()) { // loop while the client's connected
22 if (client.available()) { // if there's bytes to read from the client,
23 char c = client.read(); // read a byte, then
24 Serial.write(c); // print it out the serial monitor
25 if (c == '\n') { // if the byte is a newline character
26
27 // if the current line is blank, you got two newline characters in a row.
28 // that's the end of the client HTTP request, so send a response:
29 if (currentLine.length() == 0) {
30 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
31 // and a content-type so the client knows what's coming, then a blank line:
32 client.println("HTTP/1.1 200 OK");
33 client.println("Content-type:text/html");
34 client.println();
35
36 // the content of the HTTP response follows the header:
37 client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
38 client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");
39
40 int randomReading = analogRead(A1);
41 client.print("Random reading from analog pin: ");
42 client.print(randomReading);
43
44 // The HTTP response ends with another blank line:
45 client.println();
46 // break out of the while loop:
47 break;
48 }
49 else { // if you got a newline, then clear currentLine:
50 currentLine = "";
51 }
52 }
53 else if (c != '\r') { // if you got anything else but a carriage return character,
54 currentLine += c; // add it to the end of the currentLine
55 }
56
57 // Check to see if the client request was "GET /H" or "GET /L":
58 if (currentLine.endsWith("GET /H")) {
59 digitalWrite(led, HIGH); // GET /H turns the LED on
60 }
61 if (currentLine.endsWith("GET /L")) {
62 digitalWrite(led, LOW); // GET /L turns the LED off
63 }
64 }
65 }
66 // close the connection:
67 client.stop();
68 Serial.println("client disconnected");
69 }
70}

The final piece of code in this program is the

printWiFiStatus()
function. This one is only designed to print relevant information about the connection in the Serial Monitor. The information printed is the name of our access point and the IP address of the board.

1void printWiFiStatus() {
2 // print the SSID of the network you're attached to:
3 Serial.print("SSID: ");
4 Serial.println(WiFi.SSID());
5
6 // print your WiFi shield's IP address:
7 IPAddress ip = WiFi.localIP();
8 Serial.print("IP Address: ");
9 Serial.println(ip);
10
11 // print where to go in a browser:
12 Serial.print("To see this page in action, open a browser to http://");
13 Serial.println(ip);
14
15}

Complete Code

If you choose to skip the code building section, the complete code can be found below:

Upload Sketch and Testing the Program

Once we are finished with the coding, we can upload the sketch to the board. Once it is successful, open the Serial Monitor and it should look like the following image.

Access Point created.
Access Point created.

This means that we successfully created an access point. To connect to it, we can either use a laptop or a smartphone that can connect to Wi-Fi. We will need to disconnect from our current Wi-Fi network, so it is easier to use a phone to connect. Go to your phone's settings, and open the list of Wi-Fi networks. The name of the network we created earlier should show up now, there we need to enter the password we created to connect to it.

Find and connect to our board.
Find and connect to our board.

Once we are connected to the Wi-Fi, we can enter the URL (IP address of the board) in the browser. This is the one printed in the Serial Monitor. Now, we should see an almost empty page with two links at the top left that say "Click here to turn the LED on" and "Click here to turn the LED off", and a text saying: "Random reading from analog pin: xxx". Depending on if you are using a computer or a phone, they will look like the image below.

Interacting through browsers.
Interacting through browsers.

When interacting with the links, you should see the LED, connected to pin 2, turn on and off depending on what you click, and now we have successfully created a way of interacting with our MKR WiFi 1010 board remotely. Whenever we click on these links, the Serial Monitor will print information regarding the requests, as the image below shows.

Client is connected.
Client is connected.

Troubleshoot

If the code is not working, there are some common issues we might need to troubleshoot:

  • We have not updated the latest firmware for the board.
  • We have not installed the Board Package required for the board.
  • We have not installed the WiFiNINA library.
  • We have not entered the SSID and PASS: remember, it is case sensitive.
  • We have not selected the right port to upload: depending on what computer we use, sometimes the board is duplicated. By simply restarting the editor, this issue can be solved.

Conclusion

In this tutorial, we learned how to create an access point, and how to host a web server that we can connect to. We learned how to control an LED remotely, as well as how to display the value of an analog pin in the browser. This method can be very useful when creating a wireless system of sensors and actuators. If you have more than one Wi-Fi compatible board, we can also connect these to the same access point.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.