Project showcase

Control your MKR1000 with aREST Framework © GPL3+

You can control your MKR1000 remotely with a RESTFUL interface.

  • 5,187 views
  • 5 comments
  • 12 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

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");
}

Schematics

Breadboard
Mkr1000 arest bb
Schematic
Mkr1000 arest schem

Comments

Similar projects you might like

A Microwave Interface for the IKEA Duktig Kids Kitchen

Project showcase by Myles Eftos

  • 634 views
  • 3 comments
  • 5 respects

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 896 views
  • 6 comments
  • 10 respects

Servo Signals and Characterization

by 3 developers

  • 723 views
  • 1 comment
  • 10 respects

Cellular IoT with Blynk & Hologram

by Moheeb Zara

  • 4,785 views
  • 9 comments
  • 16 respects

Monitoring Temperature Remotely with Blynk for Dummies

Project in progress by Cmtelesann

  • 1,054 views
  • 2 comments
  • 4 respects

Add Remote Control to Your Lamp

Project tutorial by littlelab

  • 653 views
  • 2 comments
  • 4 respects
Add projectSign up / Login