#API and IoT the perfect convergence: Arduino Rest API

Arduino Rest API is a mechanism to exchange data between Arduino and other external systems.  The convergence between API and IoT opens new integration scenarios. The development of an API ecosystem is an interesting topic and the change to access to IoT service exposed by remote IoT boards using API  is an 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 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.

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.

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 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 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.


#API and #IoT a perfect convergence. Find out how to create an API ecosystem using IoT services and boardsClick To Tweet

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 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.

You can integrate Android with Arduino exposing a set of API. In the article how to use API in Android Things, you will find out how to expose Android Things API

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.

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.

More posts...

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.