Project showcase

Control your MKR1000 with aREST Framework © GPL3+

You can control your MKR1000 remotely with a RESTFUL interface.

  • 4,329 views
  • 5 comments
  • 14 respects

Components and supplies

Apps and online services

About this project

It would be nice to communicate with an Arduino thru RESTFUL interface and I found this aREST framework.  As described on its website (arest.io), the aREST framework is a complete solution to build powerful RESTful applications based on the Arduino & the Raspberry Pi platforms. It can handle all kind of communications via Serial, WiFi, Ethernet, and much more.

In this project, I will show how to setup MKR1000 with 2 LEDs and you can control the LEDs via REST API.

Steps to setup the dev environment

1. Setup the MKR100 using this Getting Started guide.

2. Install Wifi101 library using Sketch > Include Library > Manage Libraries ... menu.  (Note: I am using Wifi101 version 0.9.0)

3. Install aREST library. (Note: I am using aREST version 2.1.1)

4 Make the following changes to aREST.h:

  • Change WiFi_h to WIFI_H
  • Change the following

void addToBuffer(float toAdd) {
  char number[10];
  dtostrf(toAdd, 5, 2, number);
  addToBuffer(number);
}

         to

void addToBuffer(float toAdd) {
  char number[10];
  sprintf(number, "%5.2f", toAdd);
  addToBuffer(number);
}

5. Copy the sketch from the code section below.

6. Setup the board and LEDs.

7. Compile and upload the sketch.

About the sketch

Creating a WiFiServer instance listening to port 80:

WiFiServer restServer(80);

Setting the device ID and name:

// Give name and ID to device
rest.set_id("008");
rest.set_name("mighty_cat");

The loop() is simple, just listening to port 80 and handling incoming connection:

void loop() {
  // Handle REST calls
  WiFiClient client = restServer.available();
  rest.handle(client);
}

Exposing the custom function ledControl(String) within setup():

// Function to be exposed
rest.function("led",ledControl);

This is the ledControl(String) function:

// Custom function accessible by the API
int ledControl(String command) {
  Serial.println(command);
  // Get state from command
  int state = command.toInt();
  digitalWrite(5,state);
  return 1;
}

Finally the printWifiStatus() function:

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  IPAddress subnet = WiFi.subnetMask();
  Serial.print("Netmask: ");
  Serial.println(subnet);
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("Gateway: ");
  Serial.println(gateway);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Running the sketch

Open the Serial Monitor using Tools > Serial Monitor menu and note down the IP address shown.  

Display the device ID and name:

To turn on pin 5, type the following to the web browser:

http://<ip-address>/digital/5/1

To turn off, type

http://<ip-address>/digital/5/0

To turn on using exposed function, type  

http://<ip-address>/led?params=1

To Find out More About aREST

Refer to the aREST Arduino library page on GitHub

Schematics

Breadboard
Mkr1000 arest bb
Schematic
Mkr1000 arest schem

Code

MKR100 WiFi aREST skecthArduino
// Import required libraries
#include <SPI.h>
#include <WiFi101.h>

#define DEBUG_MODE 0

#include <aREST.h>

// Create aREST instance
aREST rest = aREST();

char ssid[] = "your_network_ssid";      // your network SSID (name)
char pass[] = "your_network_password";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer restServer(80);

void setup(void)
{
  // Function to be exposed
  rest.function("led",ledControl);

  // Give name and ID to device
  rest.set_id("008");
  rest.set_name("mighty_cat");

  // Start Serial
  Serial.begin(115200);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println();

  // you're connected now, so print out the status:
  printWifiStatus();

  // Start server
  restServer.begin();
  Serial.println(F("Listening for connections..."));

  // Enable watchdog
  //wdt_enable(WDTO_4S);
}

void loop() {

  // Handle REST calls
  WiFiClient client = restServer.available();
  rest.handle(client);

}


// Custom function accessible by the API
int ledControl(String command) {

  Serial.println(command);
  
  // Get state from command
  int state = command.toInt();

  digitalWrite(5,state);
  return 1;
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  IPAddress subnet = WiFi.subnetMask();
  Serial.print("Netmask: ");
  Serial.println(subnet);

  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("Gateway: ");
  Serial.println(gateway);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Comments

Similar projects you might like

Version 2.0 Advanced Attendance System (Without Ethernet)

Project tutorial by GadgetProgrammers

  • 2,545 views
  • 5 comments
  • 43 respects

Get The Arduino Dice

by Ingo Lohs

  • 249 views
  • 0 comments
  • 4 respects

Control LED Using Your Voice Command

by IoTBoys

  • 607 views
  • 0 comments
  • 7 respects

Critter: Crawling Arduino Robot

Project showcase by Slant Concepts

  • 4,837 views
  • 10 comments
  • 30 respects

Musical Fairy Lights

Project tutorial by Ian Cumming

  • 470 views
  • 0 comments
  • 4 respects

Cloud Costume

Project tutorial by Monica Houston

  • 1,029 views
  • 3 comments
  • 10 respects
Add projectSign up / Login