The Arduino MKR1000 is one of the best boards that Arduino released to build WiFi-connected projects. The board includes a powerful 32-bit processor, along with a WiFi chip, all in a small form factor. In this article, we are going to see how to control an Arduino MKR1000 board using the aREST library. We’ll learn how to control output digital pins, and also how to read data from an analog sensor. You will then be able to use that for your home automation, robotics, and other projects using the MKR1000. Let’s dive in!

Hardware & Software Requirements

Let’s first see what we need for this project. We are simply going to use an LED & a 330 Ohm resistor to see how to control outputs using aREST. For the sensor, we’ll use a photocell with a 10k Ohm resistor in order to measure the ambient light level. This is the list of components that are required for this project:

On the software side, you will need the latest version of the Arduino IDE. You will also need to have the aREST & the WiFi101 libraries installed, which you can find using the Arduino library manager. Finally, you will need to install the board definitions for Arduino SAMD boards.

Hardware Configuration

We are now going to see how to configure the project. Because we don’t have a lot of components here, it will all be very easy. We’ll basically connect the photocell & resistor to pin A0, and the LED + resistor on pin 5. This is a schematic to help you out:

Screen Shot 2016-06-14 at 22.15.51

This is the final result:

hw

 

Configuring the Board

We are now going to configure the board, so we can access it using aREST. The sketch starts by including the required libraries:

#include <SPI.h>
#include <WiFi101.h>
#include <aREST.h>

Then, we create an instance of the aREST library:

aREST rest = aREST();

You need to change inside the code the name of your WiFi network, and the password:

char ssid[] = "wifi-name";
char password[] = "wifi-pass";

To store the ambient light level measurements, we define a variable:

float lightLevel;

In the setup() function of the sketch, we expose this measurement variable to the aREST API:

rest.variable("light",&lightLevel);

We also give an ID and a name to the board:

rest.set_id("1");
rest.set_name("mkr1000");

Inside the loop() function of the sketch, we measure the reading coming from the A0 pin, and then we convert this to a percentage:

float reading = analogRead(A0);
lightLevel = (reading/1024.)*100;

Finally, we check if there is a client that sent a request to the board, and process it with the aREST library:

WiFiClient client = server.available();
if (!client) {
  return;
}
while(!client.available()){
  delay(1);
}
rest.handle(client);

You can of course find the complete code on the GitHub repository of the project:

https://github.com/marcoschwartz/mkr1000-arest-control

Make sure to modify the code with your own WiFi credentials, and then upload it to the board. Then, quickly open the Serial monitor: you should see the IP address of the board displayed there.

Using aREST to Control the Arduino MKR1000

We are now going to use a simple web browser to control our board via WiFi. For the rest of this article, I’ll assume the IP address of the board will be 192.168.0.106, but you of course need to use the IP address returned by the board. First, type the following command to set pin 5 as an output:

http://192.168.0.106/mode/5/o

You should get the confirmation inside your browser:

{"message": "Pin D5 set to output", "id": "1", "name": "mkr1000", "hardware": "arduino", "connected": true}

You can then set the LED pin to a high state with the following command:

http://192.168.0.106/digital/5/1

The LED should immediately light up. To get the value from the ambient light level measurement, simply type:

http://192.168.0.106/light

You will then get the answer in JSON format:

{"light": 53.61, "id": "1", "name": "mkr1000", "hardware": "arduino", "connected": true}

This JSON object can then be used in any web application, for example by a JavaScript-based application.

How to Go Further

In this project, we learned how to control an Arduino MKR1000 board using aREST. We saw how to control digital outputs, and how to read data from an analog sensor. You can now use what you learned in this project to control your projects using the MKR1000 board using aREST, drastically reducing the development time of your connected projects!

Want to Learn More?
If yes, join over 1,000 people who receive exclusive information about the aREST framework. You will also receive a free sample from my book, Discover the aREST Framework. Simply click on the button below!
Get Started!