Arduino Rest API in IoT project

Arduino Rest API is a mechanism to exchange data between Arduino and other external systems. Exploiting Arduino Rest API a client application reads or sends information to Arduino board. A typical use case of Arduino Rest API is an external system or application that retrieves sensor values. Arduino Rest API can be used in IoT projects when differents systems and boards are connected together and exchange information. Even IoT cloud platforms use Arduino Rest API mechanism. This kind of mechanism is useful when an external application (client) sends a request and Arduino replies with some data. Arduino Rest API works over HTTP protocol so this kind of requests are synchronous. In 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 Rest API paradigm there is an interesting library called aRest. This library is a framework that supports Rest services and provides several interesting features. This library supports different dev boards like Arduino, Raspberry, ES8266. You can find more information at 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 :

  • reading pin values in rest style
  • writing pin values in rest style
  • Remote sketch function call

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 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 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 describes how to put it in practice. In the example, we want to control a led strip using Rest API calls. The sketch is simple because we have to focus to the Arduino Rest API. The led strip is Neopixels RGB Stick Board and using the Adafruit library 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 a different Neopixel components but the connections are the same.

Using 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 as Arduino Rest API is setColor. So the sketch registers it at line 36 calling it rgb.

Let’s run the sketch on 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 screen shots with different led color, controlled by Rest request from a browser.

arduino rest green

arduino rest blue

arduino rest red

And below the video showing Arduino at work:

At the end of this tutorial, you gained the knowledge about how to use Arduino Rest API to send and receive data from Arduino using Rest style requests.