Implement Arduino REST API in IoT Projects

DZone 's Guide to

Implement Arduino REST API in IoT Projects

Francesco Azzola demonstrates how to use REST-style requests with an Arduino and LED device and introduces the aREST library.

· IoT Zone ·
Free Resource

The Arduino REST API is a mechanism to exchange data between Arduino and other external systems. When exploiting the Arduino REST API, a client application reads or sends information to an Arduino board. A typical use case for the Arduino REST API is an external system or application that retrieves sensor values. The API can be used in IoT projects when differents systems and boards are connected together and exchange information. Even IoT cloud platforms use this mechanism. It is useful when an external application (client) sends a request and Arduino replies with some data. The API works over an HTTP protocol so these requests are synchronous. In an IoT application, there are other protocols that are much more efficient than HTTP (like MQTT). Arduino REST API over HTTP plays an important role in a client-server scenario where Arduino acts as a server. MQTT, for example, uses a different pattern like publish-subscriber.

Arduino Rest API: aRest library

To implement the REST API paradigm there is an interesting library called aREST . This library is a framework that supports REST services and provides several features. It supports different dev boards like Arduino, Raspberry Pi, ES8266, etc. You can find more information at the aREST website. This library is simple to use and can be downloaded directly from Arduino library through Arduino IDE.

Using this library we can implement Arduino REST API paradigm because aREST support 

  • Reads pin values in REST style
  • Writes pin values in REST style
  • Remote sketch function calls

For example, an external application or system can read the pin value using a simple HTTP request. Moreover, the same app or system can set the pin value using an HTTP REST request. This is useful when we have an app that runs on a smartphone that wants to interact with an Arduino board. This interaction takes place using Arduino REST API.

One interesting aspect of aREST library is the capability to expose Arduino sketch function in a REST style. These sketch functions are called directly using a REST HTTP request.

Arduino REST API Implementation

Now that we know the basic concepts about Arduino REST API and how to use it to integrate Arduino with an external system, it is time to put it in practice. In the example, we want to control an LED strip using REST API calls. The sketch is simple because we have to focus on the Arduino REST API. The LED strip is a Neopixels RGB Stick Board and uses the Adafruit library, and it is possible to select the single RGB LED color. The sketch below shows how to wire it to Arduino UNO.

arduino rest api

This picture uses different Neopixel components but the connections are the same.

Using  the Arduino REST API request, we want to set the LED strip color. The color is passed to the sketch function as a parameter in HEX format. This example demonstrates how powerful is this library. The Arduino code is simple:

// Create aREST instance
aREST rest = aREST();
// NeoPixel Init
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, 
                                NEO_GRB + NEO_KHZ800);
void setup() {

  // Register RGB function
  rest.function("rgb", setPixelColor);
  Serial.println("Try DHCP...");
  if (Ethernet.begin(macAdd) == 0) {
    Serial.println("DHCP FAIL...Static IP");
    Ethernet.begin(macAdd , ip, myDns, myGateway) ;

  Serial.print("server IP: ");

  Serial.println("Setup complete.\n");

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

int setPixelColor(String hexColor) {
  hexColor="0x" + hexColor;
  Serial.println("Hex color " + hexColor);
  long n = strtol( &hexColor[0], NULL, 16);
  Serial.println("N :" + String(n));
  long r = n >> 16;
  long g = n >> 8 & 0xFF;
  long b = n & 0xFF;

  // set single pixel color
  return 1;

The Arduino function that we want to make available in the Arduino REST API is setColor. So the sketch registers it at line 36 calling it rgb.

Let’s run the sketch on the Arduino UNO board and make a simple HTTP request using the browser. Let us suppose we want to set the red color, the result is shown below:

These are some screenshots with different LED colors, controlled by REST requests from a browser:

arduino rest green

arduino rest blue

arduino rest red

And below the video showing Arduino at work:

Hopefully, you have learned how to use the Arduino REST API to send and receive data from Arduino using REST style requests.

arduino, arest, internet of things, iot development, rest api

Published at DZone with permission of Francesco Azzola , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}