Discover how to develop Arduino Rest API framework to integrate Arduino with external systems or control it remotely. Find out how to expose Arduino sketch as functions

Arduino Rest API is a mechanism to exchange data between Arduino and other external systems. Using Arduino Rest API framework it is possible to control Arduino remotely. The convergence between API and IoT opens new integration scenarios. The development of an API ecosystem is an interesting topic and the way we access to IoT service, exposed by remote IoT boards, using API  is a challenging aspect.

In more details, 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 framework can be used in IoT projects when different systems and boards are connected together and exchange information. Even IoT cloud platforms use Arduino Rest API mechanism. Generally speaking, this kind of mechanism is useful when an external application (client) sends a request to Arduino and it 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 can be used 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.

If you are new to Internet of things and want to get started building your first IoT project, I suggest you to read how to send sensor data to Google sheet using Arduino.

The video below shows the final result. Using a browser we are able to control Arduino:

Arduino Rest API framework: aRest library

To implement Rest API framework there is an interesting library called aRest. This library is a framework that supports Restful 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 of 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 Arduino 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 exploiting 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 into practice. In this example, we want to control a LED strip using Rest API calls. The sketch is simple because we have to focus on 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 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:

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 screenshots with different led color, controlled by Rest request from a browser.

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. You can further extend this project implementing new features. You can apply the same paradigm to other kinds of IoT boards like ESP8266 and so on. In this example, we have covered how to send data to Arduino invoking an Arduino Rest API, but you can use the same mechanism to read Arduino pin values.



Please enter your comment!
Please enter your name here